Gradle 多模块依赖 - 使用 api 和测试一起实现



我有一个只包含测试依赖项的模块,我将这些依赖项公开为api

// testshared/build.gradle
api libraries.lib1
api libraries.lib2

我用它来moduleAtestImplementation project(':testshared')

这是否等同于创建从testImplementation扩展的自定义配置并像这样使用它:

// testshared/build.gradle
configurations {
myTestDependencies.extendsFrom testImplementation
}
dependencies {
testImplementation libraries.lib1
testImplementation libraries.lib2
// .. other testImplementation dependencies here
}
// moduleA/build.gradle
testImplementation project(path: ':testshared', configuration: 'myTestDependencies')

如这里所见 https://stackoverflow.com/a/53649718/8681368 ?

testImplementation的情况下,它足以作为应用程序模块的依赖项。否则api只应在多个模块中引用相同的依赖项时使用(甚至必须使用(。testImplementation的行为与常规模块依赖项不同,因为它们适用于整个其他(测试(应用程序。如果没有应用程序模块,则无法进行集成测试,因为它需要 2 个应用程序。库本身只能进行单元测试。

如果您想有一个额外的文件,只需apply from: "testshared/build.gradle".

有时也需要debugImplementationtestApi但这只会导致:

Configuration 'testApi' is obsolete and has been replaced with 'testImplementation'.

它始终是一个无用的配置,因为每个应用程序模块只有 1 个测试应用程序。testImplementationapi一起使用只能在应用程序模块中完成。

最新更新