Search code examples
javaandroidgradlesrc

Android Flavors : Multiple java and src directories for each flavor


Can anyone explain to me how I can use for each of my flavors more then just the main and the flavor specific java/src directories ? My goal is to have directories which are used by more than one flavor but not all of them.

For example I have 3 flavors : A1, A2 and B.

  • All favors use main/src (default main src directory)
  • A1 uses A1/src (default flavor src directory)
  • A2 uses A2/src (default flavor src directory)
  • B uses B/src (default flavor src directory)
  • A1 and A2 use A/src ("special" shared directory)

Is this possible ? If so, what should I put in my build.gradle file ?

And as a bonus question, can I chose in which order gradle goes looking for files in my different directories ?

For example if I have a.png declared in both A/src and A1/src, can I tell gradle to first look for this file in A/src, and only if nothing is found look for it in A1/src ?


Solution

  • As described here

    As mentioned above, each sourceSet can define multiple resource folders.

    You can define multiple resource folder. For example something like this:

    android {
         ...
         sourceSets {
                main {
                    //....
                    res.srcDirs = ['/src/main/res']
    
                }
                flavorA1 {
                     res.srcDirs = ['/src/flavor1/res', '/src/commonA/res']
                }
                flavorA2 {
                     res.srcDirs = ['/src/flavor2/res', '/src/commonA/res']
                }     
                //.....other flavors   
         }
    }