How to create Android Product Flavors and Build Variants

The Right Software > Blog > App Development > How to create Android Product Flavors and Build Variants
How to create Android Product Flavors and Build Variants

How to create Android Product Flavors and Build Variants

This is an advanced tutorial. We’ll teach you how to make Android product flavors programmatically using Android Studio and Gradle build tool. You can use this technique to create duplicate Android apps or Android app clones.

We often have to launch similar apps for different markets, countries or niches. Same Android mobile game can become different over holiday seasons to accommodate product placement or a social event. Similarly, due to copy rights we may have to make slight changes to an app before launching it in another country.

In such a case, there may be a strong inclination among the mobile app developer to create multiple versions from scratch or be willing to charge twice or thrice the original cost from the client. With Android product flavors, this cost can be saved and further invested in bringing more advanced app features or new concepts to market.

Android product flavors also known as Android build types or Android build variants are the native Android app development way to implement different versions of the same application with minor changes provided by Gradle and Android Studio together.  Here is the official Android documentation for configuration of Android product flavors.

 

What are Android product flavors?

Android product flavors are for different types of the same application with just configuration changes like staging and production environment.

By default there are two build types available in android studio project i.e.

  1. Debug
  2. Release

Android product flavor build types

Here in build types you can change the configuration like link for staging and production environment on server.

If you need to differentiate between different configurations of an Android application, you need Android build types and if you need to release a new Android application with change features you need Android product flavors.

In latter part of the article, we will describe when and why you need product flavors. We will also demonstrate how different flavors of an application are implemented using Gradle and Android Studio.

 

Why do we need Android product flavors?

As a mobile application developer, you can have a scenario where you would like to release a new application from your previous project with some minor changes like app icon, theme, name or anything else. For this purpose, we have two approaches

  1. Copy the whole project to create a new one, do change and release another app.
  2. Only change the required files having same base code and release another app.

Option 1 looks simple but it can turn into a major maintenance headache.

Option 2 looks tricky and difficult in the start but it is very easy to manage and maintain afterwards.
Here comes the concept of product flavors.

 

How to create Android product flavors?

Let’s say we have an application with three different flavors* i.e.

  • Mango
  • Blueberry
  • Orange

*The names of flavors could be anything might be (f1, f2, f3) or (flavor1, flavor2, flavor3)

All these flavors have same basic functionality but have different

  • Package name
  • Version name
  • Version code
  • Main layout
  • Theme color
  • App name

 

Steps to implement Android product flavors

Step 1

Create an application with basic functionality.

Step 2

Define build variants in Gradle

Android product flavor product flavors

And sync the Gradle.

Step 3

After that in project structure view create three directories under app/src and name them exactly same as product flavors in Gradle

create three directories under app/src

 

Step 4

Now create sub directories with same hierarchy as in main folder to add new layouts, drawable and values for different flavors.

android-flavor-folder-heirarchy

 

Step 5

Now you have three different flavors of same application

To change the build variant, Android Studio uses: select Build > Select Build Variant in the menu bar (or click Build Variants in the windows bar), and then select a build variant from the drop-down menu.

build-variants

Now change build variant from drop down menu and run application you have three different versions of same application (Screenshots attached).

mango-app
orange-app
blueberry-app

 

 

 

 

 

Code Snippet

Build types

 

buildTypes {
    debug {
    }
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard rules.pro'
    }
}

Product flavors

productFlavors {
    mango {
        applicationId "com.therightsw.mango"
        versionCode 1
        versionName "1.0"
    }
    blueberry {
        applicationId "com.therightsw.blueberry"
        versionCode 1
        versionName "1.0"
    }
    orange {
        applicationId "com.therightsw.orange"
        versionCode 1
        versionName "1.0"
    }
}

Closing Remarks: Android product flavors to the rescue!

You do not have to write each Android mobile application from scratch and pay multiple times if you want to launch similar concepts in games or business apps with similar concepts and look-n-feel. Android product flavors will help you save development time and cost.

Link to github repository for the sample code

Pingback

Categories

Free Consultation





Please leave this field empty.

Contact Details

Office # 12,
3rd floor, MB City Mall
I-8 Markaz,
Islamabad, Pakistan.
+92-51-493-8304
hello@therightsw.com

Start a Project with The Right Software