Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Specific Gradle product flavor combinations

Tags:

android

gradle

In my code I have certain templates all deriving from one code base. For each template I want to add specific dimensions. Using flavor dimensions and product flavors I have reached this code:

flavorDimensions "template", "color"

productFlavors {

    templateA {
        applicationId "com.templatea"
        versionCode 1
        versionName "1.0.0"

        flavorDimension "template"
    }


    templateB {
        applicationId "com.templateb"
        versionCode 1
        versionName "1.0.0"

        flavorDimension "template"
    }

    templateC {
        applicationId "com.templatec"
        versionCode 1
        versionName "1.0.0"

        flavorDimension "template"

    }

    blue {
        applicationId "com.blue"
        versionCode 1
        versionName "1.0.0"

        flavorDimension "color"
    }

    green {
        applicationId "com.green"
        versionCode 1
        versionName "1.0.0"

        flavorDimension "color"
    }

    orange {
        applicationId "com.orange"
        versionCode 1
        versionName "1.0.0"

        flavorDimension "color"
    }    

Which gives the result (I have ignored the buildtypes):

templateABlue

templateAGreen

templateAOrange

templateBBlue templateBGreen

templateBOrange templateCBlue

templateCGreen

templateCOrange

Of course this is the expected behaviour but I would like to achieve something like this:

templateA

templateBBlue

templateBOrange

templateC

templateCGreen

Thus each template derives from one main code base and each template can have different variants deriving from their code base. Is there a way to specify which flavor dimension combinations can be used or a way to exclude the combinations I don't want? Just to be clear, each template can function without specifying a color.

I hope my question is clear. Thank you in advance.

like image 514
Nigel Heylen Avatar asked Feb 05 '15 10:02

Nigel Heylen


1 Answers

You can use gradle variantFilter to exclude some configurations

For example:

productFlavors {

    templateB {
        applicationId "com.templateb"
        versionCode 1
        versionName "1.0.0"

        flavorDimension "template"
    }

    templateC {
        applicationId "com.templatec"
        versionCode 1
        versionName "1.0.0"

        flavorDimension "template"

    }

    blue {
        applicationId "com.blue"
        versionCode 1
        versionName "1.0.0"

        flavorDimension "color"
    }
}

android.variantFilter { variant ->
    if(variant.getFlavors().get(0).name.equals('templateC')
            && variant.getFlavors().get(1).name.equals('blue')) {
        variant.setIgnore(true);
    }
}
like image 98
Vincent NOCK Avatar answered Oct 19 '22 19:10

Vincent NOCK