【问题标题】:add multiple google-service.json file in product flavor在产品风味中添加多个 google-service.json 文件
【发布时间】:2020-03-16 13:47:21
【问题描述】:

我有 3 个不同的 Firebase 项目。并且它必须用于 4 google-service.json 在产品风格中的使用。但我收到以下错误

文件 google-services.json 丢失。没有它,Google 服务插件将无法运行。

下面的结构

如果我将 google-service.json 放在 app 文件夹中,则会出现此错误

找不到与包名称“com.example.app1”匹配的客户端

下面的结构

build.gradle

 flavorDimensions "category", "app"

    productFlavors {
        mcq1 {
            applicationIdSuffix ""
            dimension "category"
        }
        mcq2 {
            applicationIdSuffix ""
            dimension "category"
        }
        mcqappgj {
            dimension "app"
            applicationId "com.example.app1"
            versionCode 9
            versionName "1.0.7"
        }
        mcqappen {
            dimension "app"
            applicationId "com.example.app2"
            versionCode 7
            versionName "1.0.6"
        }
        gpscapp {
            dimension "app"
            applicationId "com.example.app3"
            versionCode 2
            versionName "1.0.1"
        }
        eemcq {
            dimension "app"
            applicationId "com.example.app4"
            versionCode 2
            versionName "1.0.1"
        }
    }

【问题讨论】:

    标签: android firebase google-play-services build.gradle android-productflavors


    【解决方案1】:

    解决方案:

    我能够做到这一点的唯一方法是绕过使用 google-services.json 并动态创建 FirebaseApp 实例例如

     if (<is dev>) {
            apiKey = <dev api key>;
            databaseUrl = <dev database url>;
        } else if (<is test> {
            apiKey = <>;
            databaseUrl = <>;
        } else // production {
            apiKey = <>;
            databaseUrl = <>;
        }
    
    
        FirebaseOptions firebaseOptions = new FirebaseOptions.Builder()
                .setApiKey(apiKey)
                .setApplicationId(context.getString(R.string.google_app_id))
                .setDatabaseUrl(databaseUrl)
                .build();
    
        return FirebaseApp.initializeApp(context, firebaseOptions, "MyApp");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-24
      • 1970-01-01
      • 2016-08-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多