How to create Build Flavors and handle multiple Flavors in Android App ?

 When you create Build Flavors ? 

We create Build Flavor if we want multiple versions of our app with different features in a single code base. Ex. Paid and Unpaid Version have different features enabled. 


We can Group Flavors using Flavor Dimension

The one use case I can think of is this.

  • You have a free and paid flavour under a dimension of myapp.

  • You have a test and prod flavour which point to different backends under a dimension of environment.

When you assemble all you end up with a version for each myapp and environment so you can test a free/test version, free/prod version etc.

You don't need to inspect the dimension, just put whatever variables/ conditional code against the flavour as has always been the case.

An example using multiple dimensions

,,,
flavorDimensions "myapp", "env"

productFlavors {
    paid {
        dimension "myapp"
        ... add variables here
    }

    free {
        dimension "myapp"
        versionName = android.defaultConfig.versionName + " free"
        ... add variables here
    }

    test {
         dimension "env"
         ... add variables here
    }

    prod {
         dimension "env"
         ... add variables here
    }
}
...

Then for each flavor we need to pull all our resources into their respective directory. 

Ex. 

paid flavor has src/paid directory 


But we can use BuildConfig to store all the Constants using the Gradle Solution 



Build Variant 

It is a combination of your flavors and build types and contains all the data which will be finally used to build your app. 


 paid {
        dimension "myapp"
        ... add variables here
        applicationId "com.myapp.paid"
        ext{
            test = "dev.myapp.lol"
            prod = "myapp.lol"
        }
    
    }




Comments

Popular Posts