March 14, 2016

Android Studio comes with a lot of features; in this tutorial I’ll explain you about the Product Flavors. We have seen many applications in the play store which are paid and free version. In both app the functionalities are almost same, like in paid version app there will be no ad, and in free version there will be ad. So, do the developers make different workspace for two different versions, well I don’t know about the previous projects. But now with the Android Studio we have one very useful feature called “Product Flavors”. In this we can have a base app, and we can make as many different versions of it as we want. First thing first in Android Studio everything is handled by the build.gradle file.

Let’s take a scenario, where in the app we have to change the graphics, app name and the package name of-course. Here comes the Product Flavors to the rescue.


defaultConfig {
        applicationId "com.techno.blog.post"
        minSdkVersion 13
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }

The above is the package name we are using in the project, now we will use the productFlavors.


productFlavors {
        paidVersion {
            applicationId = " com.techno.blog.post.paid"
        }
        freeVersion {
            applicationId = " com.techno.blog.post.free"
        }
    }

Above I’m defining two different package names. Now the next step is to make a directory in the app, with the exact name as you have provided in the productFlavors. In my case it is paidVersion and the freeVersion. According to our sceneario, only graphics, app name and the package name is changing. So, we will copy the res folder and the values folder then paste it in the respected directories(paidVersion, and the freeVersion). If at any point the code is also changing then we will copy only that particular java code and will paste it.
While running the app, we have to select the build variants, selct the required variant and run it. It will run like a charm. Try it out guys.
Happy Coding.