我的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.

为什么发行apk总是未签名?
目的是仅对ProductTwo Release apk进行未签名,但是应该基于给定的配置对ProductOne Release apk进行签名。

最佳答案

显然,这是在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覆盖。

10-07 18:33