I'd like to get the build variant during runtime, is this possible without any extra config or code?
5 Answers
Look at the generated BuildConfig
class.
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "com.example.app";
public static final String BUILD_TYPE = "debug";
public static final String FLAVOR = "";
public static final int VERSION_CODE = 1;
public static final String VERSION_NAME = "";
}
-
So if your app hasn't been built yet, this class wouldn't exist and using the class before it gets built would cause a compile error, wouldn't it? Isn't this the chicken vs. egg scenario?– JohannSep 5, 2015 at 19:35
-
14BuildConfig is generated during a Gradle Sync, not the building of the app itself. Sep 5, 2015 at 23:30
-
9Friendly reminder to import the BulidConfig for your app package instead of from an external library BuildConfig...as I did... Nov 23, 2016 at 23:26
-
3
-
@ashishduh "BuildConfig is generated during a Gradle Sync, not the building of the app itself." Nope, that's not right. Just clean the project and you'll notice
BuildConfig
is gone. It is called BuildConfig for a reason– FaridDec 25, 2020 at 16:37
Another option would be to create a separate build config variable for each build variant and use it in your code like this:
In your build.gradle file:
productFlavors {
production {
buildConfigField "String", "BUILD_VARIANT", "\"prod\""
}
dev {
buildConfigField "String", "BUILD_VARIANT", "\"dev\""
}
}
To use it in your code:
if (BuildConfig.BUILD_VARIANT.equals("prod")){ // do something cool }
Here is an example to define and get BuildConfig
for different flavor
android {
defaultConfig {
...
buildTypes {
...
}
flavorDimensions "default"
productFlavors {
develop {
applicationIdSuffix ".dev"
versionNameSuffix "-dev"
}
staging {
applicationIdSuffix ".stg"
versionNameSuffix "-stg"
}
production {
applicationIdSuffix ""
versionNameSuffix ""
}
}
applicationVariants.all { variant ->
def BASE_URL = ""
if (variant.getName().contains("develop")) {
BASE_URL = "https://localhost:8080.com/"
} else if (variant.getName().contains("staging")) {
BASE_URL = "https://stagingdomain.com/"
} else if (variant.getName().contains("production")) {
BASE_URL = "https://productdomain.com/"
}
variant.buildConfigField "String", "BASE_URL", "\"${BASE_URL}\""
}
}
Using
BuildConfig.BASE_URL
You can try with
getPackageName();
it will return what you've defined in build.gradle
productFlavours{
flavour1{
applicationId 'com.example.package.flavour1'
}
flavour2{
applicationId 'com.example.package.flavour2'
}
}
If you are already flavoring then no need to provide extra string field in your gradle. Just follow simple steps to get the build details:
For build variant : BuildConfig.FLAVOR
For build version code : BuildConfig.VERSION_CODE
For build version name : BuildConfig.VERSION_NAME
-
3
BuildConfig.BUILD_TYPE
should be used for the build variant.BuildConfig.FLAVOR
should be used for any flavors that you have specified.– EdricSep 9, 2019 at 13:27
if (BuildConfig.DEBUG) {}