为特定的风格和buildType定义buildconfigfield

我有两种口味,可以说是香草和巧克力。 我也有debugging和发布构buildtypes,我需要香草发布有一个字段为真,而其他3个组合应该是假的。

def BOOLEAN = "boolean" def VARIABLE = "VARIABLE" def TRUE = "true" def FALSE = "false" VANILLA { debug { buildConfigField BOOLEAN, VARIABLE, FALSE } release { buildConfigField BOOLEAN, VARIABLE, TRUE } } CHOCOLATE { buildConfigField BOOLEAN, VARIABLE, FALSE } 

我有一个错误,所以我想debugging和释放技巧不起作用。 有可能做到这一点?

Solutions Collecting From Web of "为特定的风格和buildType定义buildconfigfield"

循环变体并检查他们的名字:

 productFlavors { vanilla {} chocolate {} } applicationVariants.all { variant -> println("Iterating variant: " + variant.getName()) if (variant.getName() == "chocolateDebug") { variant.buildConfigField "boolean", "VARIABLE", "true" } else { variant.buildConfigField "boolean", "VARIABLE", "false" } } 

这是一个没有缺失的解决scheme,我在Simas的回答下描述过

 buildTypes { debug {} release {} } productFlavors { vanilla { ext { variable = [debug: "vanilla-debug value", release: "vanilla-release value"] } } chocolate { ext { variable = [debug: "chocolate-debug value", release: "chocolate-release value"] } } } applicationVariants.all { variant -> def flavor = variant.productFlavors[0] variant.buildConfigField "boolean", "VARIABLE", "\"${flavor.ext.variable[variant.buildType.name]}\"" } 

在Gradle构build系统中,不幸的是buildTypesproductFlavors是两个独立的实体。

据我所知,为了完成你想要达到的目标,你需要创build另一种构build风格:

 buildTypes { debug{} release {} } productFlavors { vanillaDebug { buildConfigField BOOLEAN, VARIABLE, FALSE } vanillaRelease { buildConfigField BOOLEAN, VARIABLE, TRUE } chocolate { buildConfigField BOOLEAN, VARIABLE, FALSE } } 

以下是我解决这个问题的方法:

 def GAME_DIMENSION = "game" def BUILD_DIMENSION = "building" flavorDimensions GAME_DIMENSION, BUILD_DIMENSION productFlavors { lollipop { dimension BUILD_DIMENSION minSdkVersion 21 } normal { dimension BUILD_DIMENSION } game_1 { dimension GAME_DIMENSION ext { fields = [ [type: 'String', name: 'TESTSTRING', values: [debug: 'debugstring', release: 'releasestring']], [type: 'int', name: 'TESTINT', values: [debug: '1234', release: '31337']] ] } } game_2 { dimension GAME_DIMENSION ext { fields = [] // none for game_2 } } } applicationVariants.all { variant -> // get the GAME dimension flavor def game = variant.getProductFlavors() .findAll({ flavor -> flavor.dimension == GAME_DIMENSION}) .get(0) println "Adding " + game.ext.fields.size() + " custom buildConfigFields for flavor " + variant.name // loop over the fields and make appropriate buildConfigField game.ext.fields.each { field -> def fldType = field['type'] def fldName = field['name'] def fldValues = field['values'] // get debug/release specific value from values array def fldSpecificValue = fldValues[variant.getBuildType().name] // add quotes for strings if (fldType == 'String') { fldSpecificValue = '\"' + fldSpecificValue + '\"' } println " => " + fldType + " " + fldName + " = " + fldSpecificValue variant.buildConfigField fldType, fldName, fldSpecificValue } } 

(我还没有能够确定ext.fields是否存在一种风味)

 productFlavors { vanilla {} chocolate {} } buildTypes { release { productFlavors.vanilla { //your configuration for vanilla flavor with release buildType } } debug { productFlavors.chocolate{ //your configuration for chocolate flavor with debug buildType } } }