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

其他回答

通过Android Studio Tools安装Firebase…

我通过Android Studio的内置工具进行安装(遵循Firebase的最新文档)。 这安装了基本的依赖项,但当我试图连接到数据库时,它总是给我一个错误,我需要首先调用初始化,即使我是:

默认的FirebaseApp在此过程中没有初始化。一定要 首先调用FirebaseApp.initializeApp(Context)。

无论我做什么,都会得到这个错误。

最后,在看到其他答案中的一个评论后,我把我的gradle从4.1.0版本更改为:

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

当我这样做的时候,我终于看到了一个帮助我的错误:

File google-services.json is missing. The Google Services Plugin cannot function without it. Searched Location: C:\Users\%username%\AndroidStudioProjects\TxtFwd\app\src\nullnull\debug\google-services.json C:\Users\%username%\AndroidStudioProjects\TxtFwd\app\src\debug\nullnull\google-services.json C:\Users\%username%\AndroidStudioProjects\TxtFwd\app\src\nullnull\google-services.json C:\Users\%username%\AndroidStudioProjects\TxtFwd\app\src\debug\google-services.json C:\Users\%username%\AndroidStudioProjects\TxtFwd\app\src\nullnullDebug\google-services.json C:\Users\%username%\AndroidStudioProjects\TxtFwd\app\google-services.json

这就是问题所在。由于某些原因,4.1.0版本似乎没有给出这个构建错误——没有提到你有一个丢失的google服务。json文件。我没有谷歌的服务。json文件,所以我出去添加了它。

但由于这是一个升级,使用现有的实时first base数据库,我从来没有生成该文件在过去。 我去firebase生成并添加了它,它解决了这个问题。

更改回4.1.0

一旦我发现了所有这些,然后我把类路径变量改回来(到4.1.0),重新构建,它再次崩溃,错误是它没有被初始化。

根源问题

在预编译时,使用4.1.0构建不会为您提供有效的错误,因此您可能不知道发生了什么。 在4.1.0上运行会导致初始化错误。

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

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

FirebaseApp.initializeApp();

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

你需要在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

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