[英]How to programatically change Firebase google JSON for different build variants?
In general to switch between build variants you can use productFlavors
. 通常,可以在
productFlavors
变体之间进行切换,可以使用productFlavors
。 But I want to change these variants in runtime with button. 但是我想在运行时使用按钮更改这些变体。
There is ServerType
enum class containing multiple build variants. ServerType
枚举类包含多个构建变体。 I have Server
class with apiKey
and url
attribute. 我有带有
apiKey
和url
属性的Server
类。
Inside those variants are Server
objects.(You can use multiple APIs per build variant). 这些变量内部是
Server
对象。(每个构建变量可以使用多个API)。
This enum class has same functionality as productFlavors
. 该枚举类具有与
productFlavors
相同的功能。 I can change those addresses in runtime and set active variant from dropdown menu. 我可以在运行时更改这些地址,并从下拉菜单中设置活动变量。 This will affect every API call in my app.
这将影响我应用中的每个API调用。 What I cant change is what
google.json
is chosen by Firebase. 我无法更改的是Firebase选择的
google.json
。 Is there any way how to tell Firebase to use different json? 有什么办法告诉Firebase使用不同的json吗? I have 4 variants and jsons are located inside each variant file in project.
我有4个变体,并且jsons位于项目中的每个变体文件内。
Firebase is choosing automatically google.json based on what Build Variant
is selected while building the app. Firebase会在
Build Variant
应用时根据选择的Build Variant
自动选择google.json。
Have you tried the multiple environment directories? 您是否尝试过多个环境目录? Which allows you to put several different
google.json
files for different build variants. 这允许您为不同的构建变体放置几个不同的
google.json
文件。 ( help from firebase site ). ( 来自Firebase网站的帮助 )。
This is also a dedicated directory, so not that dynamic, but this is all that the Firebase can handle on runtime. 这也是一个专用目录,因此不是动态目录,而是Firebase在运行时可以处理的所有目录。
The choice of which google-services.json file to use is always determined at build time by the Google play services plugin. 使用哪个google-services.json文件的选择始终由Google Play服务插件在构建时确定。 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. 如果您需要在运行时更改项目配置,则将无法使用该插件或其google-services.json配置文件。 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.
您将必须自己调用FirebaseApp.initializeApp()而不是接受默认配置。 There are a lot of strategies to do this, but I will recommend you start here .
有很多策略可以做到这一点,但是我建议您从这里开始 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.