How to Set Up Build Environment In Android

If you want to build an android APK using development and production URL. Then you need not change your URL manually every time. This can be possible by following some simple steps.

It would be very nice, If the developer only needs to type some command on android terminal and it will generate the required APK.

For achieving this requirement:

  1. First of all, Create 2 files under the app folder of your project say development.props and production.props. Or you can add these 2 files in a package under app folder say config.

Basically, these 2 files contain keys and values. This key is same for both files. But their values are different. These files contain one key say “SERVER_URL” and its value. It would be written like this :


In this case, only URL is different. So, I have added only one key-value pair in Props file. You can add more.

2. Then, create ProductFlavours in the app build.gradle file say development and production. Now, access different props files containing URLs in their correseponding flavours like this:

productFlavors {

development {
getProps('./config/development.props').each { p ->
buildConfigField 'String', p.key, p.value
production {
getProps('./config/production.props').each { p ->
buildConfigField 'String', p.key, p.value
def getProps(path) {
Properties props = new Properties()
props.load(new FileInputStream(file(path)))
return props

In the productFlavours, this snippet is used.

getProps('./config/development.props').each { p ->
buildConfigField 'String', p.key, p.value

It means : First of all, access required .props file using getProps() and In this, each key value content will be considered as a string and it will be access using “BuildConfigField.{key}”. In my case I can access this key in the code using BuildConfigField.SERVER_URL.

Now, For each flavour, there is a build type And this BuildType is added in app build.gradle. For example, Build type is Debug and release. And I have two flavours ie. development and production. So, gradle task will be created using both flavour and build type like this:


Now, you need to type these commands only. It would generate required APK with its corresponding URL. Commands are:

  1. ./gradlew assembleProductionRelease : It would generate release build with Production URL.
  2. ./gradlew assembleDevelopmentDebug : It would generate debug build with Development URL.
  3. ./gradlew assembleProductionDebug : It would generate debug build with Production URL.
  4. ./gradlew assembleDevelopmentRelease : It would generate release build with development URL.

Top three gradle task would be very helpful. But the last task would generate Release build with development URL. But this is not recommended. So, we should stop developer to execute this task ie. “./gradlew assembleDevelopmentRelease

Now To restrict developer to generate release build using Development URL, add this snippet in your app build.gradle file:

android.variantFilter { variant ->
&& variant.getFlavors().get(0).name.equals('development')) {

Now, If we try to execute task ie. “./gradlew DevelopmentRelease”. Gradle would stop generating the build and throw exception and would say : This task assembleDevelopmentRelease is not found in the root project.