1
votes

So, I'm building a set of applications from a single code base, and I'm using productFlavors to customize each app.

Each flavor will need to have its own tracking ID for Google Analytics, so I started generating a new google-services.json for each flavor, and I found out that there's a limit at the Google Developer Console.

The question is, how can I use the same json file for all the flavors? (Each flavor has its own applicationID)

Or what are my alternatives? I only need to get the Analytics tracking working, and that's pretty much why I even started using the file in the first place.

I thought about editing manually each file to change its package_name and tracking_id, but I'm not sure that's a viable option for production...

2

2 Answers

3
votes

So I kept looking into it, and turns out you don't really need one new google-services.json for each flavor.

It doesn't seem that much intuitive at first, but when you're creating the json file, you need to set the same App name for all flavors, just different package names.

E.g.: When you're in this step, the app name would be MyApp, and the package name my.app.free.

Proceed until the end where you get to the step where you download the file. Then, just go to that step again, and now set the same App Name (in this example MyApp), but with the new package name (e.g.: my.app.pro).

When you download the new google-services.json, it will be set up for both packages, and with different analytics.

1
votes

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.

enter image description here

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! :)