Kotlin + Groovy : Groovy unable to resolve Kotlin class

plugins

(Sam Snyder) #1

I’m experimenting with replacing Gradle plugins currently written in Groovy with Kotlin.
In a such a project where Groovy class A references Kotlin class B, a gradlew clean build will fail with:

example\misc-gradle-tasks>gradlew clean build

> Task :compileKotlin
Using kotlin incremental compilation

> Task :compileGroovy
startup failed:
example\src\main\groovy\A.groovy: 44: unable to resolve class B
 @ line 44, column 5.
       @Internal
       ^

FAILURE: Build failed with an exception.

After some experimentation, I’ve found I can work around this by manually specifying this task dependency:

compileGroovy.dependsOn 'copyMainKotlinClasses'

Is there already a bug for this? Or is there some other step which should be taken to properly mix groovy and kotlin in the same project?

For reference:
Gradle version 4.0.1
sample buildscript:

plugins {
    id 'groovy'
    id("org.jetbrains.kotlin.jvm").version("1.1.3-2")
}
compileGroovy.dependsOn 'copyMainKotlinClasses'

dependencies {
    compile gradleApi()
    compile ("org.jetbrains.kotlin:kotlin-stdlib:1.1.3-2")
    
    testCompile gradleTestKit()
    testCompile ('org.spockframework:spock-core:1.1-groovy-2.4') {
        exclude module: 'groovy-all'
    }
}

(Sam Snyder) #2

I’ve also found that, unlike with Java, Kotlin classes are unable to reference Groovy classes in the same project, as Groovy is built subsequently to Kotlin.

I wonder, are there any plans to make migrating a Groovy project to Kotlin as easy as it is to migrate Java to Kotlin?