A Simple Guide to Android Product Flavors

Cleopas Banda
Level Up Coding
4 min readNov 18, 2019

--

Some time back while working on a project, I was tasked to create a variant for an “events” mobile app. It was supposed to have the core functionality of the main app but with changes to the color schemes and information, making it specific to that particular event.

Right off the bat, my initial thought was that of cloning the project and changing the files to suit the specific event. Sounds like a plan right? The problem is that as more events get added in the future, maintenance of the code becomes a massive headache and nightmare! — Enter Product Flavors to the rescue! ;)”

My goal with this article is to give an introduction to what we call “Product Flavors” in Android, and I will take you through the basic setup.

123 let’s go

Product Flavors so what are they?

Simply put, a product flavor is a variant of your app. It is very useful when you want to create multiple versions of your app. This means you can generate different versions or variants of your app using a single codebase.

Product flavors are a powerful feature of the Gradle plugin from Android Studio to create customised versions of products. They form part of what we call Build Variants.

Build Variants

Build Variants are formed by Build Types and Product Flavors.

According to the Google documentation, build variants are the result of Gradle using a specific set of rules to combine settings, code, and resources configured in your build types and product flavors.

Build Type applies different build and packaging settings. An example of build types are “Debug” and “Release”.

Product Flavors specify different features and device requirements, such as custom source code, resources, and minimum API levels.

Why Product Flavors?

  • They address the issue of having separate project code for each version of the app while still having one project code.
  • Given a scenario where you have a free and a paid app you can limit features in the free and expose all the other features in the paid version of the app.
  • Given another scenario where you want to implement region-specific functions depending on the country, you can use product flavors for such a use case.
  • White labeling (these are apps that are developed by a certain company, and they are re-branded and resold by other companies).

Pros and Cons

Pros

They address the issue of having a separate project code base for each version of the app.

They keep the code tidy and makes it much easier and faster to navigate through the code base as everything related to the specific product flavor would be kept in their corresponding folders.

Cons

(Scaling Up) The more variants, the greater the complexity which thereby makes it harder to maintain the codebase.

IDEs sometimes takes time to build the project after switching between variants.

Setting Up Product Flavors

Open up your build.gradle file in the app module and add your flavors inside the android section.

The below example has 3 product flavors added with each having its own applicationIdSuffix. Having a different applicationIdSuffix for each product flavor helps in creating different apps that can be deployed on the PlayStore.

Quick Note: All flavors must belong to a named flavor dimension. Flavor dimensions are a group of product flavors. If you do not specify a flavor dimension you will get an error.

Build the project and once done, select Build > Select Build Variant in the menu bar, and you will see the different Build Variants auto-generated when you added the Product flavors.

build variants

Structuring your Flavors

Your main folder is the one that contains all the common and shared code between flavors, you can create flavor specific code by creating different source set for each flavor MyProject/app/src/main

Open the Project pane and select the Project view from the drop-down menu at the top of the pane.

  1. Navigate to MyProject/app/src/
  2. Right-click on src and add a java folder
  3. Under Target source set choose the one for your flavor and click Finish

Bear in mind you are supposed to follow the same folder structure as the main folder.

In the java folder, you can create your flavor-specific code under your selected flavor folder.

You can also add flavor specific resources. You basically follow the same steps as above and instead of using a java folder, you can choose the res folder

Conclusion

Learning about product flavors was very insightful for me in my journey, and my main struggle was finding a simple and straight forward introduction and explanation to product flavors, thus the conception of this article. Below is further detailed reading for your perusal.

Further reading

Configure Build Variants

Advanced Android Flavors

--

--