Firebase + RoboElectric:java.lang.IllegalStateException:名称为[DEFAULT]的FirebaseApp不存在

时间:2016-08-02 06:44:15

标签: android firebase robolectric

我在Android应用程序中使用 Firebase 进行远程配置。以下是我的应用类 onCreate 方法中的初始化

    FirebaseRemoteConfig remoteConfig = FirebaseRemoteConfig.getInstance();
    FirebaseRemoteConfigSettings settings = new FirebaseRemoteConfigSettings.Builder()
                .setDeveloperModeEnabled(BuildConfig.DEBUG)
                .build();
    remoteConfig.setConfigSettings(settings);
    setFirebaseDefault();

    Map<String, Object> defaults = new HashMap<>();
    defaults.put(FirebaseConstantsKt.BREAKING_NEWS, "");
    defaults.put(FirebaseConstantsKt.ENABLE_BREAKING_NEWS, false);
    FirebaseRemoteConfig.getInstance().setDefaults(defaults);

根据this回答我的build.gradle文件中还包含以下行:

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

当我运行应用程序时,它运行正常,但是当我尝试运行我的Roboelectric测试用例时,它会崩溃并出现以下异常:

  

java.lang.IllegalStateException:名称为[DEFAULT]的FirebaseApp不存在。               在com.google.firebase.FirebaseApp.getInstance(未知来源)               在com.google.firebase.FirebaseApp.getInstance(未知来源)               在com.google.firebase.remoteconfig.FirebaseRemoteConfig.getInstance(未知来源)               在com.woi.apppackage.android.MyApplication.initFirebase(MyApplication.java:225)               在com.woi.apppackage.android.MyApplication.onCreate(MyApplication.java:107)               在org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:140)               在org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:433)               在org.robolectric.RobolectricTestRunner $ 2.evaluate(RobolectricTestRunner.java:240)               在org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)               在org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)               在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)               在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)               在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)               在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)               在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)               在org.robolectric.RobolectricTestRunner $ 1.evaluate(RobolectricTestRunner.java:152)               在org.junit.runners.ParentRunner.run(ParentRunner.java:363)               在org.junit.runner.JUnitCore.run(JUnitCore.java:137)               at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)               在com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)               在com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)               at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)               at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)               at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)               at java.lang.reflect.Method.invoke(Method.java:498)               在com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

该测试与Firebase无关。如果我在onCreate中注释掉Firebase代码,则测试运行正常。我想我在Firebase的初始化

中遗漏了一些东西

3 个答案:

答案 0 :(得分:2)

这可能是由Firebase崩溃引起的。它的当前实现创建了一个名为background_crash的过程。系统会为您的应用的每个流程创建Application课程的实例,包括background_crash。这在documentation

中有所描述

尝试添加此代码,仅在主进程中初始化远程配置:

    if (FirebaseApp.getApps(this).isEmpty()) {
        // In the crash process
    } else {
        // Not in crash process
        // Init Remote Config here
    }

答案 1 :(得分:0)

我认为Firebase存在一个在应用程序级别调用的错误。我在Firebase数据库中遇到了同样的问题,并通过在我的活动类中移动Firebase代码解决了问题(在我的案例中为SplashScreen)。

我建议尝试做同样的事情。 :)

答案 2 :(得分:0)

你可以做到

if (FirebaseApp.getApps(context).isEmpty()) {
        FirebaseApp.initializeApp(context);
}

在初始化firebase之前。如果Firebase未正确初始化(可能是因为内部异常),则会发生这种情况

相关问题