我有 2 个模块,模块 A 和模块 B。模块 B 依赖于模块 A,模块 A 通过使用 api
配置将依赖库共享给模块 B。
在设置测试环境时,在模块 A 内,我还使用 testApi
和 androidTestApi
使用共享测试库制作模块 B。但是,在运行 gradle sync 之后,我收到警告消息:WARNING: Configuration 'testApi' is obsolete and has been replaced with 'testImplementation'
。
阅读提供的 link它说其他模块不能依赖androidTest,如果你使用androidTestApi配置,你会收到以下警告
。因此,我必须在示例中的模块 B 中定义测试库以跳过此警告。
我对这种情况有一些疑问:
- 为什么一个模块不应该依赖于其他模块的测试依赖项,尽管它可以依赖于定义为
api
的正常依赖项? - 我们是否必须强制模块 B 依赖于模块 A 的测试库,而无需在模块 B 中再次定义这些库?
非常感谢
最佳答案
我这样做的方法是创建自定义配置。在你的情况下,在 build.gradle
文件模块 A 添加:
configurations {
yourTestDependencies.extendsFrom testImplementation
}
dependencies {
// example test dependency
testImplementation "junit:junit:4.12"
// .. other testImplementation dependencies here
}
并在模块B的build.gradle
中添加:
dependencies {
testImplementation project(path: ':moduleA', configuration: 'yourTestDependencies')
}
以上将包含所有在模块 A 中声明的 testImplementation
依赖项到模块 B。
关于Android gradle androidTestApi 和 testApi 配置已过时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52587491/