【问题标题】:Gradle variant-aware dependency management for test-only dependency用于仅测试依赖项的 Gradle 变体感知依赖项管理
【发布时间】:2020-02-29 17:59:47
【问题描述】:
我有一个多模块 Android 项目,该项目有一个模块,该模块仅用于通过 testImplementation project(path: 'libtestsupport') 进行测试。除非我通过implementation project(path: 'libtestsupport') 为非测试构建包含此模块,否则自动变体切换似乎不起作用,我不想这样做,因为它只被测试使用。这会导致此库模块的构建变体与其他模块不匹配:
有没有办法让这个库依赖项像应用程序中的其他依赖项一样自动切换构建变体?
【问题讨论】:
标签:
android
gradle
android-productflavors
android-build-flavors
build-variant
【解决方案1】:
如何解决:Resolve build errors related to dependency matching
构建错误的原因:
您的应用包含库依赖项没有的构建类型。
例如,您的应用包含“staging”构建类型,但依赖项仅包含“debug”和“release”构建类型。
请注意,当库依赖项包含您的应用不包含的构建类型时,没有问题。那是因为插件根本不会从依赖项中请求该构建类型。
分辨率
使用 matchingFallbacks 为给定的构建类型指定替代匹配,如下所示:
// In the app's build.gradle file.
android {
buildTypes {
debug {}
release {}
staging {
// Specifies a sorted list of fallback build types that the
// plugin should try to use when a dependency does not include a
// "staging" 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.
matchingFallbacks = ['debug', 'qa', 'release']
}
}
}