How to Use Flavours in Android Development the Right Way?

If you are a developer, you may come across clients asking for multiple versions of the same app.

It can be a lot of work as you have to create the same app from scratch many times. But not anymore.

You can use the flavours to make different versions of an app using the same base code. The good news is that it is time-saving and relatively easy. It also saves you from long hours of work and reduces the cost for you as you no longer have to pay for getting the same app built many times.

Using Flavours in Mobile Development

You can easily create flavours by adding the blocks of product flavours in the android block at the app level and inside the build.gradle file.

Though it is an extensive process, we can sum it up in the steps given below:

1. Resource Files Creation

First of all, we will be creating a directory for all the required build types. For this, go to the ‘New’ tab in the bar. There select ‘Folder’ and then navigate to ‘Res Folder.’ Here, you will see two options: 

  • Target Source set consists of the source code directories corresponding to every selected flavour.
  • The New folder location – This is where all the newly created files are present.

2. Java Files Creation

The next step involves making a java directory. You can form this easily, just like you create resource directories. But the only difference is that here you will be selecting a java folder instead of the RES.

3. Build Variant

Build variants are formed when Gradle uses a given set of rules combining the various resources. These are the ones configured by you with the attributes like settings and code, in the flavours as well as product types.

But before going ahead, it is also important to understand the build types that refer to various packaging settings and build. Commonly there are two types of builds – Debug and Release.

Build variants constitute the final step of flavour creation. Herein you sync your project and update the build.gradle. Do this easily by going to ‘Build’ and then navigating to ‘Select.’

There you will be able to see different flavours that have been auto-generated because of the command.

More Features of Android Flavors

Here, be careful that the built types and product flavours are two different things. The product flavours are a part of the top-level distribution within app variants, whereas the build types are the internal variants within the flavours.

Switching Between Different flavour types

Once you have created all the flavours, inevitably you will also have to switch between them. The android studio has made it very easy and, the good news is that just like anything in the indoor studio, you can easily do this. Simply press the shift key twice and search for the build variants.

Further, sometimes, you may have to call activities from the main directory to the flavours and vice versa. But what about the activities that are different in all the flavours? It is possible too!

Just be careful that you keep the same packaging directories. So when you call an activity, the android studio will take care of it by calling the activity of the respective flavours.

Always add a flavour

Sometimes you might get an error code saying:

It could be because the android studio has now made it necessary to ascribe a flavour dimension. Even if you are not using this functionality, you can go for a default name. Because the Gradle plugin will automatically assign all the flavours of the module case to that dimension.

Filter variants

Sometimes, you may also across scenarios where you create flavours but do not need them later. It is important to search for these and remove them. You can do this by using variantFilter to filter out all the respective build variant configurations you want to remove. This is an example of the filter variant that can be used to filter out product flavors – minApi21″ as well as “demo” product flavors.

Source Sets

Android studio also creates directories for sharing between the build variants. You can use the source directories such that they only have the codes specific to a flavor or build type. Here, do not use the same java class as it may result in an error.

Since it is ideal for the Gradle when you organize directories and source set such that they are similar to the main/source set. Here is an example for the build type debug from task output.

We hope now you will be able to create flavours without any difficulty. Using these, you can easily manage different variants of various codes. However, there might also be drawbacks like several variants may become difficult to manage and creating IDE’s might look difficult at first but they become easier as you spend more time working with them, making them a smart choice!