Android: Are there any methods to share a resource betwwen different falvors and sourceset?

179 views Asked by At

I have an application with below source sets and flavors: flavorDimensions "brand"

productFlavors {
flavor1 {
    dimension "brand"
    sourceSets {
        main {
            manifest.srcFile "src/main/flavor1/AndroidManifest.xml"
            resources.srcDirs = ['src/flavor1/']

        }
    }
}

flavor2 {
    dimension "brand"
    sourceSets {
        main {
            manifest.srcFile "src/main/flavor2/AndroidManifest.xml"
            resources.srcDirs = ['src/flavor2/']

        }
    }
}
flavor3 {
    dimension "brand"
    sourceSets {
        main {
            manifest.srcFile "src/main/flavor3/AndroidManifest.xml"
            resources.srcDirs = ['src/flavor3/']

        }
    }
}

My application is such that 99% of layouts and classes are the same. E.g. only layout file main_activity.xml is different among flavors but the other 20~30 layout files are completely the same. I don't want to copy these 20~30 in three different flavor source set. Are there any ways to share them among three flavors and if a file was present in source set, it overrides the share files? (Such as want android does for drawable and drawble-hdpi , ...)? So I don't want to have

1

There are 1 answers

1
Crispert On

Unless I have misunderstood the question, you could solve the issue by changing build.gradle like this:

productFlavors {

    flavor1 {
        dimension "brand"
    }

    flavor2 {
        dimension "brand"
    }

    //...
}

and structuring your app module like this:

/src
    /main
    /flavor1
    /flavor2
   ...

and placing all common files(source, resources) in main and the custom/specific files in the flavorX folders which will cause gradle to replace the identically named files in main when each flavor is built.