Search code examples
androidgradlebuild.gradlegradle-kotlin-dslandroid-build-flavors

How do you combine multiple gradle flavors or exclude one?


flavorDimensions("color")

productFlavors {
    register("red") {
        setDimension("color")
    }
    register("blue") {
        setDimension("color")
    }
}

redImplementation("red library")
blueImplementation("blue library")

This generates the build variants: blueDebug, blueRelease, redDebug, redRelease

But in addition to these i want a build variant that is a combination of these two so the final list will be: blueDebug, blueRelease, redDebug, redRelease, blueRedDebug, blueRedRelease

If you have them in different flavordimensions you end up with just the blueRed combination.

How do i setup my gradle script to support this case? The end goal is to able to choose if you want just the red library, just the blue library or the combination of both.


Solution

  • Since you already have the blue and red library modules, your app can have these flavors:

    • blue, which depends on blue library
    • red, which depends on red library
    • blueRed, which depends on blue and red libraries

    I'm assuming that blue and red libraries can be included side by side.

    flavorDimensions("color")
    
    productFlavors {
        register("red") {
            setDimension("color")
        }
        register("blue") {
            setDimension("color")
        }
        register("blueRed") {
            setDimension("color")
        }
    }
    
    redImplementation("red library")
    blueImplementation("blue library")
    blueRedImplementation("red library")
    blueRedImplementation("blue library")