Android gradle androidTestApi 和 testApi 配置已过时

标签 android testing android-gradle-plugin

我有 2 个模块,模块 A 和模块 B。模块 B 依赖于模块 A,模块 A 通过使用 api 配置将依赖库共享给模块 B。

在设置测试环境时,在模块 A 内,我还使用 testApiandroidTestApi 使用共享测试库制作模块 B。但是,在运行 gradle sync 之后,我收到警告消息:WARNING: Configuration 'testApi' is obsolete and has been replaced with 'testImplementation'

阅读提供的 link它说其他模块不能依赖androidTest,如果你使用androidTestApi配置,你会收到以下警告。因此,我必须在示例中的模块 B 中定义测试库以跳过此警告。

我对这种情况有一些疑问:

  1. 为什么一个模块不应该依赖于其他模块的测试依赖项,尽管它可以依赖于定义为 api 的正常依赖项?
  2. 我们是否必须强制模块 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/

相关文章:

java - 如何在行为级别上测试我的 Java/Android 代码?

java - Spinner 中的自定义行

android - 如何设计其中一个屏幕是登录屏幕的流程?

android - 确定连接到 android wifi 热点的每个客户端使用的总数据

Android App Bundles - 是否可以在一个版本中上传多个应用程序包?

java - 如何使用 selenium webdriver 找到这个元素?

unit-testing - 用 jest 测试 Vue 过滤器

android - 使用 gradle 从存储库下载 apk

android - 与项目 'com.android.support:support-annotations' 中的依赖项 ':app' 冲突。应用程序和测试应用程序的已解决版本不同

安卓工作室 : Cannot resolve symbol 'SwipeRefreshLayout'