First of all, we need to make sure we know how to handle multiple Firebase Environments.
Currently there are few ways, as having flavours or dart env config setup — however these are rather complicated to set-up on iOS side. After my question being not answered, I decided to do something about it myself.
Different Firebase environments made easy
Long story short, I’ve written a Node script, which generates correct environment before app build. All you need to do is:
- Create a new folder
- Clone repo contents to this folder. Main
tsfile should appear in
- Add your
jsonin the same path with correct suffix (
prod). Production files will have a suffix
-prod, Development files will have a suffix
You can get
jsonfile from Firebase while setting up an Android app.
You can get
plistfile from Firebase while setting up an iOS app.
End result file structure should look like this:
What script does, is simply detects, which environment you want to build you app in and copies correct file into correct folder within
For example, you run
node firebase-environment-generator dev .
- It locates
google-services-dev.jsonand places it to
- It locates
GoogleService-Info-dev.plistand places it to
That’s all it is.
To run the script, simply write
node firebase-environment-generator dev or
node firebase-environment-generator prod while you are in the correct path
Be aware that before generating new environment files —
flutter clean needs to be run. Seems that iOS caches config files therefore it’s not always reflecting the change. However after
flutter clean it works 100% of time.
If you’d like to forget about these commands, you can configure your IDE for such purpose. I’ll show how to do it in Android Studio.
Find Edit Configuration and go inside
Add new Shell Script
Name the script appropriately and fill in required data:
Script path— path where your
firebase-environment-generatordev or prod;
Working directory— your directory where script is located.
And click yes.
Now you have a shell script which you can run at any time.
Run it and you will see that everything went well! (Or not :))
You can improve the script to add pre-build
flutter clean or even put script within same build as building the app. I’ll leave it up to you.
I hope this tutorial will help you to leverage multiple environments in Firebase.
In the next tutorial I’ll move one step further — how to build an app within multiple environments with different bundle ids. So that
dev app can be on the same mobile device.