默认FirebaseApp未初始化错误

时间:2019-03-13 00:48:34

标签: android firebase firebase-authentication

我看到Default FirebaseApp is not initialized in this process com.armoured. Make sure to call FirebaseApp.initializeApp(Context) first.消息中有一些例外

堆栈跟踪如下:

2019-03-12 20:42:33.607 11511-11511/com.armoured E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.armoured, PID: 11511
java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.armoured. Make sure to call FirebaseApp.initializeApp(Context) first.
    at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@16.0.1:219)
    at com.google.firebase.auth.FirebaseAuth.getInstance(Unknown Source:1)
    at com.armoured.fragments.LoginFragment.onCreateView(LoginFragment.java:59)
    at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2439)
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
    at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
    at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:802)
    at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
    at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
    at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
    at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
    at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3273)
    at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3229)
    at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
    at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:620)
    at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1391)
    at android.app.Activity.performStart(Activity.java:7165)
    at android.app.ActivityThread.handleStartActivity(ActivityThread.java:2975)
    at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:180)
    at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:165)
    at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:142)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6718)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

我的问题与this问题中的问题完全一样。但是,该问题中没有任何解决方案对我有用。我已经将apply plugin: 'com.google.gms.google-services'添加到了我的gradle文件中。我在所有代码中都使用了firebase:16.0.3依赖项。

最后一个细节:我已经在片段中添加了FirebaseApp.initializeApp(Context)。为了完整起见,我在问题中添加了要获取此错误的函数,以便您可能对错误有了解。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    FirebaseApp.initializeApp(getActivity());
    View view = inflater.inflate(R.layout.fragment_login, container, false);
    mViewModel = ViewModelProviders.of(this).get(LoginViewModel.class);
    mAuth = FirebaseAuth.getInstance();
    ButterKnife.bind(this,view);
    return view;
}

编辑:我正在按要求附加应用程序级别gradle级别文件。

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.armoured"
        minSdkVersion 23
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    api 'com.google.android.material:material:1.0.0-beta01'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
    implementation 'com.jakewharton:butterknife:10.1.0'
    implementation 'com.google.firebase:firebase-core:16.0.3'
    implementation 'com.google.firebase:firebase-auth:16.0.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0-beta01'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0-beta01'
    implementation 'androidx.recyclerview:recyclerview:1.0.0-beta01'
    annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'

}

2 个答案:

答案 0 :(得分:2)

在您的问题中,您说过您在自己的build.gradle中添加了apply plugin: 'com.google.gms.google-services',但在那儿看不到它。它应该在您的应用程序级别build.gradle的最底部。也许您修改了顶层build.gradle而不是应用程序的。没有此行肯定会引起您所看到的问题。

也不再需要使用相同版本的Firebase依赖项。他们现在各自独立修订。如果您是第一次集成Firebase,则应使用setup documentation中显示的所有最新版本。

答案 1 :(得分:1)

将根classpath中的build.gradle4.1升级到4.2应该可以解决此问题,因为4.1的issue已在4.2中修复。

因此您的依赖项应如下所示。

dependencies {
   // other classpaths
    classpath 'com.google.gms:google-services:4.2.0'
}