85

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

}
}
natsumiyu
  • 3,217
  • 7
  • 30
  • 54

14 Answers14

115

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

Venkatesan
  • 1,174
  • 1
  • 8
  • 3
  • I removed the `apply plugin: 'com.google.gms.google-services'` from my app gradle but encounter a new error when calling `FirebaseInstanceId.getInstance().getToken()` in my `MainActivity` my app crashes. – natsumiyu May 23 '16 at 00:57
  • 1
    my bad... i needed to add the `apply plugin: 'com.google.gms.google-services' ` ? trying to add it i still encounter error. it says `Didn't find class "com.google.android.gms.dynamite.descriptors.com.google.firebase.auth.ModuleDescriptor"` – natsumiyu May 23 '16 at 02:17
  • Hi Mori, you can ignore this error as of now and they will fix this issue in the next release. thats what firebase team says :) . – Venkatesan May 25 '16 at 07:35
  • When I add this I get: "Error:Execution failed for task ':app:mergeModernDebugResources'. > [string/google_api_key] //app/src/main/res/values/defaults.xml [string/google_api_key] //app/build/generated/res/google-services/modern/debug/values/values.xml: Error: Duplicate resources" I also did not see this step in the setup or migration guides for FCM. Do you have a link I may have missed? – C Nick Jun 14 '16 at 14:48
  • 1
    Flutter may require `flutter clean & flutter run` as suggested in the answer as well. – Nae Dec 25 '18 at 14:34
  • 1
    What if I use Firebase Java Admin? (Java Backend and not Android client app) – user924 May 30 '19 at 19:59
21

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);
Codevalley
  • 4,593
  • 7
  • 42
  • 56
  • This also happens with ads. If a user clicks on an ad then returns to the app Firebase with no longer be available. – theJosh Sep 29 '16 at 22:31
  • @Codevalley, In my case, there was another process (Adobe Creative SDK Image Editor) called app:editor. It was using same application class, thus trying to get token from non-existing firebase app. Checking before getting token solved my problem. Thank you so much! :) – rzaaeeff Jul 26 '17 at 11:57
12

I've had similar problem, and for me it was a bug/problem with manifest merger. I've found out that FirebaseInitProvider has not been injected into final manifest file because of tools:node="replace" in my app's manifest file. So, try to remove this xml tag and FirebaseInitProvider will be injected and Firebase can be initialized properly.

jmodrako
  • 334
  • 2
  • 6
11

build.gradle file:

buildscript {
    repositories {
        jcenter()
        mavenLocal()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'
        classpath 'com.google.gms:google-services:3.0.0'
    }
}

allprojects {
    repositories {
        jcenter()
        mavenLocal()
    }
}

\app\build.gradle file:

apply plugin: 'com.android.application'

android {
    ..
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    ..
    compile 'com.google.firebase:firebase-core:9.0.2'
    compile 'com.google.firebase:firebase-messaging:9.0.2'
}

apply plugin: 'com.google.gms.google-services'
Elron
  • 511
  • 4
  • 14
10

@jmodrako answer solved my problem... tools:node="replace" to tools:node="merge"

Explained... on AndroidManifest.xml

From

<application
...
tools:node="replace">

To

<application
...
tools:node="merge">

Merge problems with library themes? Build problems solved using tools:replace="android:theme"

Credits to https://stackoverflow.com/a/38060272/2765087

Community
  • 1
  • 1
2

Move your firebase initialization inside the onCreate of Application class. Also if you have enabled offline persistence, FirebaseDatabase.getInstance().setPersistenceEnabled(true) should come before any other initializations.

Ramya Ramesh
  • 299
  • 3
  • 18
2

Verify all configurations as below:

1-firebase project setting: google-services.json is correct? enter image description here

2- add firebase SDK enter image description here

3- clean - rebuild your project

hopefully, this helps!

Mr Special
  • 1,576
  • 1
  • 20
  • 33
2

Add the following line to app/build.gradle

apply plugin: 'com.google.gms.google-services'  // Google Services plugin

And the following line to project build.gradle

classpath 'com.google.gms:google-services:4.3.3'
user1188867
  • 3,726
  • 5
  • 43
  • 69
0

Android Studio

  1. apply plugin: 'com.google.gms.google-services' (build.gradle - Module layer)
  2. Menu~>Build~>CleanProject

Works for me ok.

Maxim Firsoff
  • 2,038
  • 22
  • 28
0

In your dependency just add:

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
...
compile 'com.google.firebase:firebase-core:9.0.2'
compile 'com.google.firebase:firebase-messaging:9.0.2'
}

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

Biswajit Roy
  • 508
  • 2
  • 7
  • 19
pavel
  • 1,603
  • 22
  • 19
0

Register your application in Firebase and copy the google-services.json to your root project.

Apply classpath 'com.google.gms:google-services:3.1.0 to you root build.gradle.

Apply apply plugin: 'com.google.gms.google-services to your project gradle.

pRaNaY
  • 24,642
  • 24
  • 96
  • 146
0

Change the Build Action (GoogleServicesJson) to the File Name Google-Services.Json.

0

In my case I was not initializing Firebase on app startup, I had to do the following to resolve it

@Service
public class FirebaseSetup implements CommandLineRunner {
    public void run(String... args) throws Exception {
        initializeFirebase();
    }
    private void initializeFirebase() throws FileNotFoundException, IOException {
        FileInputStream serviceAccount = new FileInputStream(ResourceUtils.getFile("classpath:ssf1-v1-firebase-adminsdk-zr72u-afcb5bc13b.json"));
        FirebaseOptions options = new FirebaseOptions.Builder().setCredentials(GoogleCredentials.fromStream(serviceAccount)).build();
        FirebaseApp.initializeApp(options);
    }
}
Anand Rockzz
  • 6,072
  • 5
  • 64
  • 71
0

Add classpath 'com.google.gms:google-services:3.0.0' to build.gradle

dependencies {
        ..
        classpath 'com.google.gms:google-services:3.0.0'
    }

Add this at the end of the app\build.gradle

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

This worked for me

User
  • 1,460
  • 14
  • 11