Gradle: adding assets folder dynamically
You might be overthinking the work required. Just create the productFlavor
then define the flavor's assets.srcDirs
. Now we can sit back and let Android's gradle plugin do all the work.
android {
//...
productFlavors {
blah {}
more {}
}
sourceSets {
blah {
assets.srcDirs = files(getFlavorConfig(it.name))
}
more {
assets.srcDirs = files(getFlavorConfig(it.name))
}
}
}
// simplified method for example
def getFlavorConfig(String str) {
return ["$projectDir.absolutePath/src/extensions/assets/first", "$projectDir.absolutePath/src/extensions/assets/second"];
}
After the build our files will be where we expect:
$ ls app/build/intermediates/assets/blah/debug/
hello.txt world.txt
$ ls app/build/intermediates/assets/more/debug/
hello.txt world.txt
$ ls app/build/intermediates/assets/blah/release/
hello.txt world.txt
$ ls app/build/intermediates/assets/more/release/
hello.txt world.txt
I ended up using one flavour at a build time and used the following line:
android.sourceSets.main.assets.srcDirs += ['src/extensions/assets/'+it]