假设我有一个具有三种构建类型的 Android 应用:
buildTypes {
release {
....
}
optRelease {
....
}
debug {
....
}
}
我有一个依赖模块:
dependencies {
implementation project(':myDependency')
}
假设这个依赖项只有两种构建类型(比如 debug
和 release
),我想完全控制我的应用程序的哪个构建类型使用哪个依赖项的构建类型。例如,我希望我的应用程序的 optRelease
使用库的 release
,而应用程序的 release
使用库的 debug
.
在 Android Studio 3.0 之前这曾经是可能的,但新的构建变体系统似乎不再允许这样做了。
如何明确说明要使用的构建类型?假设我无法控制依赖项,也无法修改其 gradle 配置。
最佳答案
你可以使用matchingFallback
buildTypes {
release {
matchingFallbacks = ['debug']
...
}
optRelease {
matchingFallbacks = ['release']
...
}
debug {
matchingFallbacks = ['debug']
...
}
}
您还可以指定回退列表,如下所示:
optRelease {
matchingFallbacks = ['release','debug']
}
This will specify a sorted list of fallback build types that the plugin should try to use when a dependency does not include a "optRelease" build type. You may specify as many fallbacks as you like, and the plugin selects the first build type that's available in the dependency.
可以引用official document了解更多详情。
但是,如果您想在针对外部依赖项时使用特定于变体的配置。您可以按如下方式进行:
debugImplementation 项目(':myDependency')
这将使 myDependency
仅依赖于模块的“调试”版本。
关于android - 在依赖项中选择特定的构建类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50771532/