Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase + RoboElectric: java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't exist

I am using Firebase in our android application for remote config. Below is the initialization done in my application class onCreate method:

    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);

As per this answer my build.gradle file also has the following line in it:

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

When I run the application it works fine but when I try to run my Roboelectric test case it crashes with following exception:

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.remoteconfig.FirebaseRemoteConfig.getInstance(Unknown Source) at com.woi.apppackage.android.MyApplication.initFirebase(MyApplication.java:225) at com.woi.apppackage.android.MyApplication.onCreate(MyApplication.java:107) at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:140) at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:433) at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:240) at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188) at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234) at 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) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

The test has nothing to do with Firebase. If I comment out the Firebase code in onCreate the test runs fine. I think I am missing something in my initialization of Firebase

like image 251
Abdullah Avatar asked Nov 09 '22 11:11

Abdullah


1 Answers

This may be caused by Firebase Crash. The current implementation of it creates a process called background_crash. An instance of your Application class is created for each process of your app, including background_crash. This is described in the documentation.

Try adding this code to only initialize Remote Config when in your main process:

    if (FirebaseApp.getApps(this).isEmpty()) {
        // In the crash process
    } else {
        // Not in crash process
        // Init Remote Config here
    }
like image 168
Bob Snyder Avatar answered Nov 14 '22 21:11

Bob Snyder