我的 Android 应用程序有多种版本,我希望除一个之外的所有应用程序都使用相同的密钥.有一个需要使用不同的密钥.
I have many flavors of my Android app, and I want all but one to use the same key. There is one that needs to use a different key.
我如何只为 1 种风格的应用覆盖 signingConfig(但在相同的构建类型中,例如发布")?
How do I override the signingConfig for just 1 flavor of the app (but within the same build type e.g. "release")?
gradlew assembleRelease 命令运行所有发布版本gradlew assembleRelease command最后一点很重要,因为我目前有超过 120 种不同的口味,而且还在不断增长.为了单独定制每一种口味,需要做很多额外的工作.
This last point is important as I currently have over 120 different flavors and growing. In order to customise every single flavor individually is a lot of extra work.
我尝试过的相关帖子:
从单一构建类型生成使用不同密钥签名的多个构建
signingConfig 反正使用 gradle 签署产品风格
buildTypes 里面的 productFlavors 但我不明白如何做到这一点.buildTypes inside the productFlavors but I do not understand how to do this.在 Gradle 产品风味上调试签名配置
总的来说,每个解决方案似乎仍然使用默认的发布配置,而不是我的自定义配置.
Overall, each solution seems to still use the default release config, instead of my custom config.
我的 build.gradle 的重要部分如下所示:
Important parts of my build.gradle look like this:
signingConfigs {
releaseConfig {
storeFile file('key')
storePassword "pass"
keyAlias "alias"
keyPassword "pass"
}
custom {
storeFile file('custom_key')
storePassword "pass"
keyAlias "alias"
keyPassword "pass"
}
}
productFlavors {
apple {
applicationId "demo.apple"
}
banana {
applicationId "demo.banana"
}
// def customConfig = signingConfigs.custom
custom {
applicationId "custom.signed.app"
// signingConfig customConfig
}
}
buildTypes {
debug {
applicationIdSuffix ".debug"
}
release {
signingConfig signingConfigs.releaseConfig
// productFlavors.custom.signingConfig signingConfigs.custom
}
}
Gradle Plugin User Guide 说你可以:
通过设置每个发布包使用自己的 SigningConfigandroid.productFlavors.*.signingConfig 对象分开.
have each release package use their own
SigningConfigby setting eachandroid.productFlavors.*.signingConfigobjects separately.
此答案(Debug Signing Config on Gradle Product Flavors)和此博客文章 (使用 Gradle 构建多个版本的 Android 应用).
This is demonstrated in this answer (Debug Signing Config on Gradle Product Flavors) and this blog post (Building Multiple Editions of an Android App with Gradle).
但是,为每种风格指定单独的 signingConfig 行不能很好地扩展,并且超出了问题的范围.不幸的是,提供的答案都没有显示如何正确覆盖 signingConfig.
However, specifying a separate signingConfig line for each flavor does not scale well, and was out of scope of the question. Unfortunately none of the provided answers showed how to correctly override a signingConfig correctly.
诀窍来自这个答案(How to get the current selected build variant in gradle?),它展示了如何循环构建变体(以及扩展,风味).
The trick came from this answer (How to get the currently chose build variant in gradle?) which shows how to loop over build variants (and by extension, flavors).
我的解决方案使用循环来设置每种风味的 signingConfig,而不是为此设置单独的行.这可以很好地扩展.覆盖"是通过在循环之后指定自定义配置的单行来完成的.
My solution uses a loop to set the signingConfig on each flavor instead of having a separate line for that. This scales perfectly well. The "override" is done with a single line that specifies the custom config after the loop.
将以下代码放在 buildTypes.release 块中:
Place the following code inside the buildTypes.release block:
// loop over all flavors to set default signing config
productFlavors.all { flavor ->
flavor.signingConfig signingConfigs.releaseConfig
}
// override default for single custom flavor
productFlavors.custom.signingConfig signingConfigs.custom
这篇关于在 Android 上使用不同密钥的 Gradle 签名风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
更新到 Android Build Tools 25.1.6 GCM/FCM 后出现 IncompIncompatibleClassChangeError after updating to Android Build Tools 25.1.6 GCM / FCM(更新到 Android Build Tools 25.1.6 GCM/FCM 后出现 Incompatible
如何在 gradle 中获取当前风味How to get current flavor in gradle(如何在 gradle 中获取当前风味)
如何修复“意外元素<查询>在“清单How to fix quot;unexpected element lt;queriesgt; found in lt;manifestgt;quot; error?(如何修复“意外元素lt;查询gt;在“清单中找到错误
基于 Android Gradle 中多风味库的多风味应用Multi flavor app based on multi flavor library in Android Gradle(基于 Android Gradle 中多风味库的多风味应用)
Android 依赖在编译和运行时有不同的版本Android dependency has different version for the compile and runtime(Android 依赖在编译和运行时有不同的版本)
本地 aar 库的传递依赖Transitive dependencies for local aar library(本地 aar 库的传递依赖)