Building Android Application with different productFlavors

02 / Sep / 2014 by Navkrishna . 3 comments

Applications are developed using some arbitrary values for development and QA. But, when it comes to production or releasing a new update for app, we need to change those arbitrary values to actual values.

For instance, take an application using web services, which requires some input from user and store it in database. During development and testing, many valid but arbitrary values are provided. And, we don’t want those values inside production database. So, an option is to keep different URL for different purposes.

But, there is another problem. We need to update our application (web service url) each time we create a build for development, QA and production.

For this type of problem productFlavors comes to our rescue.

In Android Studio project, open build.gradle of the module.

[code]<project-name>/<module-name>/build.gradle[/code]

In it, inside android block, add productFlavors.

[code]android {

productFlavors {

Development {

}

QA {

}

Production {

}

}

}[/code]

Now, create three directories inside
[code]<project-name>/<module-name>/src/Development
<project-name>/<module-name>/src/QA
<project-name>/<module-name>/src/Production[/code]

Based on the requirements, create java and res directories inside all three. If you don’t need any resource specific to the flavors then skip creating res.

Now, create package inside each java and keep the java code there which are specific to that flavor.

I have uploaded a demo application project on GitHub FlavourUsage. Clone, fork, modify and create your own flavor specific application.

FOUND THIS USEFUL? SHARE IT

comments (3)

Leave a Reply

Your email address will not be published. Required fields are marked *