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

How does data persistence work in Android?

Data persistence in Android is the ability to save data to the device so that it can be accessed later, even if the app is closed or the device is restarted. There are several ways to achieve data persistence in Android

Fragments and Activities: Two Pillars of Android Development

Fragments and Activities are both important components of Android development. Fragments are a good choice for creating reusable and dynamic UIs, while Activities are a good choice for managing the lifecycle of an app’s UI and implementing core features.

Table of Contents

Send Us A Message