Create multiple flavors of an Android app using gradle script

I’ve been asked sometimes on how to work with different hosts, different icons, or even different package names, depending on different versions of the same app.

There are lot of reasons to do this and one easy way to go: Product Flavors.

Product Flavor is a very powerful feature available in the Android gradle plugin that allows us to manage different “flavors” of an application.

In this blog, we are going to learn, how to design and build a single application with multiple flavors, which can be pushed to the Play Store and deployed on the same device simultaneously.

You can use the productFlavors closure of your app/build.gradle file to define different variants of your product.


productFlavors {

    ....

    free {
        applicationId 'com.example.app.free'
    }

    paid {
        applicationId 'com.example.app.paid'
    }

}

In addition to flavors, there is another very important concept regarding building android apps, called “build types”.


buildTypes {

        release {

            minifyEnabled false
            debuggable false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

            signingConfig signingConfigs.release

        }

        debug {

            minifyEnabled false
            debuggable true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

            applicationIdSuffix ".debug"

        }
}

Once the changes have been made on app/build.gradle file, a yellow warning bar will appear across the top of the editor indicating that the changes to the Gradle build file need to be synchronized with the rest of the project. Click on the Sync Now link located in the warning panel to perform the synchronization.

screen-shot-2016-11-09-at-11-56-00-am

Before proceeding to the next step, open the Build Variants tool window either using the quick access menu located in the status bar in the bottom left hand corner of the Android Studio main window or using the Build Variant tool window bar. Once loaded, clicking in the Build Variant cell for the app module should now list the four build variants:

Providing Alternate Resources

Now we’re going to start customizing the application per flavor, starting with resources. Initially the project structure looks like below:

In this structure, main is your default source directory (i.e. the “unflavored” source). So, where you would put your flavor customizations? in a flavored directory, that is.

Let’s say we want to provide a different launcher icon for both Free and Paid Flavor, so under the src folder we are actually going to create two folder/directory named free and paid. Then add different launcher icon for all flavors, also remember file name & directory structure should be same as main folder.

 

 

Flavor Specific Dependency

Adding “flavorCompile” syntax in dependency section of app level build.gradle adds the dependency for that particular flavor. Lets assume the app uses Google Admob for the ads.

 
dependencies {

    ....

    compile 'com.google.android.gms:play-services-ads:7.8.0' 

}

The disadvantage of this setup is that it will always pull in the admob library, including the premium version of the app, even though it doesn’t need the code at all!

In order to satisfy your paying customers and only pull the admob dependency for the free version of the app, change the build.gradle to this:

 
dependencies {

    ....

    freeCompile 'com.google.android.gms:play-services-ads:7.8.0' 

}

This tells gradle, that this dependency is only required for the productFlavor “free”. This also help to reduce .apk file size.

That’s it! You won’t have to do anything more! Gradle will automatically pull the dependencies for the selected productFlavors only.

You can also generate the .apk file using terminal by following command:

Syntax:-

 ./gradlew assemble(ProductFlavor)(buildType) 

eg:-

 ./gradlew assemblefreeDebug 

the complete source for the sample app built for this article is available on GitHub, and can be used by all.

There is a standard way of doing it but just like me I think lot of other devs are not aware of it. So I thought of putting this down concisely somewhere.

Happy coding :-).

Author: Shekhar Sahu

I am Chandrashekhar Sahu, working as Android application developer at Josh Software, Pune.

1 thought on “Create multiple flavors of an Android app using gradle script”

  1. Reblogged this on Josh Software – Where Programming is an Art! and commented:
    Product Flavor is a very powerful feature available in the Android gradle plugin that allows us to manage different “flavors” of an application.

    In this blog, we are going to learn, how to design and build a single application with multiple flavors, which can be pushed to the Play Store and deployed on the same device simultaneously.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s