简体   繁体   中英

Use different signature for Gradle product flavour and build type in Android

I have an Android build.gradle.kts with multiple flavours, multiple build types and different signing configs for the combinations, it looks like this:

    android {

        signingConfigs {
            create("lite-release") {
                storeFile = file("myLiteReleaseKey.keystore")
                storePassword = "litePassword"
                keyAlias = "MyLiteReleaseKey"
                keyPassword = "litePassword"
            }

            create("full-release") {
                storeFile = file("myFullReleaseKey.keystore")
                storePassword = "litePassword"
                keyAlias = "MyFullReleaseKey"
                keyPassword = "litePassword"
            }

            create("lite-alpha") {
                storeFile = file("myLiteAlphakey.keystore")
                storePassword = "password"
                keyAlias = "MyLiteReleaseKey"
                keyPassword = "password"
            }

            create("full-alpha") {
                storeFile = file("myFullAlphaKey.keystore")
                storePassword = "password"
                keyAlias = "MyFullReleaseKey"
                keyPassword = "password"
            }
        }

        buildTypes {
            getByName("release") {
                isMinifyEnabled = true
                isShrinkResources = true
            }

            create("alpha") {
                initWith(getByName("release"))

                versionNameSuffix = "-alpha"
                applicationIdSuffix = ".alpha"
            }
        }

        flavorDimensions += "app"
        productFlavors {

            create("lite") {
                dimension = "app"
            }

            create("full") {
                dimension = "app"
            }
        }
    }

Even if it was supposed to be something simple, I got stuck when trying to assign the signatures. It should be something like..

    productFlavors.getByName("lite") {
       buildTypes.getByName("release") {
            signingConfig = signingConfigs.getByName("lite-release")
       }
    }

    productFlavors.getByName("full") {
       buildTypes.getByName("release") {
            signingConfig = signingConfigs.getByName("full-release")
       }
    }

.. but when I build "liteRelease" it takes the last assignment, signature for "full-release" in the example above.

Can you please give me a hint to how I can do this properly?

In the end this did the job:

gradle.startParameter.taskNames.forEach {
    when {
        it.contains("LiteRelease", true) -> {
            productFlavors.getByName("lite") {
                signingConfig = signingConfigs.getByName("lite-release")
            }
        }
        it.contains("FullRelease", true) -> {
            productFlavors.getByName("full") {
                signingConfig = signingConfigs.getByName("full-release")
            }
        }
    }
 }

It is a bit error prone but I could not find something else that works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM