Make sure to call FirebaseApp.initializeApp(Context) first in Android

In your SimpleBlog application class, initialize FirebaseApp in onCreate() method and remove it from RegisterActivity in order to have Firebase initialize into entire application, not just one Activity.

@Override
public void onCreate() {
    super.onCreate();
    FirebaseApp.initializeApp(this);
}

Also add apply plugin: 'com.google.gms.google-services' at the end of app gradle:

dependencies {
    ....
}

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

Plugin is required to process your json config from firebase and to avoid dependency collisions. You can read here for more details.


Spent literally a whole day. Finally, this was the solution.

Freaking 0 instead of 1 and you will not have to use the initialize firebaseapp or anything like that.

In Project gradle, use Google services: 4.0.0 and not 4.1.0.

Plus, apply plugin statement at the end is also not necessary in my experience.

(Hoping you have added firebase database from the tools => firebase assistant. Step 1 and step 2 are right and green. )


According to FirebaseApp documentation, you do not need to invoke this initialization, except that your app requires access to another Firebase project.

I invoked this initialization and sometime, user gets crash time to time when user opens my app, so I remove this line of code then everything works well. Be careful to invoke this initialization.

The capture of FirebaseApp documentation:

enter image description here

Update: below Exception will occur if you try to add this line [Some ads network requires this line, they also add some process in their AndroidManifest]

Failed to gain exclusive lock to the Firestore client's offline persistence. This generally means you are using Firestore from multiple processes in your app. Keep in mind that multi-process Android apps execute the code in your Application class in all processes, so you may need to avoid initializing Firestore in your Application class. If you are intentionally using Firestore from multiple processes, you can only enable offline persistence (i.e. call setPersistenceEnabled(true)) in one of them.

To fix it:

1) add below method in your Application class:

private boolean isMainProcess(Context context) {
    if (null == context) {
        return true;
    }
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

    int pid = android.os.Process.myPid();
    for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
        if (APPLICATION_ID.equals(processInfo.processName) && pid == processInfo.pid) {
            return true;
        }
    }
    return false;
}

2) Wrap onCreate of your Application

@Override
public void onCreate() {
    super.onCreate();
    if (!isMainProcess(this)) {
        FirebaseApp.initializeApp(this);
        FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
                .setPersistenceEnabled(false)
                .build();
        FirebaseFirestore.getInstance().setFirestoreSettings(settings);
        // other things
        return;
    }
    // other things
}

UPDATE: Sometime, my app throws below exception

Unable to create application My Application Class : java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process MY_APPLICATION_ID. Make sure to call FirebaseApp.initializeApp(Context) first.

To Fix It: let update onCreate method:

@Override
public void onCreate() {
    super.onCreate();
    FirebaseApp.initializeApp(this);
    boolean isMain = isMainProcess(this);
    FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder().setPersistenceEnabled(isMain).build();
    FirebaseFirestore.getInstance().setFirestoreSettings(settings);
    if (!isMain) {
        // other things
        return;
    }
    // other things
}