【问题标题】:FirebaseApp with name [DEFAULT] doesn't exist名称为 [DEFAULT] 的 FirebaseApp 不存在
【发布时间】:2016-09-17 11:11:03
【问题描述】:

迁移到 Firebase Cloud Messaging 后。打开我的应用程序时它崩溃并抛出错误消息 java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't exist. 我已经放置了新的 google-services.json 并更新了我的 SDK。

这是我的 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());

}
}

【问题讨论】:

    标签: java android firebase


    【解决方案1】:

    请仔细检查,你添加了

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

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

    【讨论】:

    • 我从我的应用程序 gradle 中删除了 apply plugin: 'com.google.gms.google-services',但在我的 MainActivity 我的应用程序崩溃中调用 FirebaseInstanceId.getInstance().getToken() 时遇到了一个新错误。
    • 我的错...我需要添加 apply plugin: 'com.google.gms.google-services' 吗?尝试添加它我仍然遇到错误。它说Didn't find class "com.google.android.gms.dynamite.descriptors.com.google.firebase.auth.ModuleDescriptor"
    • 您好 Mori,您现在可以忽略此错误,他们将在下一个版本中修复此问题。这就是firebase团队所说的:)。
    • Flutter 可能需要flutter clean & flutter run,正如答案中所建议的那样。
    • 如果我使用 Firebase Java 管理员会怎样? (Java 后端而不是 Android 客户端应用)
    【解决方案2】:

    不确定,是否与此处相关。但是还有另一种情况可能发生这种崩溃。


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

    FirebaseDatabase.getInstance().setPersistenceEnabled(true);
    

    我假设,在使用我们的 Application 类初始化时,后台服务以某种方式未初始化 Firebase。为了解决这个问题,我做了

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

    【讨论】:

    • 广告也会出现这种情况。如果用户点击广告然后返回应用 Firebase 且不再可用。
    • @Codevalley,在我的例子中,还有另一个名为 app:editor 的进程(Adobe Creative SDK Image Editor)。它使用相同的应用程序类,因此试图从不存在的 firebase 应用程序中获取令牌。在获得令牌之前检查解决了我的问题。太感谢了! :)
    【解决方案3】:

    我也遇到过类似的问题,对我来说这是清单合并的错误/问题。我发现FirebaseInitProvider 没有被注入到最终的清单文件中,因为我的应用程序清单文件中有tools:node="replace"。所以,试着去掉这个xml标签,FirebaseInitProvider就会被注入,Firebase就可以正常初始化了。

    【讨论】:

    • 能否请您为那些不太熟悉合并工具的人更新您的答案@jmodrako
    • 目前我已经更新到 tools:replace="android:name" 而不是 tools:node="replace"。希望有所帮助
    【解决方案4】:

    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 文件:

    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'
    

    【讨论】:

    • 错误:(90, 0) 未找到 ID 为“com.google.gms.google-services”的插件。
    【解决方案5】:

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

    解释...在AndroidManifest.xml

    来自

    <application
    ...
    tools:node="replace">
    

    收件人

    <application
    ...
    tools:node="merge">
    

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

    感谢https://stackoverflow.com/a/38060272/2765087

    【讨论】:

      【解决方案6】:

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

      【讨论】:

      • 现在不小心点击了投票,所以不允许我投票:(
      【解决方案7】:

      验证所有配置如下:

      1-firebase 项目设置: google-services.json 是否正确?

      2- 添加 Firebase SDK

      3- clean - 重建你的项目

      希望这会有所帮助!

      【讨论】:

        【解决方案8】:

        将以下行添加到 app/build.gradle

        apply plugin: 'com.google.gms.google-services'  // Google Services plugin
        

        下面这行项目 build.gradle

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

        【讨论】:

          【解决方案9】:

          Android 工作室

          1. 应用插件:“com.google.gms.google-services”(build.gradle - 模块层)
          2. Menu~>Build~>CleanProject

          对我有用。

          【讨论】:

            【解决方案10】:

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

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

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

            【讨论】:

              【解决方案11】:

              在你的依赖中添加:

              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'
              }
              

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

              【讨论】:

                【解决方案12】:

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

                【讨论】:

                  【解决方案13】:

                  在我的情况下,我没有在应用启动时初始化 Firebase,我必须执行以下操作来解决它

                  @Service
                  public class FirebaseSetup implements CommandLineRunner {
                      public void run(String... args) throws Exception {
                          initializeFirebase();
                      }
                      private void initializeFirebase() throws FileNotFoundException, IOException {
                          FileInputStream serviceAccount = new FileInputStream(ResourceUtils.getFile("classpath:ssf1-v1-firebase-adminsdk-zr72u-afcb5bc13b.json"));
                          FirebaseOptions options = new FirebaseOptions.Builder().setCredentials(GoogleCredentials.fromStream(serviceAccount)).build();
                          FirebaseApp.initializeApp(options);
                      }
                  }
                  

                  【讨论】:

                    【解决方案14】:

                    classpath 'com.google.gms:google-services:3.0.0' 添加到build.gradle

                    dependencies {
                            ..
                            classpath 'com.google.gms:google-services:3.0.0'
                        }
                    

                    在 app\build.gradle 的末尾添加这个

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

                    这对我有用

                    【讨论】:

                      猜你喜欢
                      • 2019-12-04
                      • 2016-12-07
                      • 2021-10-17
                      • 2017-04-25
                      • 1970-01-01
                      • 2019-07-09
                      • 1970-01-01
                      • 2016-09-28
                      • 1970-01-01
                      相关资源
                      最近更新 更多