Build in different “flavors” for your app

Here’s a short, yet useful, post today. How can you have different “flavors”? For example, how can you release both a free and paid version of your app? It’s actually simple.

 

Add The Base Plugin

In both your project level and app level build.gradle files, add this line:

apply plugin:’base’

This plugin allows gradle to compile two versions of your app.

 

Declare Your Flavors

productFlavors {
free {
applicationId “com.podraza.android.gaogao.gaogao.free”
}
paid {
applicationId “com.podraza.android.gaogao.gaogao.paid”
}
}

Once your flavors are declared you can switch between them by clicking on the Build Variants tab on the left-hand side of Android studio.

 

Create Different Files For Each Flavor

Identify any file that needs to be different. Left click on the app’s base package and create a new version of that file. Now you’ll be able to select the flavor from the source set options.

For example, if I wanted to create a paid version of the MainActivity layout file, I would left click the base package. Then, choose a new android resource file. I would name the file the same name as in the free flavor and choose “paid” from the available source sets.

All done!

 

 

 

Leave a comment