I'm trying to define a buildConfigVariable depending on the flavor + buildType. Ideally, this is what I want
productFlavors {
strawberry {
buildConfigField "String", "WS_API_KEY", name + variant.buildType.name
}
... more flavors ..
}
name does contain "strawberry", but I don't know if it's possible to access the variant's buildType.
Placed outside the Android closure I do have access to the BuildType and variant, but then I can't invoke 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"]
I can easily create a Java factory and return the appropriate API_KEY
depending on some BuildConfig
constants, but I'd rather keep the code configuration agnostic.
Edit2: The version after 0.14.2 will allow doing this:
applicationVariants.all { variant ->
variant.buildConfigField "int", "VALUE", "1"
}
So you'd be able to do something like this (to match the original question):
applicationVariants.all { variant ->
variant.buildConfigField "String", "WS_API_KEY", variant.productFlavors.get(0).name + '_' + variant.buildType.name
}
Edit: it's not currently possible. The API is missing for this.
Bug: https://code.google.com/p/android/issues/detail?id=67416
Try this:
applicationVariants.all { variant ->
variant.mergedFlavor.buildConfigField "String", "NAME", '"VALUE"'
}