logo

Many times we come up with requirement to have multiple version of the same project. For example, we are developing application which have two versions, Pro version and Free version of the same app. We can duplicate the code and create two versions of the same app but in future while making updates, it will be difficult to maintain even if project is simple. So to avoid this we can use Product Flavors.

Product flavor defines a customized version of the application build by the project. A single project can have different flavors which change the generated application.

Defining them is simple, you just have to write following in build.gradle.

productFlavors {
     ...
    freeVersion {
        ...
    }

    proVersion {
        ...
    }
}

So in above example we have two flavor’s i.e freeVersion and proVersion. In freeVersion, we will display ads and in proVersion, we will hide the ads.

You can check it on Android Studio  under the Build Variants tab.

flavours

Note: You can have different package name, app name, app icons, java class files and different attributes for each of the product flavors.

Details of some are listed below:

Package Name

you can define package name to each flavor.
(Remember, your app is identified by package name on Google Play Store)

android{
productFlavors {
        freeVersion {
            applicationId "com.codetoart.android.freeVersion"
        }

        proVersion {
            applicationId "com.codetoart.android.proVersion"
        }
    }
}

App name

To define app name for each flavor, you can write it in following way

android{
productFlavors {
        freeVersion {
            applicationId "com.codetoart.android.freeVersion"
            resValue "string", "override_name", "Free Version"
        }

        proVersion {
            applicationId "com.codetoart.android.proVersion"
            resValue "string", "override_name", "Pro Version"
        }
    }
}

And in manifest under application tag, you just need to mention string name. i.e.

android:label="@string/override_name"

App icons

If you want to have different icons per flavor, you just have to define new directory structures for each of the flavor. In the above example we have defined two flavors freeVersion and proVersion, so we need to create two directory structures for same under app/src/ directory. So we can define required resources for these flavors. Like,
structure

 

Java class file

We can also use java classes to build different versions. To do so create a new folder named java with a packageName com.codetoart.android.flavor.util with a file named Constants in both flavors as shown below.
java

In the class Constants on flavor freeVersion, put the following code:

        /******** APP TYPE***********/
	public static final String PARAM_APP_TYPE="Free Version";

In the class Constants on flavor proVersion, put the following code:

        /******** APP TYPE***********/
	public static final String PARAM_APP_TYPE="Pro Version";

And in MainActivity, write the following code which displays the type of app:

Log.d("****App type",""+Constants.PARAM_APP_TYPE);

Run the app and check logcat, what it logs. And don’t forget to change build variant i.e freeVersion or proVersion 🙂

This way we can add more resources, source files to respective flavor directories according to need.

Simple? As this  is simple way to have multiple versions/flavors of single code base.  🙂

Don’t forget to add your queries/comments. 🙂

AUTHOR: Vikas Hiran
2 Comments
  • mkp

    Good article.

    May 17, 2017

Leave a Comment

Your email address will not be published.