Is there a way to only apply Kotlin for unit tests


#1

We have been told not to use Kotlin for the production code of our app, but there is no reason why we couldn’t use it in unit tests, so am wondering if there is an easy way to configure the gradle plugin for Android such that Kotlin is only supported in unit tests and will not compile for the main code?


#2

Isn’t it enough to use testCompile for declaring Kotlin dependency if you are using Gradle?


#3

Nope, it can still compile kotlin files in the main source directories.


#4

You can configure source sets for kotlin using src.main.kotlin (or test). By default they are equal to src.main.java + “src/main/kotlin”


#6

Using your information here is a working solution:

afterEvaluate {
   android.sourceSets.all { sourceSet ->
      if (!sourceSet.name.startsWith("test"))
      {
         sourceSet.kotlin.setSrcDirs([])
      }
   }
}