Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

groovy is unable to resolve kotlin classes

Tags:

kotlin

groovy

As I was not able to resolve the issue I have with kotlin, I decided to return to groovy in order to implement the plugin.

However I ran into the issue. I have such project structure.

enter image description here

In order not to rewrite all classes I decided to reuse classes implemented in kotlin.

However my classes FileProcessingCoreGroovy & DownloadCoreGroovy are unable to resolve class ProcessStream and UnpackStream.

I thought they should be able freely use each other but I guess I was wrong. What is the problem?

The classes itself are imported just fine by IDE (not marked in red color and so on)

enter image description here

The console output looks like this (I use another class where I import PluginUtils from kotlin classes)

:clean                                                                           
:compileKotlin (...multiple deprecated messages...)
:compileJava UP-TO-DATE       
:compileGroovy                 
startup failed:                
D:\groovy_apps\MISC\gradle-app-environment-plugin\src\main\groovy\com.lapots.gradle.plugins.appenv\ApplicationEnvironmentGroovyPlugin.g
roovy: 4: unable to resolve class com.lapots.gradle.plugins.appenv.core.PluginUtils
 @ line 4, column 1.
   import com.lapots.gradle.plugins.appenv.core.PluginUtils
   ^                           
like image 573
lapots Avatar asked Feb 13 '17 09:02

lapots


2 Answers

Looks like it is not so simple.

See https://discuss.gradle.org/t/kotlin-groovy-and-java-compilation/14903/2

Using suggestions from there, I added

compileGroovy {
    dependsOn tasks.getByPath('compileKotlin')
    classpath += files(compileKotlin.destinationDir)
}

And it worked.

like image 52
vuk Avatar answered Nov 23 '22 11:11

vuk


vuk's answer eventually solved the problem for me.

However, it did not work immediately. Some of my Groovy Spock tests depended on a test utility in Kotlin, and I spent a lot of time figuring out what is wrong. It turned out that I did not move my Kotlin file out of the src/test/groovy folder, thus it was not compiled at the right time.

Make sure that every file is in the correct subfolder according to the language.
For example:

src
+---- main
|   +---- kotlin
|       +---- ...
+---- test
    +---- groovy
    |   +---- ...
    +---- kotlin
        +---- ...

For tests, this Gradle code will compile Kotlin before Groovy:

compileTestGroovy {
    dependsOn tasks.getByPath('compileTestKotlin')
    classpath += files(compileTestKotlin.destinationDir)
}
like image 24
TheOperator Avatar answered Nov 23 '22 10:11

TheOperator