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.

Screenshot-from-2021-12-14-17-07-37-51384b62-150x150 Basic use of flavors or build variant in android

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

Screenshot-from-2021-12-14-17-10-11-c679a1f1-150x150 Basic use of flavors or build variant in android

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

Screenshot-from-2021-12-14-17-13-13-033e776d-150x150 Basic use of flavors or build variant in android

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

Screenshot-from-2021-12-14-17-14-34-f531513e-150x150 Basic use of flavors or build variant in android

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

Screenshot-from-2021-12-14-17-21-14-2d9b49fc-150x150 Basic use of flavors or build variant in android

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: