Building Android Application with different productFlavors

02 / Sep / 2014 by Navkrishna 5 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.

<project-name>/<module-name>/build.gradle

In it, inside android block, add productFlavors.

android {
...

    productFlavors {
        
        Development {
        
        }
        
        QA {
        
        }
        
        Production {
        
        }
        
    }

}

Now, create three directories inside

<project-name>/<module-name>/src/Development
<project-name>/<module-name>/src/QA
<project-name>/<module-name>/src/Production

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 (5)

    1. Navkrishna Post author

      Hi Assaf,
      From which step you are having trouble. Please provide details and your problems will be solved.

      Reply

Leave a comment -