Compile Groovy and Kotlin?

I think this is not possible to compile Kotlin against Groovy in one module, but you can move Groovy (or Kotlin) in separate module. Groovy will be compiled in jar, and Kotlin now can depend on Groovy code. To help you i wrote simple project, and publish it on github.

upd 1:

I create new module and project with kotlin and groovy, and inspect tasks dependencies:

./gradlew -m kotlin-groovy:build
:kotlin-groovy:compileKotlin SKIPPED
:kotlin-groovy:compileJava SKIPPED
:kotlin-groovy:compileGroovy SKIPPED
:kotlin-groovy:processResources SKIPPED
:kotlin-groovy:classes SKIPPED
:kotlin-groovy:jar SKIPPED
:kotlin-groovy:assemble SKIPPED
:kotlin-groovy:compileTestKotlin SKIPPED
:kotlin-groovy:compileTestJava SKIPPED
:kotlin-groovy:compileTestGroovy SKIPPED
:kotlin-groovy:processTestResources SKIPPED
:kotlin-groovy:testClasses SKIPPED
:kotlin-groovy:test SKIPPED
:kotlin-groovy:check SKIPPED
:kotlin-groovy:build SKIPPED

BUILD SUCCESSFUL

Total time: 2.587 secs

Looks like change order of kotlin and groovy tasks enough. But if we change steps in this way:

./gradlew -m kotlin-groovy:build
:kotlin-groovy:compileGroovy SKIPPED
:kotlin-groovy:compileKotlin SKIPPED
:kotlin-groovy:compileJava SKIPPED
:kotlin-groovy:processResources SKIPPED
:kotlin-groovy:classes SKIPPED
:kotlin-groovy:jar SKIPPED
:kotlin-groovy:assemble SKIPPED
:kotlin-groovy:compileTestKotlin SKIPPED
:kotlin-groovy:compileTestJava SKIPPED
:kotlin-groovy:compileTestGroovy SKIPPED
:kotlin-groovy:processTestResources SKIPPED
:kotlin-groovy:testClasses SKIPPED
:kotlin-groovy:test SKIPPED
:kotlin-groovy:check SKIPPED
:kotlin-groovy:build SKIPPED

BUILD SUCCESSFUL

Total time: 2.745 secs

Build still doesn't work, because of Kotlin doesn't see Groovy.


It can be done like so:

After 4.10

Kotlin First:

//compileKotlin.dependsOn = compileKotlin.taskDependencies.values - 'compileJava'
compileGroovy.dependsOn compileKotlin
compileGroovy.classpath += files(compileKotlin.destinationDir)
classes.dependsOn compileGroovy

Before 4.10

Groovy First:

compileGroovy.dependsOn = compileGroovy.taskDependencies.values - 'compileJava'
compileKotlin.dependsOn compileGroovy
compileKotlin.classpath += files(compileGroovy.destinationDir)
classes.dependsOn compileKotlin

or Kotlin First:

compileKotlin.dependsOn = compileKotlin.taskDependencies.values - 'compileJava'
compileGroovy.dependsOn compileKotlin
compileGroovy.classpath += files(compileKotlin.destinationDir)
classes.dependsOn compileGroovy

To be clear, you get to choose whether your Kotlin code depends on Groovy or Groovy on Kotlin, but you don't get to have it both ways.