Gradle flavors for android with custom source sets – what should the gradle files look like?

I think you’d be better off not defining custom sourceSets but using the default gradle configuration. I used to do custom sourcesets until I realized the conventions are, well, convenient.

You’ll want something like this:

+ src
    + main // this is your common code
        + java 
        + res
    + flavor1
        + java
        + res
    + flavor2
        + java
        + res

Then you can just go ahead and remove the sourcesets closure from your build.gradle

NOTE: For the gradle configuration, resources are merged or overridden whereas all java code is put on the same class-path. For example, the AndroidManifest.xml files for each flavor need only have the differences from main‘s manifest file. Having an asset, for example, ic_launcher in a flavor overrides the ic_launcher from main if such file exists. However, having a file HomeActivity.java in both main and the flavor is not possible and will give a duplicate file error.

Leave a Comment