简体   繁体   中英

How to programatically change Firebase google JSON for different build variants?

In general to switch between build variants you can use productFlavors . But I want to change these variants in runtime with button.

There is ServerType enum class containing multiple build variants. I have Server class with apiKey and url attribute.

Inside those variants are Server objects.(You can use multiple APIs per build variant).

This enum class has same functionality as productFlavors . I can change those addresses in runtime and set active variant from dropdown menu. This will affect every API call in my app. What I cant change is what google.json is chosen by Firebase. Is there any way how to tell Firebase to use different json? I have 4 variants and jsons are located inside each variant file in project.

Firebase is choosing automatically google.json based on what Build Variant is selected while building the app.

Have you tried the multiple environment directories? Which allows you to put several different google.json files for different build variants. ( help from firebase site ).

This is also a dedicated directory, so not that dynamic, but this is all that the Firebase can handle on runtime.

The choice of which google-services.json file to use is always determined at build time by the Google play services plugin. It is never determined at runtime.

If you need to change project configurations at runtime, you will not be able to use that plugin or its google-services.json configuration file. You will need to find another way to put all the possible different configurations in your app at build time, then select which one to use at runtime. You will have to call FirebaseApp.initializeApp() yourself instead of accepting the default configuration. There are a lot of strategies to do this, but I will recommend you start here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM