TheJadav

Basic use of flavors or build variant in android

Basic use of flavors or build variant in android

This article is for flavor in android. We will see what is flavor and see basic use of flavor.

What is Flavor or Build Variant

Build variant is powerful feature of android studio, which gives facility to swap code at compile time based on flavor. Like Ice-cream have flavor vanilla, strawberry, chocolate, We can also specify different flavor for our app which make some resource available for only specific app like free or paid app.

Why Flavor

To better understand why we need flavor, lets see one example. We have one app which we have developed for one client, now we got another client who also need same app , but obviously he needs his own logo and name. So for we that we can clone our app or use variant. Cloning app and changing application Id takes too much time, whereas variant takes just few minutes to setup. So variant is useful feature in that situation.

Add Flavor in app

Open project structure dialog by simply doing right click on app and then selecting open module settings.

Select Build Variant and then from right side select flavors tab like below

Click on add button and add flavor dimension “tyre” and click on “tyre”, you can see screen as below

Now again click on + icon to add flavor, I have added free and paid flavor for this demo app which looks like below

We can see many option here when flavor added, we can specify applicationId suffix or even application ID, and so many things for this particular build. You can see that our project level build.gradle file is updated with some line as below:

flavorDimensions 'tyre'
productFlavors {
    free {
        dimension 'tyre'
    }
    paid {
        dimension 'tyre'
    }
}

We can even specify other variable inside free or paid block which will be available in that build only. We can even specify resource inside this block. Check build variant tab, In my case it is in bottom left side. There we have option to create build like:

  • freeDebug
  • freeRelease
  • paidDebug
  • paidRelease

We can also manage our java/kotlin file based on flavor. We can also check flavor at run-time by simply

BuildConfig.FLAVOR

Share this content:

Share:

More Posts

Introduction to Kotlin: A Versatile and Modern Programming Language

Kotlin, a versatile and modern programming language, offers developers a concise, safe, and interoperable coding experience. With features like null safety, extension functions, and coroutines, Kotlin enhances productivity and readability, making it an attractive choice for Android development and beyond. Whether you’re new to programming or an experienced developer, exploring Kotlin opens up a world of possibilities for building robust and expressive applications.

Mastering the Android Activity Lifecycle: A Comprehensive Guide

The Android Activity Lifecycle is a fundamental concept for Android developers. It defines how an Activity behaves during its lifecycle, from creation to destruction. Understanding the Activity Lifecycle is crucial for managing resources efficiently and delivering a smooth user experience. In this blog post, we’ll briefly introduce the different states an Activity can be in and the main callback methods associated with each state. Let’s dive in and explore this important aspect of Android app development!

Table of Contents

Send Us A Message