Android特定的Gradle产品风味组合 [英] Android Specific Gradle product flavor combinations
问题描述
在我的代码中,我有一些从一个代码库派生的模板。对于每个模板,我想添加特定的尺寸。使用风味尺寸和产品风味我已经达到了这个代码:
flavorDimensionstemplate,color
productFlavors {
$ b $ templateA {
applicationIdcom.templatea
versionCode 1
versionName1.0.0
flavorDimension模板
templateB {
applicationIdcom.templateb
versionCode 1
versionName1.0.0
flavorDimensiontemplate
}
templateC {
applicationIdcom.templatec
versionCode 1
versionName1.0.0
flavorDimensiontemplate
}
blue {
applicationIdcom.blue
versionCode 1
versionName1.0.0
flavorDimensioncolor
}
green {
applicationIdcom.green
versionCode 1
versionName1.0.0
flavorDimensioncolor
}
orange {
applicationIdcom.orange
versionCode 1
versionName1.0.0
flavorDimensioncolor
}
给出结果(我忽略了构建类型):
templateABlue
templateAGreen
templateAOrange
templateBBlue
templateBGreen
templateBOrange templateCBlue
templateCGreen
templateCOrange
当然,这是预期的行为,但我想达到这样的效果:
templateA
templateBBlue
templateBOrange
templateC
templateCGreen
因此,每个模板都是从一个主代码库派生出来的,每个模板可以有不同的变体派生自它们的代码库。有没有方法可以指定哪些味道维度组合可以使用或排除我不想要的组合?要明确,每个模板都可以在没有指定颜色的情况下运行。
我希望我的问题很清楚。您可以使用gradle variantFilter
来排除这个问题。
例如:
productFlavors {
templateB {
applicationIdcom.templateb
versionCode 1
versionName1.0.0
flavorDimensiontemplate
}
templateC {
applicationIdcom.templatec
versionCode 1
versionName1.0.0
flavorDimensiontemplate
}
blue {
applicationIdcom.blue
versionCode 1
versionName1.0.0
flavorDimensioncolor
}
}
android.variantFilter {variant - > ()。get(0).name.equals('templateC')
&&& variant.getFlavors()。get(1).name.equals('blue ')){
variant.setIgnore(true);
}
}
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);
}
}
这篇关于Android特定的Gradle产品风味组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!