19

I have been getting this issue.. followed the upgrade guide for new firebase sdk...saved the google services json file in app directory.. still the same error as you but for the database...

Caused by: java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't exist.
Rajiv Singh
  • 233
  • 1
  • 2
  • 8
  • Did you create the firebase project with your package name and download the google-services.json ? – Gabriele Mariotti May 20 '16 at 12:30
  • Yes I did.. infact my app without setting diskpersistence works correctly. If I try to enable dispersistence in my Application class as mentioned in the docs here... https://firebase.google.com/support/guides/firebase-android#get_a_database_reference_numbered ... my code fails – Rajiv Singh May 20 '16 at 13:25
  • Known issue: https://firebase.google.com/docs/crash/android#known-issues – Jared Burrows May 31 '16 at 04:08

3 Answers3

32

Are you using Firebase Crash Reporting? You might be hitting this error because of that if its hitting a background process and not the main.

Crash Reporting creates a second process (background_crash) to send crashes. Unfortunately, all processes in an Android app share a common Application subclass, so your onCreate method is run in the background process as well. That tries to initialise database, which fails.

The fix is to make sure the Database call is only run when Firebase is properly configured (which will be in the main process). You can check like this:

@Override
public void onCreate() {
  super.onCreate();
  if (!FirebaseApp.getApps(this).isEmpty()) {
    FirebaseDatabase.getInstance().setPersistenceEnabled(true);
  }
}
Ian Barber
  • 19,765
  • 3
  • 58
  • 58
  • I have tried using this solution, but the code inside braces is never executed. Cloud you give me a hint to make it work, please? – Євген Гарастович Jul 04 '16 at 10:28
  • Great.. I got this issue..only after adding firebase crash reporting lib . Thanks @Ian Barber – John Sep 16 '16 at 13:28
  • I am not using FirebaseDatabase, only Firebase Crash Reporting. As I have a different process, the problem resembles with your description. I am calling FirebaseCrash.report statically every time I send a report back to the server. Whenever I declare service as a process, it crashes. How do I solve this ? – Aveek Jul 28 '17 at 05:19
4

I solved this error by don't put anything of Firebase in Application. I put it in to MainActivity. Example: MainActivity.java

@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FirebaseDatabase.getInstance().setPersistenceEnabled(true);
    ...
}

-UPDATE

Other solution is create a DatabaseHelper class contain one FirebaseDatabase instance.

public class DatabaseHelper {
  private static boolean persistenceEnable = false;
  private static FirebaseDatabase mDatabase;


  public static boolean isPersistenceEnable(){
    return persistenceEnable;
  }
  public static FirebaseDatabase getInstance() {
    if (mDatabase == null) {
      mDatabase = FirebaseDatabase.getInstance();
      if(persistenceEnable==true) {
        mDatabase.setPersistenceEnabled(true);
      }
    }

    return mDatabase;
  }
}

and using by: FirebaseDatabase database = DatabaseHelper.getInstance();

lee
  • 161
  • 2
  • 5
0

Extending @Ian Barber's solution, you can try this generic check to skip processing your custom Application.onCreate for all non-main processes. If the additional processes don't belong to you, then you don't want any of your custom to run.

@Override
public void onCreate() {
    super.onCreate();
    if (FirebaseApp.getApps(this).isEmpty()) {
        // No firebase apps; we are in a non-main process
        // skip custom Application.onCreate
        return;
    }
    // Firebase init only in the main process
    FirebaseDatabase.getInstance().setPersistenceEnabled(true);
    // other code
}
Nizam Mohideen
  • 863
  • 10
  • 24