Product Flavors allows us to customize the codes and resources in our application for different systems. Situations such as creating paid or free versions or using two different services in the same project are examples of this.

Basically or mostly commonly used in companies for creating debug version build and release version build

But lets first discuss about some key points related to flavors:

a. Build Types:

When you create a new module, Android Studio automatically creates the debug and release build types for you. Actually we use it every day unaware. When you develop your application it compiles in debug mode by default(If you don’t change build variant. We will come this later.).

So what is build type for? It decides that how our code will be compiled. For instance, If we want to sign our .apk with debug key, we put our debug configuration into debug build type. If we want to have obfuscated code when it is compiled and ready to release, we put that configuration on our release build type. If we want to log our HTTP request in debug mode but we want to disable it on release mode, we put that configurations on build types or call build types in library dependencies.

Every build type has same codebase and same UI behaviour with different compile configurations.

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

I think this is the simplest example of build types. If you want to create different application on release and debug build. You have to change your applicationId.

defaultConfig {
applicationId "com.sampleproject"
...
}

when you run your app in debug mode, your application package name will be com.sampleproject.debug and if you run your app in release mode, your application package name will be com.sampleproject.

If you want to add more property to build types like signingOptions, debuggable, useJack, versionNameSuffix and more, you should check this link.

Product Flavor

Documentations and blogposts are saying that product flavor is like build type. First question is that comes to my mind is that If there are like each other, why don’t we use just build types instead of product flavors? Keep this question on the edge of your mind. We will come this later.

Everyone is giving same example “free” “paid” version of your app. But I will give another one. Let’s say you are developing your app for your customer users. Everything is going fine for customer app. Then your product owner said that you need to develop that app for admin users. Admin user app should have all functionalities that customer app has. But also admin user can have access to statistics page and admin user should see the app in different colours and resources. And also your admin app’s analytics should not be mixed with customer app.What will you do? The answer is Product Flavor. Same app, different behaviour.

Edit your gradle file.

android {
...
defaultConfig {...}
buildTypes {...}
productFlavors {
admin {
..
}
customer {
..
}
}
}

And add your flavor folders under /src/

Build Variants:

Build variant is the combination of the build types and the product flavors.
e.g. in the example we have 2 build types:
1. Debug
2. Release

Short and simple that’s some info on flavors.keep Coding!!