Android Gradle androidTestApi和testApi配置已过时

Vin*_*yen 9 testing android 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'

阅读提供的链接,并说other modules can't depend on androidTest, you get the following warning if you use the androidTestApi configuration。因此,我必须在示例中在模块B中定义测试库,以跳过此警告。

我对此情况有一些疑问:

  1. 为什么一个模块尽管可以依赖于定义为的正常依赖关系,却不应该依赖于测试另一个模块的依赖关系api
  2. 无论如何,我们是否有强迫模块B依赖模块A的测试库而不在模块B中再次定义这些库的问题?

非常感谢

Chr*_*nis 10

我这样做的方法是通过创建自定义配置。在您的情况下,在build.gradle文件模块A中添加:

configurations {
    yourTestDependencies.extendsFrom testImplementation
}

dependencies {
    // example test dependency
    testImplementation "junit:junit:4.12"
    // .. other testImplementation dependencies here
}
Run Code Online (Sandbox Code Playgroud)

并在build.gradle模块B的中添加:

dependencies {
    testImplementation project(path: ':moduleA', configuration: 'yourTestDependencies')
}
Run Code Online (Sandbox Code Playgroud)

上面将包括testImplementation在模块A到模块B中声明的所有依赖项。


ASP*_*ASP 5

这是Chris Margonis(感谢!)在 Kotlin-DSL 中的回答:

// "base" module/project
configurations {
    create("testDependencies"){
        extendsFrom(configurations.testImplementation.get())
    }
}

dependencies {
    // example test dependency
    testImplementation "junit:junit:4.12"
    // .. other testImplementation dependencies here
}

//another module
dependencies {
    testImplementation(project(path = ":base", configuration = "testDependencies"))
}
Run Code Online (Sandbox Code Playgroud)