Java 名称为 [DEFAULT] 的 FirebaseApp 不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37342403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:13:29  来源:igfitidea点击:

FirebaseApp with name [DEFAULT] doesn't exist

javaandroidfirebase

提问by natsumiyu

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.

迁移到 Firebase Cloud Messaging 后。打开我的应用程序时,它崩溃并抛出一个错误,说java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't exist.我已经放置了新的 google-services.json 并更新了我的 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());

}
}

采纳答案by Venkatesan

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

在应用程序的gradle文件底部,然后清理并重建项目

回答by Maxim Firsoff

Android Studio

安卓工作室

  1. apply plugin: 'com.google.gms.google-services' (build.gradle - Module layer)
  2. Menu~>Build~>CleanProject
  1. 应用插件:'com.google.gms.google-services'(build.gradle - 模块层)
  2. 菜单~>Build~>CleanProject

Works for me ok.

对我有用。

回答by elron smith

build.gradle file:

build.gradle 文件:

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:

\app\build.gradle 文件:

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'

回答by jmodrako

I've had similar problem, and for me it was a bug/problem with manifest merger. I've found out that FirebaseInitProviderhas 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 FirebaseInitProviderwill be injected and Firebase can be initialized properly.

我遇到了类似的问题,对我来说这是清单合并的错误/问题。我发现FirebaseInitProvider由于tools:node="replace"在我的应用程序的清单文件中,它没有被注入到最终清单文件中。因此,尝试删除此 xml 标记FirebaseInitProvider并将被注入并且 Firebase 可以正确初始化。

回答by Ramya Ramesh

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.

将您的 firebase 初始化移动到 Application 类的 onCreate 中。此外,如果您启用了离线持久性,则 FirebaseDatabase.getInstance().setPersistenceEnabled(true) 应该在任何其他初始化之前出现。

回答by Codevalley

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 Applicationclass, 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-crashdependancy 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,

如果您的应用程序有一个服务(具有不同的进程)并且您正在创建自己的Application类,则该服务和前台应用程序将使用相同的 Application 类(不是相同的实例)进行初始化。现在,当我使用com.google.firebase:firebase-crash依赖来处理崩溃时,它会创建一个后台服务your.app.packagename:background_crash。出于某种原因,这会导致我的应用程序崩溃。具体来说,因为在我的应用程序类中,我正在打电话,

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

我假设,使用我们的 Application 类初始化时的后台服务不知何故未初始化 Firebase。为了解决这个问题,我做了

if (!FirebaseApp.getApps(this).isEmpty())
        FirebaseDatabase.getInstance().setPersistenceEnabled(true);

回答by pavel

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'

申请 plugin: 'com.google.gms.google-services'

回答by Carlos Eduardo Liedtke Borges

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

@jmodrako回答解决我的问题......tools:node="replace"tools:node="merge"

Explained... on AndroidManifest.xml

在 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"

合并图书馆主题的问题?使用解决的构建问题tools:replace="android:theme"

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

归功于https://stackoverflow.com/a/38060272/2765087

回答by Carlos Eduardo Liedtke Borges

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

在 Firebase 中注册您的应用程序并将 google-services.json 复制到您的根项目。

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

适用 classpath 'com.google.gms:google-services:3.1.0于您的 root build.gradle。

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

适用apply plugin: 'com.google.gms.google-services于您的项目gradle。

回答by Abdul basha

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

将构建操作 (GoogleServicesJson) 更改为文件名 Google-Services.Json。