Sharing code between Android Instrumentation Tests and Unit Tests in Android Studio
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/).
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
}
}
}
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.
For multiple modules project
sourceSets {
test.java.srcDirs += ["${project(':module-name').projectDir}/src/sharedTest/java",
"src/test/java"]
}