【问题标题】:can I capture global variables in android, with ionic 5 capacitor?我可以使用离子 5 电容器在 android 中捕获全局变量吗?
【发布时间】:2025-12-27 13:35:12
【问题描述】:

我需要使用 android 和 iOS 进行配置,在进行部署时接受更改,并使用 firebase 进行配置,当我创建项目并对其进行配置时,它会给我一个文件 google-services.json 并在iOS一个GoogleService-Info.plist,我需要的是在firebase中创建的不同环境,例如我想为生产环境添加文件google-service.json,但如果它处于暂存状态,请使用google 设置-service-staging.json 等等。

所以当你在项目的build.gradle中调用变更时,传给它一个全局变量(不知道是不是真的可以做到)

def servicesJSON = file('google-services'+enviroment+'.json')

类似这样的东西,所以根据你遇到的情况,你知道他们在什么环境中部署......

【问题讨论】:

  • 这种行为不是 Ionic 或 Capacitor 的一部分,您只需编写一个脚本来读取环境变量并将适当的设置写入您的 GoogleService-Info.plist 或 google-services.json 文件。然后将该脚本作为构建序列的一部分运行。

标签: android capacitor ionic5


【解决方案1】:

对于风味(阶段,产品),您应该将单独的 json 文件放在 android 项目的相应目录中。

例如https://android.jlelse.eu/how-to-use-different-google-services-json-file-with-multiple-product-flavors-android-7853d98dd6c0

【讨论】:

  • 不客气。让我知道您是否需要任何帮助:)