在这个进程com.example.app中,我们看到了一些异常消息Default FirebaseApp is not initialized。确保首先调用FirebaseApp.initializeApp(Context)。在我们的Android应用中,我们刚刚添加了Firebase远程配置。

堆栈跟踪如下:

Fatal Exception: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.example.app. Make sure to call FirebaseApp.initializeApp(Context) first.
       at com.google.firebase.FirebaseApp.getInstance(Unknown Source)
       at com.google.firebase.remoteconfig.FirebaseRemoteConfig.getInstance(Unknown Source)
       at com.example.app.fragments.SomeFragment.updateFooter(SourceFile:295)
       at com.example.app.fragments.SomeFragment.onCreateView(SourceFile:205)
       at android.support.v4.app.Fragment.performCreateView(SourceFile:2080)
       at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1108)
       at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1290)
       at android.support.v4.app.BackStackRecord.run(SourceFile:801)
       at android.support.v4.app.FragmentManagerImpl.execSingleAction(SourceFile:1638)
       at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(SourceFile:679)
       at android.support.v4.app.FragmentPagerAdapter.finishUpdate(SourceFile:143)
       at android.support.v4.view.ViewPager.populate(SourceFile:1240)
       at android.support.v4.view.ViewPager.populate(SourceFile:1088)
       at android.support.v4.view.ViewPager.setAdapter(SourceFile:542)
       at com.example.app.SomeActivity.onSomeAsyncCallback(SourceFile:908)
       at com.example.app.SomeDataRetriever.onAsyncHttpCompleted(SourceFile:72)
       at com.example.app.io.AsyncHttp.onPostExecute(SourceFile:141)
       at com.example.app.io.AsyncHttp.onPostExecute(SourceFile:19)
       at android.os.AsyncTask.finish(AsyncTask.java:679)
       at android.os.AsyncTask.access$500(AsyncTask.java:180)
       at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:696)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:150)
       at android.app.ActivityThread.main(ActivityThread.java:5665)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:689)

这是9.6.1版本,我们还使用了其他Firebase组件:

compile 'com.google.firebase:firebase-ads:9.6.1'
compile 'com.google.firebase:firebase-config:9.6.1'
compile 'com.google.firebase:firebase-invites:9.6.1'
compile "com.google.firebase:firebase-messaging:9.6.1"

正如我从文档和Javadoc中看到的,在我们的例子中,我们不应该做任何手动初始化。

这个异常发生在Android 4-6上的各种设备上。

编辑:

我发现这个问题引起了一些关注。我想这个解释对你们中的一些人来说可能很有趣:https://firebase.googleblog.com/2016/12/how-does-firebase-initialize-on-android.html


当前回答

正如@PSIXO在评论中提到的,这可能是google-services依赖版本的问题。为了我的改变,

buildscript {
    // ...
    dependencies {
        // ...
        classpath 'com.google.gms:google-services:4.1.0'
    }
}

to

buildscript {
    // ...
    dependencies {
        // ...
        classpath 'com.google.gms:google-services:4.0.1'
    }
}

工作。4.1.0版本可能有一些问题。因为我在这个问题上浪费了很多时间,所以我想把这个作为一个答案。

其他回答

通过遵循@Gabriel Lidenor的回答,在我的情况下,初始化应用程序与上下文不工作。如果您试图创建没有google-service的firebase-app。json吗?所以在初始化任何数量的firebase应用程序之前,首先需要初始化为;

FirebaseOptions options = new FirebaseOptions.Builder().setApplicationId("APP_ID")
                    .setGcmSenderId("SENDER_ID").build();
FirebaseApp.initializeApp(context, options, "[DEFAULT]");

虽然手动初始化Firebase使用FirebaseApp.initializeApp(this);使错误消失,但不能修复根本原因,一些奇怪的问题聚集在一起似乎没有得到解决,例如

FCM需要com.google.android.c2dm.permission。接受许可 只适用于GCM 令牌在发送第一个通知后变为未注册 message not received/ onmessagerreceived()从未被调用,

使用更新的Gradle插件(例如Android插件2.2.3和Gradle 2.14.1)修复了所有问题。(当然设置必须是正确的,根据Firebase文档)

我前段时间也有同样的问题。

您正在尝试获得一个Firebase实例而不初始化它。 请在您的main函数或FutureBuilder中尝试获取Firebase实例之前添加这行代码:

FirebaseApp.initializeApp();

更新(2021年11月11日):


我们不需要调用FirebaseApp.initializeApp(this);手动在任何地方。我们也不应该这样做。

可能性1:

看起来com.google.gms:google-services:4.3.9有问题。

我们可以把它升级到

classpath 'com.google.gms:google-services:4.3.10'

或降级为

classpath 'com.google.gms:google-services:4.3.8'

dependencies {
    classpath 'com.android.tools.build:gradle:4.2.2'
    classpath 'com.google.gms:google-services:4.3.9'// <-- this was the problem
}

可能性2:

确保在app/build中添加以下行。gradle文件

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

然后清理项目并再次运行。这对我很管用。

可能性3:

我只是遇到了同样的问题,得到了一个意想不到的奇怪的解决方案。

从这个答案可以看出:

我已经删除了tools:node="replace",它就像魅力一样工作。

发生的原因这里是com.google。gms:谷歌服务版本。当我使用4.1.0时,我遇到了同样的错误。然后我把版本降级。 之前

classpath 'com.android.tools.build:gradle:3.3.0'
classpath 'com.google.gms:google-services:4.1.0'

classpath 'com.android.tools.build:gradle:3.3.0'
classpath 'com.google.gms:google-services:3.2.0'

希望它能解决这个错误。