我正在尝试根据flavor + buildType定义一个buildConfigVariable。理想情况下,这就是我想要的
productFlavors {
strawberry {
buildConfigField "String", "WS_API_KEY", name + variant.buildType.name
}
... more flavors ..
}
name确实包含“草莓”,但我不知道是否可以访问variant的buildType。
放置在Android闭包之外,我确实可以访问BuildType和variant,但是我无法调用buildConfigField
android.applicationVariants.all { variant ->
println "****************************"
println "variant: ${variant.name}"
println "flavor: ${variant.flavorName}"
println "****************************"
if (variant.buildType.name == 'release') {
if (variant.flavorName == 'strawberry') {
buildConfigField "String", "WS_API_KEY", '"strawberry_release"'
} else {
buildConfigField "String", "WS_API_KEY", '"chocolate_release"'
}
} else if(variant.buildType.name == 'debug') {
if (variant.flavorName == 'strawberry') {
buildConfigField "String", "WS_API_KEY", '"strawberry_debug"'
} else {
buildConfigField "String", "WS_API_KEY", '"chocolate_debug"'
}
}
****************************
variant: strawberryRelease
flavor: strawberry
****************************
org.gradle.api.internal.MissingMethodException:
Could not find method buildConfigField()
for arguments [String, WS_API_KEY, "strawberry_release"]
我可以轻松地创建一个 Java 工厂并API_KEY
根据一些BuildConfig
常量返回适当的工厂,但我宁愿保持代码配置不可知。