Flavors in Android

Divyesh Dhedhi
3 min readSep 14, 2019

This is my first publications so spare me for any goof ups and help me to make it better.

So you are a developer and want to know how to distribute your app to multiple clients with different configurations or white label some of the clients. Basically it’s nothing but multiple flavors of the same application and Android studio is the best IDE to manage multiple flavors codebase.

Don’t confuse this with Build Types of android. Basically build types are the different versions of the same app with different compile and building architecture. Android studio generates two default built types by default as debug and release. Where debug being the one which you build and run on your device and release is the one which you upload on playstore.

They both gets signed by different signing key. You can obfuscate code with different proguard files for different build types.

The build type script is as below

buildTypes {

release {

minifyEnabled true

shrinkResources true

proguardFiles ‘proguard1.cfg’, ‘proguard2.cfg’

testProguardFiles getDefaultProguardFile(‘proguard-android.txt’), ‘proguardTest-rules.txt’

signingConfig signingConfigs.config

buildConfigField “Boolean”, “IS_LOGGED”, “false”

}

debug {

buildConfigField “Boolean”, “IS_LOGGED”, “true”

}

}

Product Flavor

You can simply put product flavor as “same app, different behavior”. We have a lot of apps that has more than one flavor i.e free app, paid app etc. basically paid app has more features than the free one.

The basic product flavor syntax would look something like below:

android {

defaultConfig {…}

buildTypes {…}

productFlavors {

flavorName1 {

..

}

flavorName2 {

..

}

}

}

Here flavor name should follow some recommended naming conventions like start with small letter and not number, user camel case, do not use space. The reason being in future you might would like to add specific dependency to a specific flavor then an inappropriate name will break the build.

Normally to configure dependency we use following syntax in dependencies block

implementation ‘com.android.support:appcompat-v7:28.0.0’

But for flavor specific dependency we have to use

flavorName1Implementation ‘com.android.support:appcompat-v7:28.0.0’

To run specific code on a specific Build Types or a specific Product Flavor, you can add boolean as follows likewise you can add any type of variables:

buildConfigField “Boolean”, “IS_RELEASED”, “false”

Then in code just get the variable as

BuildConfig.IS_RELEASED

If there are more number of flavors than you can use json file with array of all the flavor details and loop through each in “productFlavors”

Sample flavor list in json:

{

“list”: [

{

“name”: “flavor1”,

“applicationId”: “com.div.flavor1”,

“versionCode”: 2,

“versionName”: “1.0.2”,

“keystorePath”: “../../keystore/keystore/keystore1.jks”,

“keystorePwd”: “kpass”,

“keystoreAlias”: “kalias”,

“dimension”: “default”

},

{

“name”: “flavor2”,

“applicationId”: “com.div.flavor2”,

“versionCode”: 1,

“versionName”: “1.0.0”,

“keystorePath”: “../../keystore/keystore/keystore2.jks”,

“keystorePwd”: “kpass”,

“keystoreAlias”: “kalias”,

“dimension”: “default”

},

{

“name”: “flavor3”,

“applicationId”: “com.div.flavor3”,

“versionCode”: 5,

“versionName”: “1.1.2”,

“keystorePath”: “../../keystore/keystore/keystore3.jks”,

“keystorePwd”: “kpass”,

“keystoreAlias”: “kalias”,

“dimension”: “default”

}

]

}

Then you can loop through all the flavors from the above json and add each to productFlavors as follows:

Create model class in app level gradle file

class MyFlavor {

public String flavorName

public String applicationId

public Number versionCode

public String versionName

public String keystorePath

public String keystorePwd

public String keystoreAlias

public String dimension

}

Loop t

To use different resource files for different flavors you just have to create a directory with the flavor name which mimics main directory at app/src

We can also configure different proguard files to obfuscate different flavors.

Please check the official documentation in depth understanding: https://developer.android.com/studio/build/build-variants.html#product-flavors

--

--