Build Variants — Android (Advanced)

Amir Raza
3 min readApr 5, 2020

--

It is a continuation of build variant series. If you are a new to variant, you can checkout my tutorials for Beginner and Intermediate.

What can I learn in this tutorial?

  • More configurations in gradle using flavors
  • Configurations on package level

Lets get into it.

flavorDimenstions is a key to list all flavor types you want to use. The order you write, flavors will get priority from highest to lowest when gradle merges configurations and sources.

NOTE: you must assign each product flavor to one of the dimensions.

flavorDimensions "api", "version", "environment"
productFlavors {
minApi19 {
//Assigns this flavor to `api` dimension
dimension "api"
minSdkVersion 19
versionNameSuffix "-minApi19"
...
}
minApi21 {
dimension "api"
minSdkVersion 21
versionNameSuffix "-minApi21"
...
}
free {
dimension "version"
versionNameSuffix "-free"
applicationIdSuffix ".free"
...
}
paid {
dimension "version"
versionNameSuffix "-paid"
...
}
dev {
dimension "environment"
...
}
production {
dimension "environment"
...
}
}

Build variant will be:
[minApi19, minApi24][free, paid][dev, production][debug, release]

minApi19FreeDevDebug or minApi19PaidProductionRelease etc…

Creating source set

By default, Android Studio creates main/ under src/source set and corresponding directories to share between all variants. However, you can create you own source set under src/ to manage your different configured build. Let’s look at the example below

Here I have created 2 source set dev and production to manage a Constants file according to my build version. One thing is to notice here, the java folder is in main and dev source set is blue colored where java folder underproduction is in gray colored, the reason is, when the specific variant selected, the source set is activated according that variant, so blue colored indicates which variant is currently selected.

NOTE: java folder under main source set is always in blue colored [activated] because of default configuration.

You have to make sure that the package name must be same under the different source set. Why?

Let’s consider the above example, I have created a Constants file in dev and production and initialized a variable named BASE_URL. When I use this variable in my MainActivity and try to execute the code with dev source set, the BASE_URL will be used via dev source set and the package is imported in MainActivity, now when I changed variant to production the BASE_URL will be used via production source set with the same package.

Things to understand here is java folder under main source set is the Default place means if the selected file is unavailable in the selected variant then the compiler will go to main source set to find that file. For instance. you have created a file named Configs under dev and main BUT did not create under production, and when you build the project while selected production variant the compiler will go to main’s Configs file and use.

Congratulations! You are done.

There is a lot to read regarding the variants. If you want to know more about variants, you can checkout google’s doc

--

--