Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FirebaseApp with name [DEFAULT] doesn't exist

After migrating to Firebase Cloud Messaging.When opening my app it crashes and throws an error saying java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't exist. I already put my new google-services.json and update my SDK.

Here's my MainActivity

public class MainActivity extends Activity {  @Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_main);  //Check Google play service     GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance();     int resultCode = googleAPI.isGooglePlayServicesAvailable(this);      if (resultCode != ConnectionResult.SUCCESS) {         if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {             GooglePlayServicesUtil.getErrorDialog(resultCode, this,                     PLAY_SERVICES_RESOLUTION_REQUEST).show();         } else {             Log.e(LOG_TAG, "This device is not supported.");             finish();         }     }      Log.i(TAG, "InstanceID token: " + FirebaseInstanceId.getInstance().getToken());  } } 
like image 518
natsumiyu Avatar asked May 20 '16 09:05

natsumiyu


2 Answers

Please do double check, you added

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

at the bottom of app's gradle file and then clean and rebuild the project

like image 81
Venkatesan Avatar answered Sep 17 '22 13:09

Venkatesan


Not sure, if it is relevant here. But there is another scenario when this crash can happen.


If your app has a service (with different process) and you're creating your own Application class, the service and the foreground app will use the same Application class (not same instance) to initialize. Now when I am using com.google.firebase:firebase-crash dependancy to handle crashes, it creates a background service your.app.packagename:background_crash. For some reason, this was inducing crashes on my app. Specifically, because in my Application class, I was making a call like,

FirebaseDatabase.getInstance().setPersistenceEnabled(true); 

I am assuming, the background service when initing with our Application class, somehow Firebase is not initialized. To fix this, I did

if (!FirebaseApp.getApps(this).isEmpty())         FirebaseDatabase.getInstance().setPersistenceEnabled(true); 
like image 27
Codevalley Avatar answered Sep 21 '22 13:09

Codevalley