Search code examples
androidandroid-productflavors

Product Flavor structure


I have a project that uses ~30 product flavors. Apart from the "main" code, the flavors don't have much to them, just some unique resources. Since there are so many, I would like to structure my code directories like this to make it more organized:

  • src
    • main
      • java
      • res
    • productFlavors
      • flavor1
        • java
        • res

But the build doesn't recognize the flavors when they are on a different level than the "main" folder. Is there any way I can make this work?


Solution

  • I have a project that uses ~30 product flavors

    One more, and you'll match Baskin-Robbins' original ~31 flavors.

    But the build doesn't recognize the flavors when they are on a different level than the "main" folder. Is there any way I can make this work?

    You can override where source comes from in your module's build.gradle file. While I haven't tried it for your scenario (only for building Eclipse-style projects using Gradle), something like this should work:

    android {
        // lots of cool stuff here
    
        sourceSets {
            flavor1 {
                manifest.srcFile 'src/productFlavors/flavor1/AndroidManifest.xml'
                java.srcDirs = ['src/productFlavors/flavor1/java']
                aidl.srcDirs = ['src/productFlavors/flavor1/aidl']
                res.srcDirs = ['src/productFlavors/flavor1/res']
                assets.srcDirs = ['src/productFlavors/flavor1/assets']
            }
    
            // lather, rinse, repeat
        }
    }
    

    If you stick to the fixed pattern of src/productFlavors/.../ (where ... is the flavor name), you can probably use a bit of Groovy scripting to iterate over an array of flavor names and wire up the sourcesets accordingly.