我无法首先初始化Firebase应用
我在gradle:app中添加了这些插件
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.google.firebase:firebase-auth:16.0.3'
(我试图使它们版本相同)
apply plugin: 'com.google.gms.google-services'
我也将其添加到gradle:project
classpath 'com.google.gms:google-services:4.1.0'
最后,我将其添加到MainActivity中的onCreate()
FirebaseApp.initializeApp(this);
<<<< AndroidManifest >>>>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.firebasetest">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<<<< MainActivity >>>>
package com.example.firebasetest;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseApp;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
public class MainActivity extends AppCompatActivity {
private FirebaseAuth mAuth;
String email;
String password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FirebaseApp.initializeApp(this);
mAuth = FirebaseAuth.getInstance();
...
}
@Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
...
}
private void updateUI(FirebaseUser currentUser) {
}
}
<<<<<<<<<< gradle:app >>>>>>>>>>
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.firebasetest"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.google.firebase:firebase-auth:16.0.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
apply plugin: 'com.google.gms.google-services'
<<<<<<<<<< gradle:project >>>>>>>>>>
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
classpath 'com.google.gms:google-services:4.1.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
我希望它能正常工作。 但是安装该应用程序后,它甚至没有显示MainActivity
错误消息如下:
E / Android运行时:致命异常:主要 流程:com.example.firebasetest,PID:18544 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.firebasetest / com.example.firebasetest.MainActivity}:java.lang.IllegalStateException:默认FirebaseApp在此过程com.example.firebasetest中未初始化。确保首先调用FirebaseApp.initializeApp(Context)。 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3086) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3229) 在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1926) 在android.os.Handler.dispatchMessage(Handler.java:106) 在android.os.Looper.loop(Looper.java:214) 在android.app.ActivityThread.main(ActivityThread.java:6981) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445) 由以下原因引起:java.lang.IllegalStateException:在此过程com.example.firebasetest中未初始化默认的FirebaseApp。确保首先调用FirebaseApp.initializeApp(Context)。 在com.google.firebase.FirebaseApp.getInstance(SourceFile:218) 在com.google.firebase.auth.FirebaseAuth.getInstance上(未知来源:1) 在com.example.firebasetest.MainActivity.onCreate(MainActivity.java:30) 在android.app.Activity.performCreate(Activity.java:7326) 在android.app.Activity.performCreate(Activity.java:7317) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3066) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3229) 在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1926) 在android.os.Handler.dispatchMessage(Handler.java:106) 在android.os.Looper.loop(Looper.java:214) 在android.app.ActivityThread.main(ActivityThread.java:6981) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
答案 0 :(得分:2)
更改此行
classpath 'com.google.gms:google-services:4.1.0'
此行
classpath 'com.google.gms:google-services:4.2.0'
我认为Google Play服务版本4.1.0有一个错误
答案 1 :(得分:0)
确保将apply plugin: 'com.google.gms.google-services'
放入应用gradle中
答案 2 :(得分:-1)
您需要更新类路径和Firebase身份验证版本 改变这个
classpath 'com.google.gms:google-services:4.1.0'
对此
classpath 'com.google.gms:google-services:4.2.0'
要获得更好的结果,只需更新Firebase身份验证。