我的android app模块build.gradle文件中有以下内容。
android {
defaultConfig {
...
}
signingConfigs {
if (System.getEnv()['CIRCLE-CI']) { /* CI System */
localMachine { /* CI System KeyStore file and properties. */
...
keyAlias = "ABC"
}
playStore { /* CI System KeyStore file and properties. */
...
keyAlias = "ABC"
}
} else { /* Local machine, not CI System. */
localMachine { /* debug.keystore local file and properties. */
...
keyAlias = "androiddebugkey"
}
playStore { /* debug.keystore local file and properties. */
...
keyAlias = "androiddebugkey"
}
}
}
productFlavors {
productOne {
...
}
productTwo {
...
}
}
buildTypes {
debug {
...
}
release {
...
}
}
}
/** This is where the problem is **/
android.applicationVariants.all { variant ->
if (variant.mergedFlavor.name == "productOne") {
if (variant.buildType.name == "debug") {
variant.signingConfig android.signingConfigs.localMachine
} else if (variant.buildType.name == "release") {
variant.signingConfig android.signingConfigs.playStore
}
} else if (variant.mergedFlavor.name == "productTwo") {
if (variant.buildType.name == "debug") {
variant.signingConfig android.signingConfigs.localMachine
} else if (variant.buildType.name == "release") {
variant.signingConfig null
}
}
}
}
预期产量如下。
productOneDebug -> Sign with signingConfigs.localMachine
productTwoDebug -> Sign with signingConfigs.localMachine
productOneRelease -> Sign with signingConfigs.playStore
productTwoRelease -> Unsigned.
实际输出如下。
productOneDebug -> Signed with signingConfigs.localMachine
productTwoDebug -> Signed with signingConfigs.localMachine
productOneRelease -> Unsigned.
productTwoRelease -> Unsigned.
为什么Release apks总是未签名?目的是只让ProductTwo Release apk无符号,但应根据给定的配置对ProductOne Release apk进行签名。
显然,这是Gradle Sync期间发生的事情。
Variant.signingConfig
是一个来自Flavor和Build-type的覆盖机制,它以相同的顺序,基于值是否存在。
productOne.signingConfig = playStore
productTwo.signingConfig = null
debug.signingConfig = localMachine
release.signingConfig = null
由此产生的变种将 -
productOneDebug.signingConfig = productOne.signingConfig
由debug.signingConfig
覆盖
productTwoDebug.signingConfig = productTwo.signingConfig
由debug.signingConfig
覆盖
productOneRelease.signingConfig = productOne.signingConfig
如果存在,由release.signingConfig
覆盖。
productTwoRelease.signingConfig = productTwo.signingConfig
如果存在,由release.signingConfig
覆盖。