1

我的 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 应根据给定的配置进行签名。

4

1 回答 1

2

显然,这就是 Gradle Sync 期间发生的事情。

Variant.signingConfig是 Flavor 和 Build-type 的 Overwritten 机制,以相同的顺序,基于值是否存在。

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

于 2018-08-15T16:00:09.997 回答