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

815 views Asked by At

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?

1

There are 1 answers

0
Serj On

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.