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

我们必须在应用程序类的onCreate函数中初始化Firebase。

 package com.rocks.music.videoplayer;

 import android.app.Application;
 import android.content.Context;

 import com.google.firebase.FirebaseApp;


/**
* Created by ashish123 on 22/8/15.
  */
 public class MyApplication extends Application {

private static MyApplication mInstance;

@Override
public void onCreate() {
    super.onCreate();
    mInstance = this;
    try {
        FirebaseApp.initializeApp(this);
    }
    catch (Exception e) {
    }
}

public static Context getInstance() {
    return mInstance;
}

}

清单文件中的代码:-

  <application
    android:name="com.rocks.music.videoplayer.MyApplication"
    android:allowBackup="true"
    android:icon="@drawable/app_icon"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

通过遵循@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]");

如果你最近将你的Android Studio更新到3.3.1,com.google。gms:google-services(低于4.2.0)依赖。Gms:google-services到4.2.0。

dependencies {
    classpath 'com.android.tools.build:gradle:3.3.1'
    classpath 'com.google.gms:google-services:4.2.0'
    }

对我来说,这是升级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
}