[英]Declaring flavor dependencies in multi-module project using gradle DSL
Suppose:认为:
Is it possible to configure such dependencies in Gradle Kotlin DSL?是否可以在 Gradle Kotlin DSL 中配置此类依赖项? Especially considering module F does not know anything about A1 and A2 flavors.
特别是考虑到模块F对A1和A2的风格一无所知。
EDIT WITH SOME EXTRA INFO : in my particular case, M1 and M2 are only for bringing in specific assets.编辑一些额外的信息:在我的特殊情况下, M1和M2仅用于引入特定资产。 So F does not care about those and so just has a general dependency on M .
所以F不关心这些,所以对M有一般的依赖。
I was able to solve your problem with missingDimensionStrategy
.我能够使用
missingDimensionStrategy
解决您的问题。
Here's the abbreviated scripts:这是缩写的脚本:
module/build.gradle.kts模块/build.gradle.kts
android {
flavorDimensions("module-dimension")
productFlavors {
create("module1") {
setDimension("module-dimension")
}
create("module2") {
setDimension("module-dimension")
}
}
}
feature/build.gradle.kts功能/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).我不得不在F模块中引入F1和F2风格(我猜是 android 库模块)。 You say that "Feature Module F depends on Module M ", but which flavor of M ?
您说“功能模块F依赖于模块M ”,但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应用程序/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.如果F仅依赖于M ,那么您可以通过将F的风味和风味维度重命名为与M风味相同来稍微简化这一点。 This way you can avoid having to specify
missingDimensionStrategy
in F , and use only one of them instead of two in A .这样您就可以避免在F中指定
missingDimensionStrategy
,并且只使用其中一个而不是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 .您会看到 Android Studio 会在您更改A的风格时自动为F和M选择正确的风格。
As you asked, F knows nothing about A1 and A2 .如您所问, F对A1和A2一无所知。 I hope this answers your question.
我希望这回答了你的问题。 If not, leave a comment.
如果没有,请发表评论。 Here's a sample project showing this .
这是一个显示此的示例项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.