Product flavours are easy to implement once you start but the major issue android developers get is when they have to add one or few of the flavours to another module as a library dependency. Library dependency is an essential feature and every developer needs it once in a while. So how we will implement it with product flavours? How we will command our IDE to add this flavour and not this one?
Actually android developers have a solution for this also. The solution is a Compile setup in which you define/ compile only those library product flavours you need in your module.
Suppose I have two flavours flavour1 and flavour2 , now I had a higher level module app in which i have to add flavour1 as library. So the library’s build.gradle will be like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
android { compileSdkVersion 23 buildToolsVersion "23.0.2" publishNonDefault true defaultConfig { minSdkVersion 15 targetSdkVersion 23 } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt' } } productFlavors { flavour1 { applicationId "com.webkul.flavour1" versionCode 1 versionName '1.0' } flavour2 { applicationId 'com.webkul.flavour2' versionCode 1 versionName '1.0' } } |
As you can see in the above file we have mentioned
1 |
publishNonDefault true |
Its because by default a library only publishes its release variants. So to enable the publishing of more variants we used this. You can read more about it here.
And your app’s build.gradle should be like this,
1 2 3 4 5 6 7 8 9 10 11 |
configurations { flavour1DebugCompile flavour1ReleaseCompile } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' flavour1DebugCompile project(path: ':lib', configuration: 'flavour1Debug') flavour1ReleaseCompile project(path: ':lib', configuration: 'flavour1Release') } |
And this will add only flavour2 to your project. You can add more than one flavour , you just need <yourFlavourname><Debug/Release>Compile as your configuration.