简体   繁体   中英

Android Specific Gradle product flavor combinations

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.

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);
    }
}

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