Android构建版本签名配置

时间:2018-08-14 21:19:00

标签: android gradle signing build-variant

我的android应用模块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 APK总是未签名? 目的是仅对ProductTwo Release apk进行未签名,但应该根据给定的配置对ProductOne Release apk进行签名。

1 个答案:

答案 0 :(得分:0)

显然,这是在Gradle同步期间发生的情况。

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覆盖(如果存在)。