Build Variants — Android (Beginner)

Amir Raza
3 min readFeb 9, 2020

--

Development of a scale-able app is one of the masterpiece of your project architecture, When you are building a large application (or often a small one) that might contains several features or options BUT you as a developer want that, those are available to the user on the different versions of the app. For instance, you might have seen so many apps with Free or Paid or Pro versions etc. So, maintaining the different versions of the app in a Single Project can be achieved by Build Variants in android so,

What is Build variant?

Simply a way to expose your app in different versions or flavors by maintaining only a Single Project.

How to implement in project?

Just can be done in some easy steps…
I am using Android Studio 3.5 Stable, Kotlin v1.3.50 at the time of writing article.

Step 1:

Create a project and you will see the build.gradle in app module.

Default configuration in build.gradle in app module.
Default Settings in build.gradle in app module

Click on Build Variants at bottom left in side bar and you will see the options debug and release which is by default available in every project. if you can’t find the option at bottom, just goto View > Tool Windows > Build Variants

Step 2:

Add the following code snippet after buildTypes { … } in build.gradle and press Sync Now.

flavorDimensions "env"
productFlavors {
local {
dimension "env"
}
production {
dimension "env"
}
}

productFlavors is the place where all build variants can be defined. local and production here are the two variants, you can define as many as you want.

What is dimension here?
dimension is the flavor category that where you want to put all the stuff related to the variant, to define dimension you have to add flavorDimensions just above the productFlavors.

After successfully syncing you will see the follow…

Before adding flavors you will see only 2 options debug and release. Basically there are two types of build debug(for development purpose) and release(for production/live purpose). After adding flavors, compiler will generate your added flavors + build types. In this case, local for debug and release, similarly production for debug and release. so you will see localDebug and localRelease and so on…

Cheers! You have done.

Want to go more deep in variants?

--

--