Product Flavors in Android

Victor Apoyan
5 min readJan 12, 2021

Let’s imagine you have an Awesome application 😉

Also, you have an Awesome Library which should be used in your Awsome application. The challenge is that the library has different versions and each version provides different functionality. But we want our Awesome application to work with all versions of Awsome Library.

How we can solve this challenge 🤔

Product Flavors

Easy 🧐, with the help of Product Flavors.

Product Flavor allows you to create different versions or variants of your application in the same codebase. Product Flavors are a feature of Gradle Plugin used in Android Studio.

It’s easy to add flavor using Android Studio, to do so you need to navigate to File 👉 Project Structure 👉 Build Variants 👉 Flavors.

Here you need to press on the + button which will show you a popup

  • Add Flavor Dimension
  • Add Product Flavor

To be able to add Product Flavor, first, you need to add Product Dimension.

Adding Flavor Dimension

All flavors must belong to a named flavor dimension, which is a group of product flavors. You must assign all flavors to a flavor dimension; otherwise, you will get the build error.

As our challenge was to support different versions, let’s call the dimension version, press on the Add Flavor Dimension button and in the opened dialog type version and press ok.

Adding Product Flavor

So an Awsome Library has different versions stage, alpha, and beta, so we will name our flavors in the same way, press on the Add Product Flavor button, and in the opened dialog type the names of the flavors and press ok.

After pressing Apply this will generate code in the build.gradle file of your module, which will look like this

Also in the Android Studio in the tab Build Variants, you will have

These are all Build Variants which are basically the combination of Build Type plus Build Flavor in your application. So now depending on the selected variant, your application can have different behavior.

Dependencies

So now depending on the selected variant Awesome Application should use a different version of the Awesome Library, it’s easy to achieve, as now in the dependency section we can specify for each flavor a specific dependency

In the way how it is specified above the debug and release build types for each flavor will use the same library. If you want to change this and specify a different dependency for Build Variant stageRelease you need to write

Codebase

You can write different code for different flavors. To do so right-click on the app module then New 👉 Directory

now you need to add the directory java in the path src/alpha/java all code that will go there will be executed only in the case if you have selected Build Variant: alphaRelease or alphaDebug . In the same way, add java directory for beta and stage flavors, at the end your project structure should look like

you can do the same for jni, res, assets, aidl and other directories and place in it for each flavor different stuff, for example, strings for alpha and beta can be different or layouts for beta and stage can be different.

Also if you create directories for specific build type and flavor like src/alphaRelease/java.

Now let’s create 4 new files. In the main directory, we will add abstractAwesomeAPIBase class and for the each flavor we will add AwsomeAPI classes which will be extended from AwesomeAPIBase class.

abstract class AwesomeAPIBase {

abstract fun getAwesomeName(): String
}

and each class for the flavor will implement getAwesomeName and print its text.

class AwesomeAPI : AwesomeAPIBase() {
override fun getAwesomeName(): String = "I am Alpha Awesome API"
}

and in the MainActivity we will just call

val awesomeApi  = AwesomeAPI()
textView.text = awesomeApi.getAwesomeName()

now depending on the selected flavor, the text will be different.

Configurations

If you navigate to the Project Structure dialog and select the Flavors tab

and then select one of the flavors stage, alpha, beta you will see that for each flavor you can specify Application ID, Version Name, Version Code etc.

Pros and Cons

Pros

Of course, the same challenge can be also solved by having different Awesome Applications and using in each the correct version of the Awesome library, but then you would have problems with maintenance, flavors keep your codebase in one place, you can easily navigate through the project in Android Studio, no need to open several Android Studio projects.

Cons

Imagine you have 2 build types (release, debug) 2 flavor dimensions (version and api) and each has 2 product flavor for each dimension (alpha, beta and api24, api25), this means that you have 12 build variants (alphaApi24Release, alphaApi24Debug, alphaApi25Release, alphaApi25Debug), which scales and complicate your codebase immediately.

Conclusion

  • Before creating two separate projects which have slightly different logic think about using flavors instead. As an example, if you have free and paid applications where the paid version has more functionality than the paid, flavors can definitely help.
  • Sometimes it’s good to mock some functionality in the testing stage, flavors can help to achieve this and test your app easily.

Official Documentation

https://developer.android.com/studio/build/build-variants

Thanks for reading, please clap👏 and share 🤝

--

--