add multiple google-service.json file in product flavor

孤者浪人 提交于 2020-01-24 19:23:35

问题


I have 3 different Firebase projects. and it has to be used in 4 google-service.json use in product flavors. but i get an below error

File google-services.json is missing. The Google Services Plugin cannot function without it.

for below structure

And if I put the google-service.json in the app folder then i get this error

No matching client found for package name 'com.example.app1'

for below structure

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

回答1:


Solution:

The only way I was able to do this was to bypass use of google-services.json and create FirebaseApp instance dynamically e.g.

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


来源:https://stackoverflow.com/questions/58961183/add-multiple-google-service-json-file-in-product-flavor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!