Handle Multiple Firebase Project in An Android Project

Blasius Neri
2 min readJun 20, 2021

Hello Android Geeks,
I have developing an app that support multiple environment like production, preproduction, and testing and so far all environment working find until my apps planning to add firebase and support for multiple firebase environment. Here’s the problem.

Usually, to implement firebase, we need to add google-services.json and it can put in different build variant like staging release, preproduction release, production release, etc by putting the file in specific folder name.

support multiple firebase by build variant

This solution still has a problem, because my app can change environment in run time and when environment change, firebase environment still stuck in the different environment like select testing environment but the analytics, real time database, firebase messaging service connect to production which is it hard to test and make sure app didn’t report any crash or analytic in wrong environment.

Because of that problem, I’m change the implementation of initiate firebase project by programmatically. First step is copy all key from google-service.json and put as constant. In my implementation, I put the constant in build gradle. Put all needed key, in below example, I use Firebase Real Time Database.

Programmatically initiate Firebase Project

From above implementation, I can use Firebase Cloud Messaging and Firebase real time database in different environment. Those implementation also required app to killed first then relaunch because in app launch, Firebase will reinit the Firebase key. For analytics, it can’t initialize using same way because it will based on the build. So I still keep the google-service.json by build variant for analytics.

--

--