I\'m setting up the product flavors in my app and have run into one problem. Two of my product flavors are very similar to each other and only differ by a few resources, let\'s
I think you won't be able to do this in the android
object directly, but you can use a configuration function to set common properties on a flavor. Put this after the android
object:
void configureWhiteLabelFlavor(flavor) {
flavor.useJack = true
}
configureWhiteLabelFlavor(android.productFlavors.whiteLabelA)
If needed, you can implement deep inheritance by calling other functions from this function.
I found two solutions:
1. Use flavor dimensions.
android {
...
flavorDimensions 'palette', 'color'
productFlavors {
blackAndWhite {
dimension 'palette'
}
redAndWhite {
dimension 'palette'
}
red {
dimension 'color'
}
white {
dimension 'color'
}
}
variantFilter { variant ->
def first = variant.getFlavors().get(0).name
def second = variant.getFlavors().get(1).name
if(!(first.equals('blackAndWhite') && second.equals('white')) ||
!(first.equals('redAndWhite') && (second.equals('white') || second.equals('red')))) {
variant.setIgnore(true);
}
}
...
}
Because of variantFilter
we have 3 combinations instead of 4:
blackAndWhite => white
redAndWhite => white
=> red
You can consider that as red
extending redAndWhite
and white
extending blackAndWhite
or redAndWhite
.
This answer and variant filter documentation were helpful.
2. Edit source set.
productFlavors{
flavor1 {
}
flavor2 {
}
}
sourceSets {
flavor2 {
java.srcDirs = sourceSets.flavor1.java.srcDirs
res.srcDirs = sourceSets.flavor1.res.srcDirs
resources.srcDirs = sourceSets.flavor1.resources.srcDirs
aidl.srcDirs = sourceSets.flavor1.aidl.srcDirs
renderscript.srcDirs = sourceSets.flavor1.renderscript.srcDirs
assets.srcDirs = sourceSets.flavor1.assets.srcDirs
}
}
Code example was shamelessly copied from this blog post. Big thanks to its author.
I was looking for a similar thing in gradle and found Multi-flavor variants. I have an app that should have versions A and B, and each version has dev and pro environments, so I ended up with this in my gradle:
flavorDimensions 'app', 'environment'
productFlavors {
versionA {
flavorDimension 'app'
}
versionB {
flavorDimension 'app'
}
pre {
flavorDimension 'environment'
}
pro {
flavorDimension 'environment'
}
}
And in my build variants I have versionAPreDebug, versionAPreRelease, versionBPreDebug, versionBPreRelease, etc. I think what you need is something like that.