This question in continuation of my other question, which i want to improve further.
I am being able to group flavors (having common configuration) under sourceSets
with the following code :
(got it from a genius in the linked question above)
import com.android.build.gradle.api.AndroidSourceSet
android {
sourceSets {
[flavor2, flavor4].each { AndroidSourceSet ss ->
ss.assets.srcDirs = ['repo-assets/flavor2']
ss.res.srcDirs = ['repo-res/flavor2']
}
}
}
Now, I was wondering if the list [flavor2, flavor4]
could be pulled from any of the following:
In addition to the flavor name, I intend to store the following in the external source (one of above):
productFlavors
)PORPOSE: I want to write a generic piece of code to iterate and dynamically create the productFlavors
and sourceSets
. I have generalized sourceSets
to almost 90% and one block is now sufficing for all flavors.
It looks something like this now:
sourceSets {
[flavor1, flavor2, flavor3 ...].each { AndroidSourceSet ss ->
ss.assets.srcDirs = ['repo-assets/' + ss.name.split('_')[0]]
ss.res.srcDirs = ['repo-mipmap/' + ss.name.split('_')[0] , 'repo-strings/' + ss.name]
}
}
Also want to do the same thing for productFlavors
as hinted above.
STUCK AT: getting the list [flavor2, flavor4]
in the code above from an external source (along with a few additional fields per flavor as listed above).
I see methods like
productFlavors.add()
productFlavors.addAll()
but am not quite sure about how to go about using these. As the methods are available I'm sure it is possible to do what I'm trying to.
Has anyone done this and has some pointers?
I finally got it to work this way:
Created a custom class MyFlavor
within build.gradle
and added each flavor from the csv file to an ArrayList
of MyFlavor
class MyFlavor {
public String flavorname;
public String basename;
public String appid;
public String bannerid;
public String interstitialid;
public String category;
}
def ArrayList<MyFlavor> myFlavors = new ArrayList<>();
new File('app/flavors.csv').eachLine {
String[] values = "$it".split(',');
MyFlavor f = new MyFlavor();
f.flavorname = values[0];
f.basename = values[0].split('_')[0];
f.appid = values[1];
f.bannerid = values[2];
f.interstitialid = values[3];
if(values[0].contains('_')) f.category= "state"
else f.category = "country";
myFlavors.add(f);
}
Then iterated over the ArrayList
to dynamically create the productFlavors
and sourceSets
as follows:
productFlavors {
myFlavors.each { MyFlavor f ->
"$f.flavorname" {
applicationId = "$f.appid"
buildConfigField 'String', 'BANNER_ID', "\"" + "$f.bannerid" + "\""
buildConfigField 'String', 'INTERSTITIAL_ID', "\"" + "$f.interstitialid" + "\""
buildConfigField 'String', 'CATEGORY', "\"" + "$f.category" + "\""
}
}
}
sourceSets {
myFlavors.each { MyFlavor f ->
"$f.flavorname" {
assets.srcDirs = ['repo-assets/' + "$f.basename"]
res.srcDirs = ['repo-mipmap/' + "$f.basename" , 'repo-strings/' + "$f.flavorname"]
}
}
}
Hope this helps someone. I was successful in getting rid of 1000s of lines of code (because i have a lot of flavors) and bring it down to these 10s of lines you see here.