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.

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 ?

Upvotes: 8

Views: 6433

Answers (2)

Jon
Jon

Reputation: 9803

I've found a slightly different format is required for setting java source files

sourceSets {
    favorA1 {
        java {
            srcDirs('src/favorA1/java/src', 'src/commonA/java/src')
        }
    }
    favorA2 {
        java {
            srcDirs('src/favorA2/java/src', 'src/commonA/java/src')
        }
    }
}

Gradle Java
Gradle Source Api

Upvotes: 3

Gabriele Mariotti
Gabriele Mariotti

Reputation: 363825

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   
     }
}

Upvotes: 12

Related Questions