FirebaseApp with name [DEFAULT] doesn't exist

2019-01-04 12:48发布

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

}
}

10条回答
爷的心禁止访问
2楼-- · 2019-01-04 13:19

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'
查看更多
来,给爷笑一个
3楼-- · 2019-01-04 13:19

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.

查看更多
▲ chillily
4楼-- · 2019-01-04 13:26

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.

查看更多
相关推荐>>
5楼-- · 2019-01-04 13:29

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.

查看更多
forever°为你锁心
6楼-- · 2019-01-04 13:31

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);
查看更多
欢心
7楼-- · 2019-01-04 13:36

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

查看更多
登录 后发表回答