Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple signingConfigs for multiple variants

How can I set different signing configs for different variants?

For instance, we currently have the buildtypes Debug/Beta/Release with 2 flavors, free and paid, resulting in 6 variants. To make it a bit easier, let's forget the Debug variants and only focus on freeBeta/paidBeta/freeRelease/paidRelease.

What I'd like, is for each variant to use a separate different signingConfig.

So far the only solutions I could find is either putting the signingConfigs in the buildTypes so all Beta variants would have the same signingConfigs:

buildTypes {
    beta {
        signingConfigs.beta
    }
    release {
        signingConfigs.release
    }
}

Alternatively, using the flavors, in which case all free variants would have the same signingConfigs:

productFlavors {
    free {
        signingConfig signingConfigs.free
        applicationId 'com.example.free'
    }
    paid {
        signingConfig signingConfigs.paid
        applicationId 'com.example.paid'
    }
}

Is there a way to do this in the current productFlavor closure? Can this only be fixed by overridding the android.applicationVariants.all { variant -> and manually applying a signingConfig for each application variant based on some naming scheme or some other ugly hack?

I also found this answer, but it doesn't appear to work in the latest build tools; when compiling I get the following error:

FAILURE: Build failed with an exception.

  • Where: Build file '/home/dev/projects/app/build.gradle' line: 61

  • What went wrong: A problem occurred evaluating project ':app'.

    Could not find property 'free' on ProductFlavor container.

like image 228
Aegis Avatar asked Sep 23 '15 14:09

Aegis


People also ask

What are build variants?

Build variants are the result of Gradle using a specific set of rules to combine settings, code, and resources configured in your build types and product flavors. Although you do not configure build variants directly, you do configure the build types and product flavors that form them.

How to add new build variant android?

NOTE: By default, the Android Studio will generate "debug" and "release" Build Types for your project. So, to change a Build Type, all you need to do is just select your Build Type from the Build Variant and after the project sync, you are good to go.

What is buildtype in Gradle?

A build type determines how an app is packaged. By default, the Android plug-in for Gradle supports two different types of builds: debug and release . Both can be configured inside the buildTypes block inside of the module build file.


2 Answers

The answer linked is actually working fine. I got it to compile like this (with buildTools 1.3.1 and gradle-wrapper 2.7). The error you were having (Could not find property "free" on ProductFlavor container) is most certainly due to the fact that your build types are defined before the productFlavors in your build.gradle

This won't work

signingConfigs {
    freeBeta {}
    freeRelease {}
    paidBeta {}
    paidRelease {}
}    
buildTypes {
    debug {}
    beta {}
    release {}
}
productFlavors {
    free {}
    paid {}
}

This would work (simply swapping the definition order of the productFlavors and buildType)

signingConfigs {
    freeBeta {}
    freeRelease {}
    paidBeta {}
    paidRelease {}
}    
productFlavors {
    free {}
    paid {}
}    
buildTypes {
    debug {}
    beta {}
    release {}
}

Here's a full working example:

signingConfigs {
        freeBeta {
            keyAlias 'freeBeta'
            keyPassword 'test'
            storeFile file('C:/keystore.jks')
            storePassword 'test'
        }
        freeRelease {
            keyAlias 'freeRelease'
            keyPassword 'test'
            storeFile file('C:/keystore.jks')
            storePassword 'test'
        }
        paidBeta {
            keyAlias 'paidBeta'
            keyPassword 'test'
            storeFile file('C:/keystore.jks')
            storePassword 'test'
        }
        paidRelease {
            keyAlias 'paidRelease'
            keyPassword 'test'
            storeFile file('C:/keystore.jks')
            storePassword 'test'
        }
    }

    productFlavors {
        free {

        }
        paid {

        }
    }

    buildTypes {
        debug {

        }
        beta {
            productFlavors.free.signingConfig signingConfigs.freeBeta
            productFlavors.paid.signingConfig signingConfigs.paidBeta
        }
        release {
            productFlavors.free.signingConfig signingConfigs.freeRelease
            productFlavors.paid.signingConfig signingConfigs.paidRelease                
        }
    }
like image 52
Eric Labelle Avatar answered Sep 28 '22 09:09

Eric Labelle


The https://stackoverflow.com/a/32810290/3961802 answer will not work.

    beta {
        productFlavors.free.signingConfig signingConfigs.freeBeta
        productFlavors.paid.signingConfig signingConfigs.paidBeta
    }
    release {
        productFlavors.free.signingConfig signingConfigs.freeRelease
        productFlavors.paid.signingConfig signingConfigs.paidRelease                
    }

In this case, the release build type will overwrite all flavors. So signing config for the freeBeta will be freeRelease.

At the moment, the only solution that I know is to sign all the build variants in a separate task.

signingConfigs {

    bananaDebug {}
    bananaBeta {}
    bananaRelease {}

    orangeDebug {}
    orangeBeta {}
    orangeRelease {}

    lemonDebug {}
    lemonBeta {}
    lemonRelease {}
}

productFlavors {

    banana {}

    orange {}

    lemon {}
}

buildTypes {

    debug {}

    beta {}

    release {}
}

applicationVariants.all {
    def flavorName = it.getFlavorName()
    def buildTypeName = it.buildType.name
    def buildVariantName = flavorName + buildTypeName.capitalize()
    def currentSigningConfig = signingConfigs.getByName(buildVariantName)

    it.mergedFlavor.signingConfig = currentSigningConfig
    // If you want to sign debug build
    buildTypes.debug.signingConfig currentSigningConfig
}
like image 34
Denys Makhov Avatar answered Sep 28 '22 09:09

Denys Makhov