java.lang.IllegalStateException: FirebaseApp with name [DEFAULT]

12,220

Solution 1

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

Solution 2

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

Share:
12,220
Rajiv Singh
Author by

Rajiv Singh

Updated on June 20, 2022

Comments

  • Rajiv Singh
    Rajiv Singh about 2 years

    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.