Android 设置了不同的包名
buildTypes {
release {
}
debug {
applicationIdSuffix ".debug"
}
}
如何设置不同的google-services.json
Android 设置了不同的包名
buildTypes {
release {
}
debug {
applicationIdSuffix ".debug"
}
}
如何设置不同的google-services.json
引自免费微信小程序:皆我百晓生
在Android项目中,如果你有多个应用模块(即不同的包名),每个模块可能都需要Google Play Services的支持,比如Firebase服务。在这种情况下,你需要为每个模块创建并配置一个独立的google-services.json
文件。
首先,确保你在Firebase控制台上为每一个应用模块都创建了一个项目,并且获取到了对应的google-services.json
文件。对于每个应用模块,按照以下步骤进行配置:
将每个模块的google-services.json
文件放在对应模块的根目录下。例如,如果你有两个模块:app和module_debug,那么你应该这样放置文件:
app/google-services.json
module_debug/google-services.json
在module_debug
的build.gradle
文件中,除了现有的配置外,还需要添加以下内容来指定特定的google-services.json
文件:
apply plugin: 'com.google.gms.google-services'
android {
// ...
buildTypes {
debug {
applicationIdSuffix ".debug"
manifestPlaceholders = [manifestPackageName: "${applicationId}"]
}
}
packagingOptions {
pickFirst 'services.json'
}
}
这里我们使用了manifestPlaceholders
来动态设置应用的主包名,然后通过packagingOptions
中的pickFirst
规则,告诉Gradle在构建时优先选择哪个services.json
文件。因为module_debug
的google-services.json
位于其根目录,所以它会覆盖全局的google-services.json
。
build.gradle.kts
文件中也执行类似的操作。在android
块内添加以下内容:android {
// ...
buildTypes {
debug {
applicationIdSuffix = ".debug"
manifestPlaceholders = mapOf("manifestPackageName" to applicationId)
}
}
packagingOptions {
pickFirst("services.json")
}
}
完成以上步骤后,每个模块都会根据其特定的google-services.json
文件来配置Google Play Services相关功能。注意,这仅适用于不同包名下的模块,如果是在同一个模块内部切换包名,那么可能需要额外的配置来支持热更新等场景。