43

I have multiple flavors of my app. How should I set this up server side? My package names are:

com.example.app (production) com.example.app.staging (staging) com.example.app.fake (fake)

Should this be 3 separate projects in the firebase console?

ZakTaccardi
  • 12,212
  • 15
  • 59
  • 107

3 Answers3

35

Largely it will depend on how you want your project to work. You can set all three up in the same console, or you can set up as two or more different projects. Either option is valid.

Benefits of same project:

  • Share the same billing, quotas, permissions, and services (database, storage, FCM etc).
  • Environment which is the same as production.

Benefits of different projects:

  • No risk of overwriting production data or affecting production users.

If using multiple projects, you can take advantage of the build types support which will allow you to have different google-services.json files for different versions. If using one project, the same google-services.json will work for all the varieties.

Note: as CodyMace says in the comments - remember to re-download the JSON file each time you add an app!

There are things you can do to minimise risks in either case (e.g. have dev/ stage/ prod/ keys in your Database, and have similar structures underneath), but what makes sense for you is largely about tradeoffs.

If you're just starting out, I would suggest starting with one project while you're in development, and once you've launched consider moving your development environment to a separate project. Staging could go either way.

Ian Barber
  • 19,765
  • 3
  • 58
  • 58
  • note, I went with multiple package names within the same app approach – ZakTaccardi Jun 07 '16 at 22:34
  • 9
    I didn't realize that every time you add a new "app" in other words a "staging" or "debug" version, it creates a json file for you with all of the apps together. – CodyMace Jul 13 '16 at 17:40
  • Ian Barber: Please mention @CodyMace's comment in you answer. It's significant information. – Raul Pinto Aug 30 '16 at 07:12
  • If you go with the first option, using 2 app in the same console, they both are going to share the same DB right? – Nico Feb 13 '17 at 00:26
  • @IanBarber And there is no way to share the same auth and settings (rules, etc.) between two projects? – Nico Feb 14 '17 at 20:06
  • Touched on a little bit in https://firebase.googleblog.com/2016/12/working-with-multiple-firebase-projects-in-an-android-app.html but largely no - though if you use the firebase CLI to upload rules you could use that to push identical rules to both projects. – Ian Barber Feb 15 '17 at 21:42
  • How to use multiple apps in the same project if each build flavor has different package name and you can't create one json file for all of them? – Choletski Mar 09 '17 at 09:43
  • _If using **one project, the same google-services.json** will work for all the varieties._ is not working. I see multiple son file available in console to download. Any suggestion how to minimise that, @IanBarber ? – CoDe Dec 06 '17 at 01:12
8

Note I didn't fully try this yet, but documenting it here to not lose it until I get to it.

One actually isn't forced to use the gradle plugin, which enforces you to have a firebase project for all of your flavors and build types.

This is poorly documented, but one can find a hint at the top of the documentation for FirebaseApp and some more at https://firebase.google.com/docs/configure/

Alternatively initializeApp(Context, FirebaseOptions) initializes the default app instance. This method should be invoked from Application. This is also necessary if it is used outside of the application's main process.

So, fetch the google-services.json as usual and from it take mobilesdk_app_id and current_key (under api_key), that should be all that's needed for Google Analytics tracking at least. With that information run the following in your app's Application subclass for the variants where you need it:

FirebaseOptions options = new FirebaseOptions.Builder()
  .setProjectId("<project_id>")
  .setApplicationId("<mobilesdk_app_id>")
  .setApiKey("<current_key>")
  .build();
FirebaseApp.initializeApp(this, options);
Jonne Haß
  • 4,792
  • 18
  • 30
0

EDIT Obsolete solution

in case you are wondering to how to use google-services.json for different firebase backends. first place all relevant json files in separate folder like:

...src/dev/google-services.json ...src/prod/google-services.json

you need to move file when a build task is running to the root as per your flavour in app level gradle file

like this:

android{
...
    def flavourName = getCurrentFlavor()
        delete 'google-services.json'
        clean
        if (flavourName == "development") {
            copy {
                from 'src/dev/'
                include '*.json'
                into '.'
            }
        } else if (flavourName == "stagging") {
            copy {
                from 'src/dev/'
                include '*.json'
                into '.'
            }
        } else if (flavourName == "production") {
            copy {
                from 'src/prod/'
                include '*.json'
                into '.'
            }
        } else {
            println("NA")
        }

}

here getCurrentFlavor() is defined as->

def getCurrentFlavor() {
    Gradle gradle = getGradle()
    String tskReqStr = gradle.getStartParameter().getTaskRequests().toString()

    Pattern pattern
    println tskReqStr

    if (tskReqStr.contains("assemble"))
        pattern = Pattern.compile("assemble(\\w+)(Release|Debug)")
    else
        pattern = Pattern.compile("generate(\\w+)(Release|Debug)")

    Matcher matcher = pattern.matcher(tskReqStr)

    if (matcher.find())
        return matcher.group(1).toLowerCase()
    else {
        println "NO MATCH FOUND"
        return ""
    }
}
  • 1
    This is unnecessary, you can just put the google-services.json in flavor folders. See here: https://stackoverflow.com/questions/30772201/google-services-json-for-different-productflavors – Chris McCabe Jul 07 '21 at 14:54