Similar question: Sharing code between Android Instrumentation Tests and Unit Tests in Android Studio
My setup is the following:
src/test
folder that contains unit tests. These can be either Java or Kotlin classessrc/androidTest
that contains instrumentation tests. These can also be either Java or Kotlin classessrc/sharedTest
is a folder that contains a bunch of utils that are shared between unit and instrumentation tests.This sharing is defined in gradle as:
sourceSets {
test.java.srcDirs += 'src/sharedTest/java'
androidTest.java.srcDirs += 'src/sharedTest/java'
}
This allows any Java class in src/test
or src/androidTest
to access the utils. but not the Kotlin unit tests. My assumption is that they are not added to the sourceSets.
My question is: how can I add them? I tried:
sourceSets {
test.kotlin.srcDirs += 'src/sharedTest/java'
}
But that doesn't seem to work.
The default setup would be making the Kotlin source sets visible to the Java compiler and the IDE as well:
android {
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
test.java.srcDirs += 'src/test/kotlin'
test.java.srcDirs += 'src/sharedTest/java'
androidTest.java.srcDirs += 'src/sharedTest/java'
}
}
You don't need to configure the Kotlin source sets by itself.
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