0
votes

I need to separate my firebase analytics logs between development/testing and production. I have 2 firebase projects one production and 1 dev. I would like to override the google-services.json file and initialize a difference instance of firebase when on dev. I'm trying to do something like:

if(!((MyApplication)getApplication()).isProd()) {
            FirebaseOptions options = new FirebaseOptions.Builder()
                    .setApplicationId("mypplicationid")
                    .setApiKey("myKey")
                    .setDatabaseUrl("myurl").build();
            FirebaseApp.initializeApp(this, options);
        }
        else{
            FirebaseApp.initializeApp(this);
        }

but the app crashes immediately with FirebaseApp name [DEFAULT] already exists. Is there a way around this besides having 2 google-services.json files and switching them out?

1
Your approach pretty much matches what the Firebase documentation recommends, except that you fail to give the FirebaseApp a name. - Frank van Puffelen
Adding a name dispelled the crash, but for some reason it's still sending the analytics to the prod firebase account. - Ryan McCaffrey
firebase.google.com/docs/configure/#reliable-analytics seems relevant to that. I must admit I never tried though, so can't be of much help. - Frank van Puffelen

1 Answers

0
votes

It's preferable to do this by placing your different google-sevices.json files in different directories that correspond to your build flavors for both development and production. The google services will know which one to use a build time based on which flavor you're building. The documentation for the plugin has a section that illustrates how you can do this.