【问题标题】:Firebase Crash Reporting : FirebaseApp with name [DEFAULT] doesn't existFirebase 崩溃报告:名称为 [DEFAULT] 的 FirebaseApp 不存在
【发布时间】:2016-07-14 03:59:20
【问题描述】:

这是我在尝试使用 FirebaseAuth 和 Firebase 崩溃报告时遇到的异常。我正在尝试在我的应用程序中匿名登录 onCreate

public class MainApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
        firebaseAuth.signInAnonymously().addOnCompleteListener(task -> {
            if (task.isSuccessful()) {
                Log.wtf("TAG", "Signed signInAnonymously successful ");
            }
        });
    }
}

我还想使用崩溃报告,所以我的 gradle 依赖项看起来像这样。

dependencies {
    // other dependencies
    // ...

    compile 'com.google.firebase:firebase-core:9.2.1'
    compile 'com.google.firebase:firebase-storage:9.2.1'
    compile 'com.google.firebase:firebase-auth:9.2.1'
    compile 'com.google.firebase:firebase-config:9.2.1'
    compile 'com.google.firebase:firebase-crash:9.2.1'
}

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

一段时间后,我会收到一个崩溃对话框和这个异常。它是另一个崩溃的进程,因此应用程序本身仍将运行。

java.lang.RuntimeException: Unable to create application com.android.tools.fd.runtime.BootstrapApplication: java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't exist. 
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4612)
    at android.app.ActivityThread.access$1600(ActivityThread.java:169)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1337)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5476)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't exist. 
    at com.google.firebase.FirebaseApp.getInstance(Unknown Source)
    at com.google.firebase.FirebaseApp.getInstance(Unknown Source)
    at com.google.firebase.auth.FirebaseAuth.getInstance(Unknown Source)
    at com.redstar.collectors.MainApplication.onCreate(MainApplication.java:22)
    at com.android.tools.fd.runtime.BootstrapApplication.onCreate(BootstrapApplication.java:369)
    at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1013)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4609)
    at android.app.ActivityThread.access$1600(ActivityThread.java:169) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1337) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:136) 
    at android.app.ActivityThread.main(ActivityThread.java:5476) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 
    at dalvik.system.NativeStart.main(Native Method)

如果我删除 firebase-crash 依赖项,就不会发生崩溃。

【问题讨论】:

  • 这是您的初始化代码和 Firebase 中的一些自动初始化代码之间的竞争条件。您需要将初始化移到您的主要活动中(稍后运行),或者最好移到内容提供者中(较早运行的位置)。

标签: android firebase firebase-authentication firebase-crash-reporting


【解决方案1】:

FirebaseApp with name [DEFAULT] doesn't exist 是在您在由 Firebase 崩溃报告创建的单独进程上运行 Firebase 代码时引起的,因为您的自定义 Application 类是为您应用中的每个进程创建的。

您应该将代码从您的Application 类移动到其他地方。这可能在您的主要活动中(始终在您的主流程中),也可能在您在清单中注册的单独 ContentProvideronCreate() 中(ContentProvideronCreate() 在应用进程的生命周期,就像Application.onCreate(),但只在单个进程中运行。

【讨论】:

  • 感谢您的回答。将代码放入我的主要活动中的一个问题是我的主要活动不能保证每次都会启动。例如,如果我从共享意图启动,我将不会打开我的主要活动。所以我可能有重复的代码或基本活动,我宁愿没有。内容提供商似乎有点奇怪,因为我实际上不会提供任何内容。也许注射是要走的路?
  • 是的,完全同意 ContentProvider 方法的奇怪感觉。您绝对可以将代码作为类中的静态方法,并从需要确保其准备就绪的每个组件中调用它 - 如果您已经完成设置,则静态布尔值将确保每次只完成一次进程初始化。
  • 好吧,也许这是最好的方法。谢谢。另一方面,真的有必要在单独的进程中运行 Firebase 崩溃报告吗?
  • @Modge - 这是当前版本的要求,尽管我似乎记得在 Firebase forum 上看到他们希望删除该要求。
猜你喜欢
  • 2016-12-07
  • 2019-07-09
  • 2016-09-17
  • 2016-12-03
  • 2019-12-04
  • 2021-10-17
  • 2017-04-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多