在这个进程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


当前回答

使用com.google.gms:google-services:4.0.1'而不是4.1.0

其他回答

确保添加到你的根级build.gradle

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

然后,在你的模块级Gradle文件(通常是app/build.gradle)中,在文件底部添加'apply plugin'行来启用Gradle插件:

apply plugin: 'com.android.application'

android {
  // ...
}

dependencies {
  // ...
  implementation 'com.google.firebase:firebase-core:9.6.1'
  // Getting a "Could not find" error? Make sure you have
  // the latest Google Repository in the Android SDK manager
}

// ADD THIS AT THE BOTTOM
apply plugin: 'com.google.gms.google-services'

如文件中所述。在上面的一个问题中,当我忘记在我的gradle文件中添加这个时,我有例外。

你需要在build中添加Firebase Gradle buildscript依赖。gradle(项目级)

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

在app/build.gradle中添加Firebase插件

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

build.gradle will include these new dependencies:
    compile 'com.google.firebase:firebase-database:11.0.4'

来源:Android Studio Assistant

我把项目gms:google_services降级为 类路径的com.google.gms:谷歌服务:4.0.1 这对我很管用。

对我来说,这是升级com.google的依赖关系。Gms:构建内部的google服务。gradle将

buildscript {
repositories {
    jcenter()
    mavenCentral()
    maven {
        url 'https://maven.google.com/'
        name 'Google'
    }
    google()
}
dependencies {
    classpath 'com.android.tools.build:gradle:3.3.2'
    classpath 'com.google.gms:google-services:4.2.0'

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}

After updating various dependencies I got a Crashlytics error in the compile, 'Crashlytics found an invalid API key: null. Check the Crashlytics plugin to make sure that the application has been added successfully! Contact support@fabric.io for assistance.' The one non-auto response I got from repeated attempts to support@fabric.io the error directs you to was that Fabric and Crashlytics are separate teams so they couldn't help me. I've avoided implementing the extra Fabric layer to Crashlytics, and was unable to get a new key from the Fabric site, or even get the site to recognize me. On attempting to work around this by just removing Crashlytics from my code, I got the 'Default FirebaseApp is not initialized in this process com.example.app. Make sure to call FirebaseApp.initializeApp(Context) first' crash in the run.

我从来没有添加'FirebaseApp.initializeApp(this)'的初始化行,事实上它被注释掉了。文档甚至提到,如果只在一个活动中使用Firebase,则不需要这样做。添加它没有什么不同,仍然得到了运行终止错误。

Turns out what was causing the new obscure errors was the updated google-services dependency. For now, I don't have time to spend more days trying to fix the shotgun errors the new dependency is causing, so until someone comes up with solutions I'll stick to the old version. Besides the odd initialization crash, the new version may be forcing Fabric on Crashlytics users. Users are being forced back to the old dependency version for this too: Crashlytics found an invalid API key: null. after updated com.google.gms:google-services:4.1.0

//com.google.gms:google-services:4.1.0 BAD
com.google.gms:google-services:4.0.1//GOOD

EDIT 10/17/18:再次更新以下依赖项后

implementation 'com.google.firebase:firebase-ads:17.0.0'
implementation 'com.google.firebase:firebase-auth:16.0.4'
implementation 'com.google.firebase:firebase-database:16.0.3'
implementation 'com.google.firebase:firebase-core:16.0.4

我在尝试安装时立即崩溃,提示“xxx意外关闭”,就像我尝试谷歌服务依赖更新时一样。在日志中我发现了一个链接,指示我将其添加到清单中

<meta-data
        android:name="com.google.android.gms.ads.APPLICATION_ID"
        android:value="ca-app-pub-xxxxxx~xxxxxx"/>

这是新的,在这里https://firebase.google.com/docs/android/setup和这里https://developers.google.com/admob/android/interstitial的设置和插页说明中没有提到。

我过去只需要为我的应用程序处理一个与广告相关的ID,即INTERSTITIAL_UNIT_ID。现在有两个问题需要解决。除了上述添加,文档指导在这里添加ADMOB_APP_ID(与ads.APPLICATION_ID在新清单代码中绑定的数字相同)

MobileAds.initialize(this, ADMOB_APP_ID);

INTERSTITIAL_UNIT_ID和ADMOB_APP_ID id可以在你的谷歌AdMob控制台中找到。我的游戏应用程序在我第一次更新firebase依赖时停止提供广告,仍然没有提供广告,在

public void onAdFailedToLoad(int errorCode){...

即使在所有这些增加的混乱之后,我仍然无法在没有初始化错误运行崩溃的情况下更新google-services依赖项。我希望在google服务:4.0.1上停留一段时间。

编辑10/24/18:mobileadssdk-advisor+support@google.com在更新后没有得到广告服务的几周通信后:

“谢谢你分享设备日志。从日志来看,这似乎是一个已经存在的问题,这是我们的优先事项,我们的团队正在努力修复,这只发生在Android O和P设备上。”

只有O和P设备?这是最后两个版本,O在2017年9月25日推出。呵。

更新8/8/21:在将google-services从4.3.8更新到4.3.9之后,我突然又遇到了将近3年前的这个问题,尽管确实调用了初始化(尽管据称不需要)。将再次不得不延迟更新实现:

//com.google.gms:google-services:4.3.9 BAD
com.google.gms:google-services:4.3.8//GOOD