I happened to come back to this because of a comment on my answer and realized that this answer is superfluous (still better than the accepted one which is even more so). For each productFlavor and buildType, combination and individual source sets already exist. i.e. src/{buildType}
, src/{productFlavor}
and src/{productFlavor}{buildType}
are already available source folders.
So essentially, all that was needed for the OP was to put the resources in src/flavor1Debug
which is equivalent to the src/debug/flavor1
that the OP envisions.
OLD ANSWER:
I've done something similar with buildConfig
but hopefully it should work with sourceSets
.
Basically, you define the common stuff at the productFlavors
level in a variable and keep adding things as you move down.
productFlavors {
def common = 'src/main'
flavor1 {
def flavor = 'src/main/flavor1'
buildTypes {
debug {
sourceSets {
res.srcDirs = [ common + ',' + flavor + ',' + 'src/main/debug'
}
}
release {
sourceSets {
res.srcDirs = [ common + ',' + flavor + ',' + 'src/main/release'
}
}
}
}
I haven't tested this. I think you might need to use android.sourceSets
instead of just sourceSets
.
I've also needed to define separate resources for the productFlavors
so I used a separate statement late in the build file. Like so:
android.sourceSets.flavor1 {
res.srcDirs = ['flavor_resources/flavor1/res']
}
You should just be able to use android.sourceSets.flavor1.debug
instead if you need to.
Also note that according to the Android Gradle user guide, using srcDir
adds the directory to the default sources and srcDirs
replaces them.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…