androidfirebaseconfigvariants

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...


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