Android 口味签名未按预期工作

Android flavors signing not working as expected

我需要使用特定签名配置对产品风格进行签名。我在 Whosebug 上找到了一些参考,比如 and this。它适用于我的发行版 flavor,但不适用于调试版。我在 gradle:

中有这个配置
...
signingConfigs {
    release {
        storeFile file("../config/keystores/release_keystore")
        storePassword "mysecurepassword"
        keyAlias "myultrasecurealias"
        keyPassword "myreallysecurekeypassword"
    }

    debug {
        storeFile file("../config/keystores/debug.keystore")
        storePassword "mysecurepassword"
        keyAlias "myultrasecurealias"
        keyPassword "myreallysecurekeypassword"
    }

    other {
        storeFile file("../config/keystores/other")
        storePassword "mysecurepassword"
        keyAlias "myultrasecurealias"
        keyPassword "myreallysecurekeypassword"
    }
}

flavorDimensions "dim"

productFlavors {
    production {
        dimension "dim"
    }

    other {
        dimension "dim"
    }
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'        
        productFlavors.other.signingConfig signingConfigs.other
        productFlavors.production.signingConfig signingConfigs.release
    }

    debug {
        productFlavors.other.signingConfig signingConfigs.other
        productFlavors.production.signingConfig signingConfigs.debug
    }
}

这对风味 otherRelease 非常有效。但是当我使用构建配置 otherDebug 时,我的 APK 没有使用 other 签名配置进行签名。 release 版本已正确签名。

有谁知道为什么在调试模式下签名配置没有按配置应用?

我终于弄清楚出了什么问题,感谢@AllanHasegawa 在另一期的评论:Signing product flavors with gradle。简而言之,我必须在 buildTypes 中添加 signingConfig null 因为 Android 添加了一些默认签名配置。即使我试图覆盖它。基于我的问题的完整示例:

...
signingConfigs {
    release {
        storeFile file("../config/keystores/release_keystore")
        storePassword "mysecurepassword"
        keyAlias "myultrasecurealias"
        keyPassword "myreallysecurekeypassword"
    }

    debug {
        storeFile file("../config/keystores/debug.keystore")
        storePassword "mysecurepassword"
        keyAlias "myultrasecurealias"
        keyPassword "myreallysecurekeypassword"
    }

    other {
        storeFile file("../config/keystores/other")
        storePassword "mysecurepassword"
        keyAlias "myultrasecurealias"
        keyPassword "myreallysecurekeypassword"
    }
}

flavorDimensions "dim"

productFlavors {
    production {
        dimension "dim"
    }

    other {
        dimension "dim"
    }
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

        // this loop is a better implementation than my previous example
        productFlavors.all { flavor ->
                flavor.signingConfig signingConfigs.release
        }        
        productFlavors.other.signingConfig signingConfigs.other
    }

    debug {
        signingConfig null
        // this loop is a better implementation than my previous example
        productFlavors.all { flavor ->
                flavor.signingConfig signingConfigs.debug
        }
        productFlavors.other.signingConfig signingConfigs.other
    }
}