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


当前回答

我们必须在应用程序类的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">

其他回答

在我的情况下,这个错误发生时,我升级我的谷歌gms类路径。 我

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

所以我不得不降级为:

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

它运行得很好。

虽然手动初始化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文档)

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">

发生的原因这里是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'

希望它能解决这个错误。