user3352488
user3352488

Reputation: 281

Multiple google-services.json Per Product Flavour

I think this is a variation on other questions that have been posed on this subject.

I have two product flavors. I deploy my app in different environments, each of which talk to different Firebase projects. As such, for each flavor I need to be able to target a specific environment (dev, test, production, etc.)

Is there a way, I can make build variants of a flavor that select the appropriate google-services.json file without introducing new product flavors? Maybe I am approaching this problem in the wrong way...

Upvotes: 3

Views: 4554

Answers (2)

John O'Reilly
John O'Reilly

Reputation: 10330

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

Upvotes: 1

Akshay Chopde
Akshay Chopde

Reputation: 690

First, place the respective google_services.json for each buildType in the following locations:

app/src/debug/google_services.json
app/src/main/google_services.json

Now, let’s whip up some gradle tasks in your :app’s build.gradle to automate moving the appropriate google_services.json to app/google_services.json

task switchToDebug(type: Copy) {
    description = 'Switches to DEBUG google-services.json'
    from "src/debug"
    include "google-services.json"
    into "."
}

task switchToRelease(type: Copy) {
    description = 'Switches to RELEASE google-services.json'
    from "src/release"
    include "google-services.json"
    into "."
}

Great — but having to manually run these tasks before you build your app is cumbersome. We would want the appropriate copy task above run sometime before :assembleDebug or :assembleRelease is run. Let’s see what happens when :assembleRelease is run:

Zaks-MBP:my_awesome_application zak$ ./gradlew assembleRelease
Parallel execution is an incubating feature.
.... (other tasks)
:app:processReleaseGoogleServices
....
:app:assembleRelease

Notice the :app:processReleaseGoogleServices task. This task is responsible for processing the root google_services.json file. We want the correct google_services.json to be processed, so we must run our copy task immediately beforehand. Add this to your build.gradle. Note the afterEvaluate enclosing.

afterEvaluate {
    processDebugGoogleServices.dependsOn switchToDebug
    processReleaseGoogleServices.dependsOn switchToRelease
}

Now, anytime :app:processReleaseGoogleServices is called, our newly defined :app:switchToRelease will be called beforehand. Same logic for the debug buildType. You can run :app:assembleRelease and the release version google_services.json will be automatically copied to your app module’s root folder.

https://medium.com/google-cloud/automatic-per-variant-google-services-json-configurations-with-gradle-d3d3e40abc0e

Upvotes: 0

Related Questions