【问题标题】:Android gradle androidTestApi & testApi configuration obsoletedAndroid gradle androidTestApi & testApi 配置已过时
【发布时间】:2023-04-03 00:52:02
【问题描述】:

我有 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,它说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 中再次定义这些库?

非常感谢

【问题讨论】:

  • 嘿!您找到以下问题的任何解决方案了吗?我知道某些模块不能依赖于 androidTestApi ,这很好。但是我找不到 testApi 也过时的充分理由。
  • 我到现在也没有找到任何解决方案:(

标签: android testing android-gradle-plugin


【解决方案1】:

我这样做的方法是创建自定义配置。在您的情况下,在 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。

【讨论】:

  • 假设我有远程库而不是模块 A,我如何使用远程库中声明的自定义配置?
  • 对于远程库,我认为使用testApi,androidTestApi是正确的。但它不起作用。
  • 这是为了依赖。从模块 B(演示)访问模块 A(域)的类/方法怎么样?
【解决方案2】:

这是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"))
}

【讨论】:

    【解决方案3】:

    为了补充接受的答案,在您的基本模块 :core 添加配置闭包:

        // build.gradle (core module)
        configurations {  
            testDependencies.extendsFrom testImplementation  
            testRuntimeOnlyDependencies.extendsFrom testRuntimeOnly  
        }
        
        dependencies {
            def junit5_version = "5.6.0"
    
            // dependencies
            testImplementation "org.junit.jupiter:junit-jupiter-api:$junit5_version"  
            testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junit5_version"  
            testImplementation "org.junit.jupiter:junit-jupiter-params:$junit5_version" 
            testRuntimeOnly "org.junit.vintage:junit-vintage-engine:$junit5_version"
        }
        
    

    然后在你的feature 模块中添加:

        dependencies {
            implementation project(':core') {  
                testImplementation configurations.getByName('testDependencies').allDependencies  
                testRuntimeOnly configurations.getByName('testRuntimeOnlyDependencies').allDependencies  
            }
        }
    

    现在您的测试依赖项已跨模块共享。

    注意:模块corefeature是虚构的,请相应替换。

    【讨论】:

    • 不错的解决方案!但它在android中不起作用应用程序模块。你知道解决这个问题的方法吗?
    • @AouledIssa 该解决方案不适用于app 模块,因为应用程序模块不是共享模块,因此您唯一的选择是创建一个可重用的模块供此类使用-案例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-19
    • 1970-01-01
    • 2013-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多