With Android Studio 3.0 / android_gradle_version = '3.0.1' / gradle-4.5
Let's say I have two android modules
module-base
module-a
When I want to access sources from module-base in module-a , I just need to write this in my module-a.gradle
dependencies {
implementation project(path: ':module-base')
}
But, what if I want to access test sources from module-base in test of module-a? Here does not work approach like above
dependencies {
testImplementation project(path: ':module-base')
}
I found lot of advices (few years old) which says something like
compileTestJava.dependsOn tasks.getByPath(':module-base:testClasses')
testCompile files(project(':module-base').sourceSets.test.output.classesDir)
or
testCompile project(':module-base).sourceSets.test.classes
But no one from mentioned works. There is always something wrong from the compiler point of view :-/
Can you someone help me how to create Android test code dependency between two modules?
A project with multiple Gradle modules is known as a multi-module project.
When should you use the androidTest directory to store your test classes? when the tests consist only of unit tests. when the number of tests to run is large(500+).
Use the command ./gradlew test to run all tests.
Note: Instrumented test, also known as instrumentation tests, are initialized in a special environment that gives them access to an instance of Instrumentation. This class provides access to the application context and APIs to manipulate the app under test and gives instrumented tests their name.
Actually I find just workaround for this. Don't use test sources from module-base but use sources from test related module module-testutils which is defined like this
dependencies{
testImplementation project(':module-testutils')
}
Thus you can share common test code which will be excluded for non-testable apk.
In case anyone else ends up here, one way to accomplish this is to define the target module as a source set. Let's assume test-mdoule is the module we want to import stuff from, we can do it this way:
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")
}
}
}
Reference: https://jonnycaley.medium.com/sharing-test-code-in-a-multi-module-android-project-7ce4de788016
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With