且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Android Studio中的Android Instrumentation测试和单元测试之间共享代码

更新时间:2023-01-18 15:01:14

最后,我发现解决方案(解决方法)得益于Dan Lew的博客文章( http://blog.danlew.net/ 2015/11/02 / sharing-code-between-unit-tests-and-instrumentation-tests-on-android / )。

Finally I found the solution (workaround) thanks to a blog post from Dan Lew (http://blog.danlew.net/2015/11/02/sharing-code-between-unit-tests-and-instrumentation-tests-on-android/).


我提出的解决方案是利用源集来定义通用代码。首先,我将共享的测试代码放入src / sharedTest / java1中。

The solution I've come up with is to leverage source sets to define common code. First, I put my shared test code into src/sharedTest/java1 .



android {  
  sourceSets {
    String sharedTestDir = 'src/sharedTest/java'
    test {
      java.srcDir sharedTestDir
    }
    androidTest {
      java.srcDir sharedTestDir
    }
  }
}




上面的操作是将我的共享代码目录添加到两个测试和androidTest源码集。现在,除了它们的默认Java源代码之外,它们还将包含共享代码。

What it's doing above is adding my shared code directory to both the test and androidTest source sets. Now, in addition to their default Java sources, they'll also include the shared code.