简体   繁体   English

使用 gradle DSL 在多模块项目中声明风味依赖项

[英]Declaring flavor dependencies in multi-module project using gradle DSL

Suppose:认为:

  • Module M (Android Library) has flavors M1 and M2模块M (Android 库)具有M1M2风格
  • Feature Module F depends on Module M功能模块F依赖于模块M
  • App A depends on feature module F and has flavors A1 and A2 (different dimension to those in M )应用A依赖于功能模块F并具有A1A2风格(与M中的不同维度)
  • Furthermore, App A flavor A1 depends on M1 , and A2 depends on M2此外,App A flavor A1依赖于M1 ,而A2依赖于M2

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.特别是考虑到模块FA1A2的风格一无所知。

EDIT WITH SOME EXTRA INFO : in my particular case, M1 and M2 are only for bringing in specific assets.编辑一些额外的信息:在我的特殊情况下, M1M2仅用于引入特定资产。 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模块中引入F1F2风格(我猜是 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的风格时自动为FM选择正确的风格。

As you asked, F knows nothing about A1 and A2 .如您所问, FA1A2一无所知。 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您只需将您的风味描述从:app gradle 复制到:module gradle 并更改路径
my files:我的文件:
app:应用程序:
在此处输入图像描述

login module:登录模块: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM