0

I created a new project and added firebase via android studio tool of firebase. Did everything as instructed but I am getting this error during lunching the app.

Process: com.chitchat, PID: 20084
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.chitchat/com.chitchat.MainActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.chitchat. Make sure to call FirebaseApp.initializeApp(Context) first.
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2951)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3086)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6718)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.chitchat. Make sure to call FirebaseApp.initializeApp(Context) first.
    at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@16.0.1:219)
    at com.google.firebase.auth.FirebaseAuth.getInstance(Unknown Source:1)
    at com.chitchat.MainActivity.onCreate(MainActivity.java:22)
    at android.app.Activity.performCreate(Activity.java:7144)
    at android.app.Activity.performCreate(Activity.java:7135)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2931)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3086) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:6718) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

MainActivity.java is

public class MainActivity extends AppCompatActivity {

private FirebaseAuth mAuth;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    FirebaseApp.initializeApp(MainActivity.this);
    mAuth = FirebaseAuth.getInstance();
}

@Override
public void onStart() {
    super.onStart();
    // Check if user is signed in (non-null) and update UI accordingly.
    FirebaseUser currentUser = mAuth.getCurrentUser();
    //updateUI(currentUser);

    if(currentUser == null){
        Intent startAct = new Intent(MainActivity.this, Start_Activity.class);
        startActivity(startAct);
        finish();
    }
}
}

and gradle.build is

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

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.chitchat"
        minSdkVersion 23
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.google.firebase:firebase-auth:16.0.3'
    implementation 'com.google.firebase:firebase-core:16.0.3'
    //implementation 'com.google.android.gms:play-services-base'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'


}

Project-level gradle.build

    buildscript {
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.0'
        classpath 'com.google.gms:google-services:4.1.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()

    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Couldn't find anything on web! Have added firebaseApp.initializeApp(this) which was not present in any instructions but still the same error on emulator and on device.

1
  • is it possible that i have misconfig firebase which is causing this error!? Commented Jan 23, 2019 at 13:13

4 Answers 4

6

I had this issue too.I saw many people were having the issue in recent weeks.There is issue with

        classpath 'com.android.tools.build:gradle:3.3.0'

change it to

        classpath 'com.android.tools.build:gradle:3.2.0'

as mentioned here!

because AGP is having some issues according to this discussion here! I think this will solve your issue.

EDIT: As of 30th jan 2021, just change to latest version of all firebase class dependencies and it should work: classpath 'com.google.gms:google-services:4.3.4'

Sign up to request clarification or add additional context in comments.

1 Comment

I had updated google-services to 4.3.9 and experienced the problem. Downgrading to 4.3.4 got around the issue.
2

Add this to bottom of your build.gradle:

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

4 Comments

Post your project-level build.gradle
@AjayChoudhary Try this: FirebaseApp.initializeApp(getApplicationContext())
tried this FirebaseApp.initializeApp(getApplicationContext()) : no luck!
Updated project-level build.gradle
2

Add following code in your Application class instead of Activity, in order to initialize Firebase into entire application, not just one Activity

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

Also, you need to move your apply plugin code from top to bottom Referencing: Add Firebase SDK

// ADD THIS AT THE BOTTOM
apply plugin: 'com.google.gms.google-services'

It requires that the apply plugin: 'com.google.gms.google-services' line be at the bottom of your app/build.gradle file so that no dependency collisions are introduced. You can see the result of this step by running ./gradlew :app:dependencies. Reference

Also, try syncing all firebase and gms:google-services dependencies to the latest version.

2 Comments

I created a new java class, extends Application. Created entry in manifest file. Used your code in it. Still getting the same error! Please let me know if i misunderstood anything!
For those using kotlin DSL see this answer
1

First thing you need to add com.google.gms:google-services:x.x.x at root level build.gradle. Like this

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

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}

then add google plugin at the bottom of your build.gradle(app).

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.google.firebase:firebase-auth:16.0.3'
    implementation 'com.google.firebase:firebase-core:16.0.3'
    //implementation 'com.google.android.gms:play-services-base'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'


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

1 Comment

'com.google.gms:google-services:4.1.0' is already present. I added 'apply plugin' but this didn't help. Same error!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.