問題描述
我的 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")?
- 我希望所有構建都默認使用主發布配置.
- 我只想覆蓋 1 種風味
- 我希望能夠使用單個
gradlew assembleRelease
命令運行所有發布版本
- I would like all builds by default to use the main release configuration.
- I only want to override 1 flavor
- I want to be able to run all release builds with a single
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
但我不明白如何做到這一點.
- accepted solution doesn't work (for me)
- according to a comment this is possible by putting
buildTypes
inside theproductFlavors
but I do not understand how to do this.
在 Gradle 產品風味上調試簽名配置
- 如博文中所述:使用 Gradle 構建多個版本的 Android 應用
它不工作實際上它完美工作- 但它不能很好地適應 119 種口味
總的來說,每個解決方案似乎仍然使用默認的發布配置,而不是我的自定義配置.
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 說你可以:
通過設置每個發布包使用自己的 SigningConfig
android.productFlavors.*.signingConfig
對象分開.
have each release package use their own
SigningConfig
by setting eachandroid.productFlavors.*.signingConfig
objects 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模板網!