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.