Android Firebase+;RoboElectric:java.lang.IllegalStateException:FirebaseApp(名称为[DEFAULT]不';不存在
我正在android应用程序中使用Firebase进行远程配置。下面是在我的应用程序类onCreate方法中完成的初始化:Android Firebase+;RoboElectric:java.lang.IllegalStateException:FirebaseApp(名称为[DEFAULT]不';不存在,android,firebase,robolectric,Android,Firebase,Robolectric,我正在android应用程序中使用Firebase进行远程配置。下面是在我的应用程序类onCreate方法中完成的初始化: FirebaseRemoteConfig remoteConfig = FirebaseRemoteConfig.getInstance(); FirebaseRemoteConfigSettings settings = new FirebaseRemoteConfigSettings.Builder() .setDevel
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);
当我运行应用程序时,它工作正常,但当我尝试运行Roboelectric测试用例时,它崩溃,出现以下异常:
java.lang.IllegalStateException:名为[DEFAULT]的FirebaseApp不存在。
位于com.google.firebase.FirebaseApp.getInstance(未知源)
位于com.google.firebase.FirebaseApp.getInstance(未知源)
位于com.google.firebase.remoteconfig.firebasemoteconfig.getInstance(未知源)
位于com.woi.apppackage.android.MyApplication.initFirebase(MyApplication.java:225)
位于com.woi.apppackage.android.MyApplication.onCreate(MyApplication.java:107)
位于org.roblectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:140)
位于org.roblectric.roblectrictTestRunner.setUpApplicationState(roblectrictTestRunner.java:433)
位于org.roblectric.roblectrictTestRunner$2.evaluate(roblectrictTestRunner.java:240)
位于org.roblectric.roblectrictTestRunner.runChild(roblectrictTestRunner.java:188)
位于org.roblectric.roblectrictTestRunner.runChild(roblectrictTestRunner.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.roblectric.roblectrictTestRunner$1.evaluate(roblectrictTestRunner.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(本机方法)处
位于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时遗漏了一些东西我认为Firebase有一个bug,它在应用程序级别被调用。我对Firebase数据库也有同样的问题,通过在我的活动类中移动我的Firebase代码(在我的例子中是SplashScreen)解决了这个问题
我建议你也这样做。:) 这可能是由Firebase崩溃引起的。它的当前实现创建了一个名为
background\u crash
的进程。将为应用程序的每个进程创建一个应用程序
类的实例,包括background\u crash
。这在中进行了描述
尝试添加此代码以仅在主进程中初始化远程配置:
if (FirebaseApp.getApps(this).isEmpty()) {
// In the crash process
} else {
// Not in crash process
// Init Remote Config here
}
你能行
if (FirebaseApp.getApps(context).isEmpty()) {
FirebaseApp.initializeApp(context);
}
在初始化firebase之前。如果Firebase未正确初始化(可能是由于内部异常),则可能发生这种情况
if (FirebaseApp.getApps(context).isEmpty()) {
FirebaseApp.initializeApp(context);
}