【问题标题】:Android multi module test dependencyAndroid多模块测试依赖
【发布时间】:2018-09-01 08:34:19
【问题描述】:

使用 Android Studio 3.0 / android_gradle_version = '3.0.1' / gradle-4.5

假设我有两个 android 模块

module-base
module-a

当我想从 module-a 中的 module-base 访问源时,我只需要在我的 模块-a.gradle

dependencies {
implementation project(path: ':module-base')
}

但是,如果我想在 module-a 的测试中访问 module-base 的测试源这里不能像上面那样工作

dependencies {
testImplementation project(path: ':module-base')
}

我发现了很多建议(几年前),上面写着类似

    compileTestJava.dependsOn tasks.getByPath(':module-base:testClasses')
    testCompile files(project(':module-base').sourceSets.test.output.classesDir)

testCompile project(':module-base).sourceSets.test.classes

但是没有提到的作品。从编译器的角度来看总是有问题:-/

谁能帮我在两个模块之间创建 Android 测试代码依赖关系?

【问题讨论】:

标签: unit-testing android-studio android-gradle-plugin build.gradle gradle-plugin


【解决方案1】:

实际上,我找到了解决方法。不要使用来自 module-base 的测试源,而是使用来自测试相关模块 module-testutils 的源,它的定义如下

dependencies{
   testImplementation project(':module-testutils')
}

因此,您可以共享通用测试代码,这些代码将被排除在不可测试的 apk 中。

【讨论】:

  • module-testutils 到底是什么?我的意思是,它只是在 src/main/java 而不是 src/test/java 中创建的另一个模块吗?
  • 是的,对于我一直在使用的确切工作代码示例,只需看看这个 android 项目:github.com/kotomisak/image-analyzer-android
【解决方案2】:

万一其他人在这里结束,实现此目的的一种方法是将目标模块定义为源集。假设 test-mdoule 是我们要从中导入内容的模块,我们可以这样做:

android {
    sourceSets {
        // non-instrumental test example
        test {
            java.srcDir project(":test-module").file("src/test/java")
        }
        // instrumental test example
        androidTest {
            java.srcDir project(":test-module").file("src/androidTest/java")
        }
    }
}

参考:https://jonnycaley.medium.com/sharing-test-code-in-a-multi-module-android-project-7ce4de788016

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-19
    • 2010-12-16
    • 2015-03-25
    • 2015-07-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多