Setup Debug & Release Environments in Android App – The Best Way

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.

Setup Debug & Release Environments in Android App

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.

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 ( 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:

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:

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.

You Might Interested In

Leave a Reply

Enclose a code block like: <pre><code>Your Code Snippet</code></pre>.