Android Specific Gradle产品风味组合

Nig*_*len 9 android gradle

在我的代码中,我有一些模板都来自一个代码库.对于每个模板,我想添加特定尺寸.使用风味尺寸和产品口味我已经达到了这个代码:

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"
    }    
Run Code Online (Sandbox Code Playgroud)

这给出了结果(我忽略了buildtypes):

templateABlue

templateAGreen

templateAOrange

templateBBlue templateBGreen

templateBOrange templateCBlue

templateCGreen

templateCOrange

当然这是预期的行为,但我希望实现这样的目标:

templateA

templateBBlue

templateBOrange

templateC

templateCGreen

因此,每个模板源自一个主代码库,并且每个模板可以具有源自其代码库的不同变体.有没有办法指定可以使用哪种风味尺寸组合或者排除我不想要的组合?需要明确的是,每个模板都可以在不指定颜色的情况下运行.

我希望我的问题很明确.先感谢您.

小智 7

您可以使用gradle variantFilter排除某些配置

例如:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)