android - Gradle 多模块依赖 - 一起使用 api 和 testImplementation

标签 android gradle

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

// testshared/build.gradle

api libraries.lib1
api libraries.lib2

我在 moduleA 中使用它与 testImplementation 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" .
debugImplementation有时也需要,但 testApi只会导致:

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

它总是一个无用的配置,因为每个应用程序模块只有一个测试应用程序。使用 testImplementation连同 api只能在应用程序模块中完成。

关于android - Gradle 多模块依赖 - 一起使用 api 和 testImplementation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59663319/

相关文章:

gradle - Gradle:如何修改动态创建的任务?

android - 在给定矩形边界的情况下以编程方式缩放和居中 imageView

android - 定位服务是否需要 Internet 才能获取位置?

android - 动画可见性模式,GONE 和 VISIBLE

docker - 通过docker文件安装Gradle

android - appcompat-v7 :28. 0.0-rc01 构建问题

gradle - RedPhone构建失败

java - 如何准确地设置一个十进制值为 float 的搜索栏进度?

Android将创建的位图保存到SD卡上的目录

gradle - 如何从gradle中 checkout 并构建maven项目?