Q:

Get product flavor or build variant in an android app

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?

A:

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.

A:

The accepted solution is awesome. For some cases, an alternate may be useful though. 
You can add a config file, say 'Appconfig.java' to your productFlavor specific source folders i.e. src/<productflavor>/java/<packagename>/Appconfig.java; you can then use Appconfig.PRODUCT_FLAVOR in your app. The compiler will keep you honest and make sure you add this for all flavors. 
Like I said, in this particular case, the accepted solution is probably better since you don't even need to add anything for new productFlavors but this approach might be useful for other cases.

