为特定风格和 buildType 定义 buildconfigfield
Define buildconfigfield for an specific flavor AND buildType
我有 2 种口味,比如说香草味和巧克力味。我也有 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
}
我有一个错误,所以我猜调试和发布技巧不起作用。可以这样做吗?
在 Gradle 构建系统中,buildTypes
和 productFlavors
很遗憾是两个独立的实体。
据我所知,要完成您想要实现的目标,您需要创建另一种构建风格:
buildTypes {
debug{}
release {}
}
productFlavors {
vanillaDebug {
buildConfigField BOOLEAN, VARIABLE, FALSE
}
vanillaRelease {
buildConfigField BOOLEAN, VARIABLE, TRUE
}
chocolate {
buildConfigField BOOLEAN, VARIABLE, FALSE
}
}
循环变体并检查它们的名称:
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"
}
}
这是我解决这个问题的方法:
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
}
}
}
这是我在
下描述的没有缺点的解决方案
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]}\""
}
对于您的具体情况,您也可以只使用 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-release。
您可以尝试多种产品口味:
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]}"
}
@Simas 答案是正确的,但是用 switch case 看起来会好一点:
android {
defaultConfig {
...
}
buildTypes {
debug {
...
}
release {
...
}
}
flavorDimensions "type"
productFlavors {
vanilla {
dimension "type"
...
}
chocolate {
dimension "type"
...
}
}
applicationVariants.all { variant ->
switch (variant.getName()) {
case "vanillaDebug":
variant.buildConfigField 'String', 'SDK_API_KEY', "\"$vanillaSdkApiKeyDebug\""
break
case "vanillaRelease":
variant.buildConfigField 'String', 'SDK_API_KEY', "\"$vanillaSdkApiKeyRelease\""
break
case "chocolateDebug":
variant.buildConfigField 'String', 'SDK_API_KEY', "\"$chocolateSdkApiKeyDebug\""
break
case "chocolateRelease":
variant.buildConfigField 'String', 'SDK_API_KEY', "\"$chocolateSdkApiKeyRelease\""
break
default:
throw new GradleException("The values are unknown for variant: ${variant.getName()}")
break
}
}
}
我有 2 种口味,比如说香草味和巧克力味。我也有 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
}
我有一个错误,所以我猜调试和发布技巧不起作用。可以这样做吗?
在 Gradle 构建系统中,buildTypes
和 productFlavors
很遗憾是两个独立的实体。
据我所知,要完成您想要实现的目标,您需要创建另一种构建风格:
buildTypes {
debug{}
release {}
}
productFlavors {
vanillaDebug {
buildConfigField BOOLEAN, VARIABLE, FALSE
}
vanillaRelease {
buildConfigField BOOLEAN, VARIABLE, TRUE
}
chocolate {
buildConfigField BOOLEAN, VARIABLE, FALSE
}
}
循环变体并检查它们的名称:
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"
}
}
这是我解决这个问题的方法:
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
}
}
}
这是我在
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]}\""
}
对于您的具体情况,您也可以只使用 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-release。
您可以尝试多种产品口味:
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]}"
}
@Simas 答案是正确的,但是用 switch case 看起来会好一点:
android {
defaultConfig {
...
}
buildTypes {
debug {
...
}
release {
...
}
}
flavorDimensions "type"
productFlavors {
vanilla {
dimension "type"
...
}
chocolate {
dimension "type"
...
}
}
applicationVariants.all { variant ->
switch (variant.getName()) {
case "vanillaDebug":
variant.buildConfigField 'String', 'SDK_API_KEY', "\"$vanillaSdkApiKeyDebug\""
break
case "vanillaRelease":
variant.buildConfigField 'String', 'SDK_API_KEY', "\"$vanillaSdkApiKeyRelease\""
break
case "chocolateDebug":
variant.buildConfigField 'String', 'SDK_API_KEY', "\"$chocolateSdkApiKeyDebug\""
break
case "chocolateRelease":
variant.buildConfigField 'String', 'SDK_API_KEY', "\"$chocolateSdkApiKeyRelease\""
break
default:
throw new GradleException("The values are unknown for variant: ${variant.getName()}")
break
}
}
}