In our Android project we code in Kotlin and target java 1.6. However we are forced to java 1.8 in our test since some JUnit5 features requires it (static methods in interfaces).
Is it possibe to compile the tests differently than the production code?
We tried to raise the jvmTarget to 1.8 by adding this to our build.gradle:
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
kotlinOptions {
jvmTarget = "1.8"
}
}
This raises the jvmTarget of the production code too, but we only want it for our tests. The Docs indicate that it can be specified for the test like this:
compileReleaseUnitTestKotlin{
kotlinOptions {
jvmTarget = "1.8"
}
}
Unfortunatly the build.gradle doesn't compile.
The Kotlin docs mention how to do it for tests only.
Groovy DSL:
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
Kotlin DSL:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
val compileTestKotlin: KotlinCompile by tasks
compileTestKotlin.kotlinOptions.jvmTarget = "1.8"
This snippet does the job for me:
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) {
if (it.name.contains("UnitTest")) {
kotlinOptions {
jvmTarget = "1.8"
}
}
}
I have to say, It's not the best solution to filter the test tasks based on their names, so think of this as a quick and dirty aid.