Compile Groovy and Kotlin?

Viewed 5557

I'm working on a small project with Groovy and Kotlin, and my Kotlin code depends on my Groovy code, not the other way around. However, Kotlin compiles my code first instead of Groovy, and, as a result, I get errors like Unresolved reference: SiteRepository

Any suggestions how I can fix this, by either changing the build sequence, or Kotlin depending explicitly on Groovy, or any other suggestion?

4 Answers

When Gradle 6.1 is released proposed solutions above will not work. You can use a new way of how to solve the original issue.

tasks.named('compileGroovy') {
    // Groovy only needs the declared dependencies
    // and not the output of compileJava
    classpath = sourceSets.main.compileClasspath
}
tasks.named('compileKotlin') {
    // Kotlin also depends on the result of Groovy compilation 
    // which automatically makes it depend of compileGroovy
    classpath += files(sourceSets.main.groovy.classesDirectory)
}

And with Kotlin:

tasks.named<GroovyCompile>("compileGroovy") {
    // Groovy only needs the declared dependencies
    // and not the output of compileJava
    classpath = sourceSets.main.get().compileClasspath
}
tasks.named<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>("compileKotlin") {
    // Kotlin also depends on the result of Groovy compilation 
    // which automatically makes it depend of compileGroovy
    classpath += files(conventionOf(sourceSets.main.get()).getPlugin(GroovySourceSet::class.java).groovy.classesDirectory)
}

https://docs.gradle.org/6.1-rc-1/release-notes.html#compilation-order

For Gradle 5.2.1 if you want to call Groovy from Kotlin:

compileGroovy.dependsOn.remove('compileJava')
compileKotlin.dependsOn compileGroovy
compileKotlin.classpath += files(compileGroovy.destinationDir)
Related