Android 在product flavor中添加多个google-service.json文件

Android 在product flavor中添加多个google-service.json文件,android,firebase,google-play-services,build.gradle,android-productflavors,Android,Firebase,Google Play Services,Build.gradle,Android Productflavors,我有3个不同的Firebase项目。而且它必须在4google service.json产品风格中使用。但我得到一个错误 缺少文件google-services.json。没有它,谷歌服务插件就无法运行 对于下面的结构 如果我把google service.json放在app文件夹中,我会得到这个错误 找不到包名称“com.example.app1”的匹配客户端 对于下面的结构 build.gradle flavorDimensions "category", "app" pro

我有3个不同的Firebase项目。而且它必须在4google service.json产品风格中使用。但我得到一个错误

缺少文件google-services.json。没有它,谷歌服务插件就无法运行

对于下面的结构

如果我把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"
        }
    }

解决方案:

我能做到这一点的唯一方法是绕过使用
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");
if(){
apiKey=;
数据库URL=;
}若否({
apiKey=;
数据库URL=;
}其他//生产{
apiKey=;
数据库URL=;
}
FirebaseOptions FirebaseOptions=新建FirebaseOptions.Builder()
.setApiKey(apiKey)
.setApplicationId(context.getString(R.string.google\u app\u id))
.setDatabaseUrl(数据库URL)
.build();
返回FirebaseApp.initializeApp(上下文,firebaseOptions,“MyApp”);

解决方案:

我能做到这一点的唯一方法是绕过使用
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");
if(){
apiKey=;
数据库URL=;
}若否({
apiKey=;
数据库URL=;
}其他//生产{
apiKey=;
数据库URL=;
}
FirebaseOptions FirebaseOptions=新建FirebaseOptions.Builder()
.setApiKey(apiKey)
.setApplicationId(context.getString(R.string.google\u app\u id))
.setDatabaseUrl(数据库URL)
.build();
返回FirebaseApp.initializeApp(上下文,firebaseOptions,“MyApp”);