Android Build Variants
One common problem that Android developers occasionally face is the pain of releasing more than one version of an app to the market. As a developer, you may want to have different versions of your app with some differences but at the same time you may not be so willing to create and maintain different projects. Being either free-paid versions or different flavors of an app as one of them is updated for its basic functionality, the other version or flavor of the app has to be updated properly.
A very rough solution to that problem is to maintain different projects for different versions and make the updates for each project. That is a painful and a kind of unnecessary process. There is an alternative way to solve this problem thanks to Gradle build system: Build Variants. By using Build Variants, more than one app can be handled through a single project. The rest of this article will be on how to create different flavors of an app, how to install and release them as different apps.
To better explain Build Variants, we will develop a celebrity app and generate different flavors of that app for different celebrity/mode options. The main functionality for the flavors of that app is same but the resources such as the images, colors and strings used in the app are different.
Project Setup
There is no special setup for this project but it is recommended to use the latest Android Studio and Gradle versions (For this project Android Studio 3.0.1 and Gradle version 4.1 are used). You can find the sample project used for this article on github.
At the end of this tutorial, there will be flavors for two different celebrities with Debug-Release and Free-Full combinations. The two flavors derived from one Android Studio project look like the following images.
Creating Flavors
First of all as seen from the final output of the flavors, background images, toolbar color and app names are different for two flavors. Some other resources including app icon are kept same which is not implicit and we will come to it later.
We first declare flavors in module gradle file. We can categorize flavors by using flavorDimensions depending on what type of flavor we want to produce. In our case we declared two flavors; content to specify different celebrities, mode for releasing the free or full version of the app.
The same declarations and any other flavor changes can be done through Build > Edit Flavors menu. Note that there are a variety of options for flavor customization such as App ID, Target/Min Sdk Version and Version Code/Name.
To build/run the flavor, a flavor can be chosen from the Build Variants menu located left-bottom of Android Studio.
Arranging Flavor Specific Resources
Next thing to do is assigning flavor specific resources. Normally resource folders are located under app\src\main\res folder. For each flavor, we can create subfolders under src folder. The folder structure for a multi-flavor app should look like the following.
The resources for each flavor should be located under the associated folder and the resources such as strings and colors can be flavor specific. For our sample app, each flavor has its own background image and primary color as well as flavor-specific app names. The resources that are located under flavor subfolders are used for that flavor and the resources that are not overridden by flavors are used as specified in the main flavor. For instance, the icon drawables are not overridden by the flavors which means although two flavors have different app names, they have same app icon.
As an alternative to placing the resources directly under the related folder, new flavor resource can be created from Android Studio by following File > New option and choose the right Target Source Set from the dropdown menu as shown.
Code Specification For Each Flavor
Android Studio automatically generates BuildConfig files for flavors. One of the BuildConfig class is as shown.
To run flavor specific code in app, BuildConfig class is to be used. Below code shows how to implement this feature. You can implement flavor related code within these conditions.
if(BuildConfig.FLAVOR.equals("whitneyhoustonFull")) {
...
}
else if(BuildConfig.FLAVOR.equals("elvispresleyDemo")) {
...
}
Generating Signed APK
App signing configuration for each flavor can be added or edited following Build > Edit Flavors menu.
If Build > Generate Signed APK menu is followed, the APK of the flavor that is being selected in Build Variants menu can be generated directly.
Conclusion
In this article we have explained how to implement and release various flavors of an app. Build Variants in Gradle build system is a great feature enabling us to create more than one flavor of an app. By using Build Variants many flavors can be maintained through one project and this gives developers a great flexibility.
The multi-flavor sample app used for this article can be found here.