Default alt text
  • android
  • android app development
  • android apps
  • android studio
  • application
  • apps
  • build
  • build types
  • build variants
  • business apps
  • gradle
  • mobile apps
  • mobile development
  • mobile games
  • product flavors

Android Product Flavors and Build Variants: How to create and use them

Native Android programming tutorial. We'll teach you how to make Android product flavors programmatically using Android Studio and Gradle build tool. You can use this…

   
Android Product Flavors and Build Variants: How to create and use them
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. This is an advanced tutorial.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 typesHere 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 like shown below.Android product flavor product flavorsAnd 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 Gradlecreate 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 applicationTo 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-variantsNow change build variant from drop down menu and run application you have three different versions of same application (Screenshots attached).
mango-apporange-appblueberry-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"
    }
}

By following this tutorial, we are hopeful that you have understood basics of using Android product flavors/build variants. Send an email below if you need help in your app to create product flavors. You can also hire Android developers with us.

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.