【问题标题】:Firebase + RoboElectric: java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't existFirebase + RoboElectric:java.lang.IllegalStateException:名称为 [DEFAULT] 的 FirebaseApp 不存在
【发布时间】:2016-12-07 09:28:06
【问题描述】:

我在我们的 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) 在 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) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:498) 在 com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

该测试与 Firebase 无关。如果我注释掉 onCreate 中的 Firebase 代码,测试运行良好。我想我在 Firebase 的初始化中遗漏了一些东西

【问题讨论】:

    标签: android firebase robolectric


    【解决方案1】:

    这可能是由 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
        }
    

    【讨论】:

      【解决方案2】:

      我认为在应用程序级别调用 Firebase 存在错误。我对 Firebase 数据库有同样的问题,并通过在我的活动类中移动我的 Firebase 代码来解决它(在我的例子中是 SplashScreen)。

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

      【讨论】:

        【解决方案3】:

        你可以的

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

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

        【讨论】:

          猜你喜欢
          • 2019-07-09
          • 2016-09-17
          • 2016-12-03
          • 1970-01-01
          • 2019-12-04
          • 2021-10-17
          • 2017-04-25
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多