'IllegalStateException: Default FirebaseApp is not initialized in this process. Make sure to call FirebaseApp.initializeApp(Context) first

I've been getting this fatal error today after changing some gradle dependences. Even though I tried to revert the gradle dependences back to the original I'm still getting the error on this line:

class MainActivity : AppCompatActivity() {

    val db = FirebaseFirestore.getInstance() // IllegalStateException

error:

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.app/com.app.MainActivity}: 

java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.app. Make sure to call FirebaseApp.initializeApp(Context) first.

 Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.app. Make sure to call FirebaseApp.initializeApp(Context) first.
    at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@17.1.0:186)
    at com.google.firebase.firestore.FirebaseFirestore.getInstance(com.google.firebase:firebase-firestore@@20.1.0:70)
    at com.app.MainActivity.<init>(MainActivity.kt:43)

gradle (app)

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

dependencies {

// Firebase
implementation 'com.google.firebase:firebase-auth:18.0.0'
implementation 'com.firebaseui:firebase-ui-auth:4.3.1'
implementation 'com.google.firebase:firebase-firestore:20.1.0'
implementation 'com.google.firebase:firebase-storage:18.0.0'
implementation 'com.firebaseui:firebase-ui-storage:4.3.2'
implementation 'com.google.firebase:firebase-messaging:19.0.1'
implementation 'com.google.firebase:firebase-functions:17.0.0'

}

gradle (Project)

buildscript {
    ext.kotlin_version = '1.3.21'
    repositories {
        google()
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.2.0'
    }
}

According to multiple answers 1 2 the problem is related to the google-services dependency - which i have changed to the recommended version 4.2.0 - but I'm still getting the same error.

Any idea how I can fix it?

Edit:

This is a duplicate because the proposed answer FirebaseApp.initializeApp(this) did not solve the issue.

EDIT 2: There is no error on my emulator, only my device



Solution 1:[1]

I tried setting up Firebase using Android Studio(3.5) assistant.

Here is the recommended dependencies they showed me to inject

classpath 'com.google.gms:google-services:4.2.0'

In app build.gradle

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

implementation 'com.google.firebase:firebase-auth:16.0.4'
implementation 'com.google.firebase:firebase-core:16.0.4'
implementation 'com.google.firebase:firebase-messaging:16.0.4'
implementation 'com.google.firebase:firebase-database:16.0.4'
implementation 'com.google.firebase:firebase-storage:16.0.4'
implementation 'com.google.firebase:firebase-functions:16.0.4'

You don't need to add all these dependencies, just add the required ones only. Otherwise the APK will get larger.

Solution 2:[2]

I had this error when the Package name in the Manifest was different from "package_name" in google-services.json.

After i fixed the package name, I had to uninstall the application and clean up the build.

In Xamarine: You don't need to call

FirebaseApp.initializeApp(Context);

You can directly call

await FirebaseMessaging.Instance.SubscribeToTopic(topic);

or

Java.Lang.Object token = await FirebaseMessaging.Instance.GetToken();

It works like a charm. :-)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Martin4code