Search code examples
androidgradlebuildbuild.gradlecode-signing

Signing product flavors with gradle


I am tyring to migrate my projects to gradle. One of my projects has multiple product flavors and each one of them has to be signed with a different signingConfig in its release version. So this is what I tried so far:

buildscript {
    ...
}

apply plugin: 'android'

android {
    compileSdkVersion 17
    buildToolsVersion '17'

    signingConfigs {
        flavor1 {
            storeFile file("keystore")
            storePassword "secret"
            keyAlias "aliasForFlavor1"
            keyPassword "secretFlavor1"
        }

        flavor2 {
            storeFile file("keystore")
            storePassword "secret"
            keyAlias "aliasForFlavor2"
            keyPassword "secretFlavor2"
        }
    }

    productFlavors {
        flavor1 {
            signingConfig signingConfigs.flavor1
        }

        flavor1 {
            signingConfig signingConfigs.flavor2
        }
    }
}

dependencies {
    ...
}

When I run gradle build I get a groovy.lang.MissingFieldException and the following error message:

No such field: signingConfigs for class: com.android.build.gradle.internal.dsl.GroupableProductFlavorFactory

So I assume the productFlavors.* part of the Gradle script is not the right place to put code signing configurations.


Solution

  • Per the user guide, signingConfigs for flavors are supported.

    The problem here has to do with the scope of the signingConfigs object. I just assigned it to a variable inside the productFlavors block, but outside the flavor1 flavor block to fix the issue:

    productFlavors {
        def flavor1SigningVariable = signingConfigs.flavor1
    
        flavor1 {
            ...
            signingConfig flavor1SigningVariable
            ...
        }