Many times we come up with a requirement to have multiple versions of the same project. For example, we are developing an application which has 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 the future while making updates, it will be difficult to maintain even if the 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 that change the generated application.
Defining them is simple, you just have to write following in build.gradle
.
productFlavors {
…
freeVersion {
…
}
proVersion { … } }
So in the 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.
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 names 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 the 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 the same under app/src/directory
. So we can define the required resources for these flavors. Like,
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.
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? This is a simple way to have multiple versions/flavors of single code base. 🙂
Don’t forget to add your queries/comments. 🙂