Get product flavor or build variant in an android app
android, gradle
Solution
Edit: This is now done automatically starting with version 0.7 of the plugin. `BuildConfig.java` will contains
public static final String BUILD_TYPE = "debug";
public static final String FLAVOR = "f1Fa";
public static final String FLAVOR_group1 = "f1";
public static final String FLAVOR_group2 = "fa";
`FLAVOR` is the combined flavor name with all flavor dimensions/groups applied. Then each dimension is available as `FLAVOR_<group name>`.
Finally `BUILD_TYPE` contains the name of the used build type.
If you have a single flavor dimension, then you only have the `FLAVOR` constant.
This is the only way to do it right now.
One thing you could do is automate it. So you could do something like this:
android {
productFlavors.whenObjectAdded { flavor ->
flavor.buildConfig "public static final String PRODUCT_FLAVOR = \"${flavor.name}\";"
}
// your normal config here.
}
This way you don't have to manually do it for each flavor.
Problem
I have an android app with a number of different product flavors configured in my build.gradle file eg ``` productFlavors { someFlavor {} anotherFlavor {} } ``` In my application code, I want to be able to get hold of the name of the currently compiled flavor (or build variant). One solution is this: ``` productFlavors { someFlavor { buildConfig "public static final String PRODUCT_FLAVOR = \"someFlavor\";" } anotherFlavor { buildConfig "public static final String PRODUCT_FLAVOR = \"anotherFlavor\";" } } ``` And then in my android app call `BuildConfig.PRODUCT_FLAVOR`. Is there some way I can get gradle to do this automatically? Or is there some other API in android I can use to get the product flavor name?