首页 > 解决方案 > 用于仅测试依赖项的 Gradle 变体感知依赖项管理

问题描述

我有一个多模块 Android 项目,其中包含一个仅用于测试的模块testImplementation project(path: 'libtestsupport')。除非我通过 为非测试构建包含此模块,否则自动变体切换似乎不起作用,implementation project(path: 'libtestsupport')我不想这样做,因为它只被测试使用。这导致此库模块的构建变体与其他模块不匹配:

构建变体不匹配

有没有办法让这个库依赖项像应用程序中的其他依赖项一样自动切换构建变体?

标签: androidgradleandroid-productflavorsandroid-build-flavorsbuild-variant

解决方案


如何解决:解决与依赖匹配相关的构建错误

构建错误的原因:

您的应用程序包含库依赖项不包含的构建类型。

例如,您的应用程序包含“暂存”构建类型,但依赖项仅包含“调试”和“发布”构建类型。

请注意,当库依赖项包含您的应用程序不包含的构建类型时,没有问题。那是因为插件根本不会从依赖项中请求该构建类型。

解析度

使用 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']
        }
    }
}

推荐阅读