Multiple Build Variants in Android

Multiple Build Variants in Android

Learn using different build variant in Android

While developing Android application, sometimes we have requirement to create multiple APKs variant. Sometimes we change the base url of the apis that we are using so we can ship a production application, sometimes we need to uncomment the proguard line in Gradle so we can build the proguarded build. Sometimes Debug sometimes Production and some endless possibilities.

In this article, we will be learning about what is Build Variants and how can it improve our productivity and efficiency. Let's dive in.

What is Build Types?

While developing any Android application we create different types of build. while development it's the "debug" build type, when releasing app on Google play store we need "release" build type. Now that we know what's build types, let's know about product flavors.

What is Product Flavour?

An Android application may have different product flavors of same app for example Free, Paid for free and paid users respectively.

You must be wondering, you want to know what Build Variant and I'm explaining Build Types and Product Flavors. Let's see why these were important before knowing Build Variant.

What is Build Variant?

Build variants can be considered as a cartesian product of all our build types and all of product flavors.

  • By default, Android Studio will generate two types of Builds: debug and release

To see the available build variants for your app, usually you'll find an option named "Build Variant" in left bottom of the Android studio. You can also search for Build Variant after pressing cmd + shift + A. It will look something like this:

In the left column, you'll see the list of modules in your project and the right side will show the selected/active build variant foreach module.

How to add new Build Types?

Here we'll see how we can add different types of build variants in Android.

By default, for any newly created project, Android studio creates two build types i.e. "debug" and "release". But in some cases we might need to introduce more build types , we need to add them into our module-level build.gradle file inside the buildTypes block. Below is the example demonstrating same:

android {

    defaultConfig {
        applicationId "com.codewithniks.buildvariant"
        versionCode 1
        versionName "0.0.1"
        ...
    }

    buildTypes {
        debug {
            versionNameSuffix ".debug"
            debuggable true
            ...
        }
        release {
            debuggable false
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            ...
        }
        minifiedDebug {
            versionNameSuffix ".minifiedDebug"
            debuggable true
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            ...
        }
     }
...
    }

In the above snippet, we can see there are three build types i.e. debug, release and minifiedDebug. The debug and release are the default build variants generated by Android Studio and minifiedDebug is the we created which is basically a debug build but proguard is enabled for this.

What is versionNameSuffix?

versionNameSuffix as it's clear from it's name, it is basically modification of version name by adding a suffix for example ".debug" and ".minifiedDebug" in our case, after the versionNameSuffix is added, the version name of our app when debug variant is enabled will be : 0.0.1.debug and when minifiedDebug is enabled then 0.0.1.minifiedDebug.

Let's take a look at a case when you need all the properties of a debug build but want to add a few more, then you won't have to copy all the properties but instead you can use initWith BUILD_VARIANT_NAME. this will get all the properties of the BUILD_VARIANT_NAME. for example:

newVersionDebug {
    initWith debug
    versionNameSuffix ".version3"
    ...
}

Here you can see we are importing all the properties of debug using initWith also on the other hand we are modifying the versionNameSuffix, so this will not use the versionNameSuffix from the debug configuration but this one.

How to add Product Flavors?

For adding product flavors in our android project, we need to add productflavors block inside the android block and provide some flavor dimensions using flavorDimensions to each of the product flavor.

android {
    ...
    buildTypes {
        debug{...}
        release{...}
        minifiedDebug{...}
    }

    flavorDimensions "version"
    productFlavors {
        development {
            dimension "version"
            versionNameSuffix ".development"
        }
        production {
            dimension "version"
            versionNameSuffix ".production"
        }
    }
}

One of the advantage of product flavors is they are associated with the build types. let's say for "debug", "release" and "minifiedDebug" build types and flavors "development" and "production", our build variants will be:

  • developmentDebug
  • developmentRelease
  • developmentMinifiedDebug
  • productionDebug
  • productionRelease
  • productionMinifiedDebug

So this was all about Build Variants, Product Flavors, Build Types in our project. That's all for this blog.

If you have any doubts, feel free to comment and ask questions, If you loved it, please follow so that you can read as many blogs as possible and become a better developer.

Cheers, CodeWithNiks

Instagram : CodeWithNiks YouTube : CodeWithNiks Hashnode: CodeWithNiks

Did you find this article valuable?

Support CodeWithNiks | G S Rathore by becoming a sponsor. Any amount is appreciated!