Declaring flavor dependencies in multi-module project using gradle DSL

Viewed 1663

Suppose:

  • Module M (Android Library) has flavors M1 and M2
  • Feature Module F depends on Module M
  • App A depends on feature module F and has flavors A1 and A2 (different dimension to those in M)
  • Furthermore, App A flavor A1 depends on M1, and A2 depends on M2

Is it possible to configure such dependencies in Gradle Kotlin DSL? Especially considering module F does not know anything about A1 and A2 flavors.

EDIT WITH SOME EXTRA INFO: in my particular case, M1 and M2 are only for bringing in specific assets. So F does not care about those and so just has a general dependency on M.

2 Answers

I was able to solve your problem with missingDimensionStrategy.

Here's the abbreviated scripts:

module/build.gradle.kts

android {
    flavorDimensions("module-dimension")
    productFlavors {
        create("module1") {
            setDimension("module-dimension")
        }
        create("module2") {
            setDimension("module-dimension")
        }
    }
}

feature/build.gradle.kts

android {
    flavorDimensions("feature-dimension")
    productFlavors {
        create("feature1") {
            setDimension("feature-dimension")
            // Tells gradle to use "module1" flavor of the "module" module for this flavor.
            missingDimensionStrategy("module-dimension", "module1")
        }
        create("feature2") {
            setDimension("feature-dimension")
            missingDimensionStrategy("module-dimension", "module2")
        }
    }
}

dependencies {
    implementation(project(":module"))
}

I had to introduce flavors F1 and F2 to the F module (which I guessed is an android library module). You say that "Feature Module F depends on Module M", but which flavor of M? I guessed that it was same flavor that app depends on. In any case, if you don't provide flavor specific code in the feature module the result is the same.

app/build.gradle.kts

android {
    flavorDimensions("app-dimension")
    productFlavors {
        create("app1") {
            setDimension("app-dimension")
            missingDimensionStrategy("module-dimension", "module1")
            missingDimensionStrategy("feature-dimension", "feature1")
        }
        create("app2") {
            setDimension("app-dimension")
            missingDimensionStrategy("module-dimension", "module2")
            missingDimensionStrategy("feature-dimension", "feature2")
        }
    }
}

dependencies {
    implementation(project(":module"))
    implementation(project(":feature"))
}

If F only depends on M, then you can simplify this a little by renaming F's flavors and flavor dimension to be the same as the M flavors. This way you can avoid having to specify missingDimensionStrategy in F, and use only one of them instead of two in A. That's because when flavors match across modules, the build system resolves the correct flavor automatically.

You'll see that Android Studio automatically selects the correct flavor for F and M when you change the flavor of A.

As you asked, F knows nothing about A1 and A2. I hope this answers your question. If not, leave a comment. Here's a sample project showing this.

faced same issue
you need just copy your flavors descriptions from :app gradle to :module gradle and change paths
my files:
app:
enter image description here

login module: enter image description here

Related