Suppose I have an app that shows locations on a map. Now I want to make several different variations of this app, one showing shops, one showing restaurants, etc.
In some of these apps I want to include ads, in others I don't.
How can I selectively add certain srcDirs to one flavor but not another? I tried the following:
productFlavors {
Shops {
applicationId "com.me.shops"
ext.hasAds = true
}
Restaurants {
applicationId "com.me.restaurants"
ext.hasAds = false
}
... more flavors with ext.hasAds either true or false
}
applicationVariants.all { variant ->
String withAdsDir = 'src/withAds'
String withoutAdsDir = 'src/withoutAds'
if (variant.productFlavors.get(0).ext.hasAds) {
variant.sourceSets java.srcDir withAdsDir + '/java'
variant.sourceSets res.srcDir withAdsDir + '/res'
}
else {
variant.sourceSets java.srcDir withoutAdsDir + '/java'
variant.sourceSets res.srcDir withoutAdsDir + '/res'
}
}
But this fails because for one, the syntax in the if-structure is incorrect.
I am trying to avoid having to specify the folders for every flavor individually, therefore using the .ext.hasAds boolean.
How can I achieve specifying particular srcDirs depending on a certain boolean?
UPDATE
I 'fixed' it. Meaning I get what I want. But I'm not very happy about how:
flavorDimensions "adsornot", "typeofapp"
productFlavors {
Shops {
applicationId "com.me.shops"
ext.hasAds = true
dimension "typeofapp"
}
Restaurants {
applicationId "com.me.restaurants"
ext.hasAds = false
dimension "typeofapp"
}
Ads {
ext.hasAds = true
dimension "adsornot"
}
NoAds {
ext.hasAds = false
dimension "adsornot"
}
}
sourceSets {
String withAdsDir = 'src/withAds'
String withoutAdsDir = 'src/withoutAds'
Ads {
java.srcDir withAdsDir + '/java'
res.srcDir withAdsDir + '/res'
}
NoAds {
java.srcDir withoutAdsDir + '/java'
res.srcDir withoutAdsDir + '/res'
}
}
variantFilter { variant ->
boolean shouldHaveAds = variant.getFlavors().get(0).ext.hasAds
boolean variantHasAds = variant.getFlavors().get(1).ext.hasAds
variant.setIgnore(shouldHaveAds != variantHasAds)
}
Explanation:
I create 2 flavor dimensions: type-of-app, and ads-or-not
This means there will be 4 variants generated. But I only want 2. The variantfilter checks each variant's 2 flavors, they should have matching ext.hasAds
(defined in the flavors). If they do not match, ignore this variant.