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.
There's a class called BuildConfig. java which is automatically generated by the build system. This class is updated automatically by Android's build system (like the R class). It already contains a static final boolean called DEBUG, which is normally set to true.
Product flavours lets you create multiple variants of an android app while using a single codebase. To create product flavours you need to define rules in the build.
Build types: In Android apps, build types usually refer to the environment in which you're testing. By default, when you create an app in Android Studio from any of the templates, you get two build types: debug and release.
What is BuildConfig? Gradle generates a BuildConfig class that contains static configuration constants that are specific to the build at build time. The class includes default fields such as debug and flavor, but you can override them with build.
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"'
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With