I’ve been trying to understand how the Kotlin Gradle plug-in works and I’m a bit puzzled.
For example, I created a small project where a Kotlin file depends on a Java file:
// src/main/kotlin/example
package example
fun main(argv: Array<String>) {
println("KotlinMain calling JavaClass().run()")
JavaClass().run()
}
// src/main/java/example
package example;
public class JavaClass {
public void run() {
System.out.println("JavaClass run()");
}
}
Not surprisingly, if I try to compile the Kotlin file alone and manually, it fails:
$ kotlinc src/main/kotlin/example/KotlinMain.kt
src/main/kotlin/example/KotlinMain.kt:5:2: error: unresolved reference: JavaClass
JavaClass().run()
However, if I launch the build with Gradle, something interesting happens:
$ ./gradlew clean
$ ./gradlew compileKotlin
$ find build/classes/main/example/KotlinMainKt.class
build/classes/main/META-INF/mixed-compileKotlin.kotlin_module
build/kotlin-classes/main/example/KotlinMainKt.class
build/kotlin-classes/main/META-INF/mixed-compileKotlin.kotlin_module
Somehow, Kotlin was able to compile KotlinMain
without ever needing JavaClass
!
I suspect there is some magic happening with the compilation that I don’t follow. Maybe the compiler can be told to compile despite missing symbols?
Can somebody explain how the Kotlin Gradle plug-in manages to do this? I’d like to replicate this with Kobalt…
Thanks!