I add two modules in my AndroidStudio project:
app-base
|
|----src
|____androidTest
|________MyTestBase.java
app
|
|----src
|____androidTest
|________MyTest.java
Some common test class are defined in app-base's androidTest, and are used in app'androidTest.
I have tried to add the following code in app's build.gradle:
evaluationDependsOn(':app-base')
compile project(':app-base')
androidTestCompile project(':app-base')
I have include both app-base and app in settings.gradle, the output of gradlew projects is:
Root project 'MyProject'
+--- Project ':app-base'
+--- Project ':app'
No compile error by this way, but when I Run MyTest in ide, it said class MyTestBase is not found.
Do you know what's wrong? Any ideas are appreciated. Thanks.
I've been looking for a solution to this for a while and finally found something that works for me.
This will allow you to put common testing classes in another module.
android {
...
sourceSets {
androidTest.java.srcDirs += ["${project('match the module name that is currently in the dependencies').projectDir}/src/androidTest/java"]
}
}
So for the example above it would probably look something like
androidTest.java.srcDirs += ["${project(':appbase').projectDir}/src/androidTest/java"]
I suggest you the following strategy : create a new module test-utils
and put MyTestBase.java
in the main sources of this module.
test-utils
|
|----src
|____main
|________MyTestBase.java
Then you add this test-utils
as a test dependency in all modules where it is required
androidTestCompile project(':test-utils')
With little change in Stimsoni's answer, this is finally worked for me. I added this to my app gradle:
sourceSets {
test.java.srcDirs += ["${project(':app-base').projectDir}/src/test/java"]
}
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