We always need to setup debug, testing or release environments or variants while creating an android app. In a web application, we mostly have the option to define environment configuration variables. In Android, you can also setup debug, release or other build types and their parameters. The app will have the appropriate set of parameters depending on the build variant.
Suppose you want to use a different set of values for the debug, beta and release builds in android. For example, server address or some update interval need to be different in debug/production environment. The best way to do so is, setup debug & release parameters once for each build type in build.gradle file and forget afterwards.

So best and easy! Just call the parameter anywhere in the code and the app would have only values related to specific build always. Let us know how can we do this magical & productive setup.
Setup Debug & Release Environments in Android App
It’s time to do it. Open the build.gradle file of app module (not the one of project gradle). This file is also used to setup product flavour and dependencies and resides in the project -> app folder. Among the other constants, create buildConfigField
and specify what you need. Below is how I setup necessary constants for the debug, release and beta build types or environments.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | android { compileSdkVersion 23 buildToolsVersion "23.0.2" defaultConfig { applicationId "com.fellow.tuts" minSdkVersion 15 targetSdkVersion 23 versionCode 32 versionName "1.0.0" } buildTypes { debug { buildConfigField('long', 'UPDATE_INTERVAL', '1000') buildConfigField('String', 'AUTH', 'ThisIsCredentIALforDebug123') buildConfigField('String', 'ENDPOINT', '"http://192.168.1.123:8080/ftapp"') ext.enableAnalytics = false } release { buildConfigField('long', 'UPDATE_INTERVAL', '1800000') buildConfigField('String', 'AUTH', 'ThisIsCredentIALforRelease456') buildConfigField('String', 'ENDPOINT', '"http://fellowtuts.com/ftapp"') minifyEnabled true shrinkResources true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } beta { initWith release buildConfigField('String', 'VERSION', 'V2') buildConfigField('String', 'AUTH', 'ThisIsCredentIALforBeta789') } } productFlavors { } } dependencies { ... } |
Note a few things:
buildConfigField
uses the format: data type, constant name, value. All must be enclosed within single quotes. If the data type is a string, the value must be quoted inside double quotes first.
You can use already declared build variant for another build type and assign a new value for a specific constant as well. As I did to set up beta build using release variant and with help of initWith option.
Do you need to set up a static IP address for localhost as I have in debug environment (192.168.1.123
)? Check the article: Connect Android App to Localhost Web Server.
Use Debug or Release Build Constants We Have Setup
Now you just need to call the constant and the value from the corresponding build or environment will be used. See the example code below to use them:
1 2 3 4 5 6 7 8 9 10 11 | protected void createLocationRequest() { mLocationRequest = new LocationRequest(); final long UI = BuildConfig.UPDATE_INTERVAL; mLocationRequest.setInterval(UI) .setFastestInterval(UI / 2) .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) .setSmallestDisplacement(1000); mGoogleApiClient.connect(); } |
Notice the line no. #4. We have called here BuildConfig
configuration parameter and UPDATE_INTERVAL
constant. No need to conditionally check which environment or build is in use and setup values.
If I’m using debug variant, the value will be 1000 else it would be 1800000. So simple and best, when you setup debug & release environments in build.gradle file of your android app.
Conditionally Checking Build Type in Android
There might be the case when you need to check which build type is it and update the code accordingly. For example in beta variant I need to manipulate the code and pass the value of VERSION
constant:
1 2 3 4 5 6 | private void getData (Map<String, Object> params, String str) { if(BuildConfig.BUILD_TYPE.equals("beta")){ params.put("version", BuildConfig.VERSION); } ... } |
It’s how you can setup debug & release environments in your android app. The best is that you don’t need to check or conditionally specify values for different build variants here and there. The Android will set up correct constants for you in debug/release build types each in the app.