使用Gradle模块作为具有特定flavor的依赖项。

7
我有一个名为library的库模块,它有两种flavor flavor1flavor2。然后我有两个应用程序模块,让我们称它们为app1app2,它们将使用此库模块,每个应用程序将使用特定的flavor..例如,app1将始终使用带有flavor1风格的library。我应该如何配置gradle依赖项才能实现我想要的效果?如果我只在app1 build.gradle中尝试implementation project(':library'),显然不起作用,因为它不知道选择哪个flavor..所以我尝试了一些类似implementation project(path: ':library', configuration: 'flavor1Release')这样的东西,但也不起作用,它打印出很多错误。
ERROR: Unable to resolve dependency for ':app1@debug/compileClasspath': Could not resolve project :library.
Show Details
Affected Modules: app1

ERROR: Unable to resolve dependency for ':app1@debugAndroidTest/compileClasspath': Could not resolve project :library.
Show Details
Affected Modules: app1

ERROR: Unable to resolve dependency for ':app1@debugUnitTest/compileClasspath': Could not resolve project :library.
Show Details
Affected Modules: app1

ERROR: Unable to resolve dependency for ':app1@release/compileClasspath': Could not resolve project :library.
Show Details
Affected Modules: app1

ERROR: Unable to resolve dependency for ':app1@releaseUnitTest/compileClasspath': Could not resolve project :library.
Show Details
Affected Modules: app1

我在Stack Overflow上看到一些问题,比如这里,说要将库模块的flavor复制到主应用程序模块,但这对我来说没有意义,app1不知道任何关于flavor2的事情,我不想在那里引入它。

可能是基于Android Gradle中多味库的多味应用程序的重复问题。 - Jeel Vankhede
1
@JeelVankhede,这是将口味复制到所有模块的解决方案,在我的情况下没有意义。 - Billda
1个回答

10
你应该为app1app2都定义missingDimensionStrategy,例如:
// app1/build.gradle

 defaultConfig {
    missingDimensionStrategy 'library', 'flavor1'
}

// app2/build.gradle

 defaultConfig {
    missingDimensionStrategy 'library', 'flavor2'
}

你是对的!只需要做一个小改变,在你的例子中,'library' 应该指定 flavor dimension :) - Billda

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接