Android Product Flavours

Android Product Flavours

What is a Product Flavour?

Product Flavour basically means having a variant version of your app. Having a different behavior for each app means creating a code base for each functionality and as a developer you want a clean code. There come Android Product Flavours. It simply specifies different features and device requirements such as custom source code.

If let's say you want to limit features accessed by users who have a premium account and those of regular accounts, product flavor would be a great fit. And the best thing is that it's just a Gradle plug-in away!!

It's Part Of the Build Variant

Build Variants are achieved by Gradle's set of rules which merge settings, code, and resources in your build types and product files under the Gradle file.

Let's dive in !! So first thing we'll add our flavors inside the build.gradle file under the app module. In this case, we're going to use regular and premium as our two flavors. Isn't that easy?

android {

  //...  
  productFlavors {
    regular {
      applicationIdSuffix ".regular"
    }
    premium {
      applicationIdSuffix ".premium"
    }
}

Be sure to sync the gradle and then click View>Tool Windows> Build Variants. On clicking the dropdown, we see two variants, premium and regular. Awesome! You made a progress!!

Now let's add the flavors categorically

navigate to app>src> and right-click src. and add a package folder and be sure to follow the same folder structure as the main structure . Under the Target source set choose the one for your flavor and click Finish.

YourAndroidProject/
  app/
    src/
      main/
       <contents of main>
      regular/
        res/
          values/
            regular.java
      premium/
        res/
          values/
            premium.java

now you can comfortably switch between the regular and premium flavor. Thumbs up!!!!