我有两种口味,比如说香草和巧克力。我也有Debug和Release构建类型,我需要Vanilla Release才能使字段为true,而其他3种组合应该是false。
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
}
我有错误,所以我猜调试和发布技巧不起作用。有可能这样做吗?
答案 0 :(得分:88)
循环变体并检查其名称:
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"
}
}
答案 1 :(得分:17)
以下是我在Simas answer
下所描述的解决方案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.variable[variant.buildType.name]}\""
}
答案 2 :(得分:6)
在Gradle构建系统中,遗憾的是,buildTypes
和productFlavors
是两个独立的实体。
据我所知,要完成你想要实现的目标,你需要创建另一个构建版本:
buildTypes {
debug{}
release {}
}
productFlavors {
vanillaDebug {
buildConfigField BOOLEAN, VARIABLE, FALSE
}
vanillaRelease {
buildConfigField BOOLEAN, VARIABLE, TRUE
}
chocolate {
buildConfigField BOOLEAN, VARIABLE, FALSE
}
}
答案 3 :(得分:2)
以下是我如何解决这个问题:
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
)
答案 4 :(得分:1)
对于您的特定情况,您也可以只使用defaultConfig:
defaultConfig {
buildConfigField BOOLEAN, VARIABLE, TRUE
}
buildTypes {
debug {
buildConfigField BOOLEAN, VARIABLE, FALSE
}
release {
}
}
productFlavors {
VANILLA {
}
CHOCOLATE {
buildConfigField BOOLEAN, VARIABLE, FALSE
}
}
默认值为TRUE,但随后将FALSE应用于所有Debug版本和所有Chocolate版本。因此,唯一剩下的TRUE是VANILLA版本。
答案 5 :(得分:1)
您可以尝试多种口味的产品:
productFlavors {
demo {
applicationId "com.demo"
versionCode 1
versionName '1.0'
ext {
APP_BASE_URL = [debug: "${BASE_URL_DEV}", release: "${BASE_URL_PRODUCTION}"]
}
}
demo1 {
applicationId "com.demo1"
versionCode 1
versionName '1.2'
ext {
APP_BASE_URL = [debug: "${BASE_URL_DEV}", release: "${BASE_URL_PRODUCTION}"]
}
}
applicationVariants.all { variant ->
def flavor = variant.productFlavors[0]
variant.buildConfigField "String", "BASE_URL", "${flavor.ext.APP_BASE_URL[variant.buildType.name]}"
}
答案 6 :(得分:1)
@Simas Aswer是正确的,但在开关盒的情况下看起来可能会更好:
if(storeA)
{
storeA.DoSOmething();
}
else StoreB.DoSOmething();
答案 7 :(得分:-1)
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
}
}
}
答案 8 :(得分:-1)
我知道这是一个旧线程,但这是我搜索此问题时弹出的第一个线程。实际上,这与 targetSdkVersion 28
flavorDimensions "type"
productFlavors {
vanilla {
dimension "type"
applicationIdSuffix ".vanilla"
buildConfigField "boolean", "IS_VANILLA", true
resValue "string", "app_name", "Vanilla"
}
chocolate {
dimension "type"
applicationIdSuffix ".chocolate"
buildConfigField "boolean", "IS_VANILLA", false
resValue "string", "app_name", "Chocolate"
}
}