If each flavor has its own applicationID and you want a different tracking ID for Google Analytics, then you need to generate google-services.json for each flavor.
Please be careful, since google-services.json must be placed under the app folder, you need to manually copy google-services.json of a flavor to app folder whenever you change the flavor.
Here is my way to do it:
Let's say we have two flavors, development and production. Then put the google-services.json for development inside src/development/google-services folder. And for production inside src/production/google-services folder.
Then we need to configure the copy task for those google-service.json files. Put this script in your build.gradle under the app folder. For me, I put this script under the android {...}
.
android {
...
}
task switchToDevelopment(type: Copy) {
description = 'Switches to DEVELOPMENT google-services.json'
from "src/development/google-services"
include "google-services.json"
into "."
}
task switchToProduction(type: Copy) {
description = 'Switches to PRODUCTION google-services.json'
from "src/production/google-services"
include "google-services.json"
into "."
}
afterEvaluate {
processDevelopmentDebugGoogleServices.dependsOn switchToDevelopment
processDevelopmentReleaseGoogleServices.dependsOn switchToDevelopment
processProductionDebugGoogleServices.dependsOn switchToProduction
processProductionReleaseGoogleServices.dependsOn switchToProduction
}
This script will be executed whenever you change the flavor. It will copy the correct google-services.json of a flavor to app folder before executing process[FlavorBuildtypes]GoogleServices
. Hope it helps! :)