# Puzzling behavior from the Kotlin Gradle plug-in

**URL:** <https://discuss.kotlinlang.org/t/puzzling-behavior-from-the-kotlin-gradle-plug-in/1441>\
**Category:** Support\
**Created:** [February 1, 2016, 5:47pm UTC](https://discuss.kotlinlang.org/t/puzzling-behavior-from-the-kotlin-gradle-plug-in/1441 "2016-02-01T17:47:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cedricbeust](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/cedricbeust/32/1429_2.png) [@cedricbeust](https://discuss.kotlinlang.org/u/cedricbeust)\
**Post date:** [February 1, 2016, 5:47pm UTC](https://discuss.kotlinlang.org/t/puzzling-behavior-from-the-kotlin-gradle-plug-in/1441/1 "2016-02-01T17:47:45Z")

</div>

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:

```auto
// src/main/kotlin/example
package example

fun main(argv: Array<String>) {
	println("KotlinMain calling JavaClass().run()")
	JavaClass().run()
}

```

```auto
// 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:

```auto
$ 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:

```auto
$ ./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!

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [February 1, 2016, 5:51pm UTC](https://discuss.kotlinlang.org/t/puzzling-behavior-from-the-kotlin-gradle-plug-in/1441/2 "2016-02-01T17:51:07Z")

</div>

The Kotlin compiler is capable of parsing Java source code. In your first example, you specified only the path to the Kotlin file, so it wasn’t able to locate the dependency. In the second example, the Java source code is part of the project, so the Gradle plugin passes it to the compiler.

---

<div class="post-metadata">

**Author:** ![cedricbeust](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/cedricbeust/32/1429_2.png) [@cedricbeust](https://discuss.kotlinlang.org/u/cedricbeust)\
**Post date:** [February 1, 2016, 5:52pm UTC](https://discuss.kotlinlang.org/t/puzzling-behavior-from-the-kotlin-gradle-plug-in/1441/3 "2016-02-01T17:52:34Z")

</div>

That explains it.

How do I pass this source directory to the compiler?

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [February 1, 2016, 6:03pm UTC](https://discuss.kotlinlang.org/t/puzzling-behavior-from-the-kotlin-gradle-plug-in/1441/4 "2016-02-01T18:03:22Z")

</div>

CompilerConfiguration.addJavaSourceRoot()

---

<div class="post-metadata">

**Author:** ![cedricbeust](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/cedricbeust/32/1429_2.png) [@cedricbeust](https://discuss.kotlinlang.org/u/cedricbeust)\
**Post date:** [February 1, 2016, 6:13pm UTC](https://discuss.kotlinlang.org/t/puzzling-behavior-from-the-kotlin-gradle-plug-in/1441/5 "2016-02-01T18:13:33Z")

</div>

Thanks.

I couldn’t find any args on the compiler to set this. I’m invoking `K2VMCompiler` directly, what’s the easiest way to add these directories to `K2VMCompiler`?

---

<div class="post-metadata">

**Author:** ![cedricbeust](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/cedricbeust/32/1429_2.png) [@cedricbeust](https://discuss.kotlinlang.org/u/cedricbeust)\
**Post date:** [February 1, 2016, 6:16pm UTC](https://discuss.kotlinlang.org/t/puzzling-behavior-from-the-kotlin-gradle-plug-in/1441/6 "2016-02-01T18:16:49Z")

</div>

Follow up question: apparently `kotlinc` merely parses the Java files (probably just to parse the symbols so it can then compile the Kotlin files) but it doesn’t actually compile them, so it needs to run `javac` later.

That seems inefficient, why not actually compile them once and for all instead of doing the work twice?

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [February 1, 2016, 8:10pm UTC](https://discuss.kotlinlang.org/t/puzzling-behavior-from-the-kotlin-gradle-plug-in/1441/7 "2016-02-01T20:10:48Z")

</div>

Because those java files can contain references to kotlin classes, and javac can’t parse kotlin sources. Therefore kotlin sources have to be compiled before java ones.
