Building Kotlin projects with Gradle sometimes fails with NoClassDefFoundError

*爱你&永不变心* 提交于 2020-06-27 12:24:29

问题


I can't reliably reproduce this problem because it is sporadic. This is what I get when I build a Kotlin project with Gradle:

Could not perform incremental compilation: Could not connect to Kotlin compile daemon
Could not connect to kotlin daemon. Using fallback strategy.
:myproj:compileKotlin FAILED
1 actionable task: 1 executed
e: java.lang.NoClassDefFoundError: Could not initialize class kotlin.Unit
warning: the '-d' option with a directory destination is ignored because '-Xbuild-file' is specified
Exception in thread "Thread-25" java.lang.NoClassDefFoundError: Could not initialize class kotlin.Unit
    at org.jetbrains.kotlin.compilerRunner.UtilsKt$runToolInSeparateProcess$readErrThread$1$1.invoke(utils.kt)
    at kotlin.io.TextStreamsKt.forEachLine(ReadWrite.kt:154)
    at org.jetbrains.kotlin.compilerRunner.UtilsKt$runToolInSeparateProcess$readErrThread$1.invoke(utils.kt:68)
    at org.jetbrains.kotlin.compilerRunner.UtilsKt$runToolInSeparateProcess$readErrThread$1.invoke(utils.kt)
    at kotlin.concurrent.ThreadsKt$thread$thread$1.run(Thread.kt:18)
e: java.lang.NoClassDefFoundError: Could not initialize class kotlin.Unit
...

I run Gradle with the following options

  • gradle assemble --no-daemon --parallel
  • I tried this within Docker and on my local machine as well (this even happens on the CI server as well)
  • this error is completely sporadic I was not able to pinpoint a cause
  • it happens with single and multi-module projects as well
  • removing the --parallel or adding --daemon option does not help
  • I'm using Gradle 4.2

What could be the problem?


回答1:


This is a known issue with Kotlin. Unfortunately at this time we don't know what exactly causes it.




回答2:


The cause of this problem is related to use of two or more different Kotlin version. In my case, I use Android Studio 3 and IntelliJ IdeaU 2017. The solution that I used and it worked is:

  • check your Kotlin version in either IDE. In this case, you should update the Kotlin's compiler to the same version.

  • If you use just one IDE, the best possible solution is to remove your IDE and all caches.

For more information, please refer to Kotlin community.



来源:https://stackoverflow.com/questions/46648318/building-kotlin-projects-with-gradle-sometimes-fails-with-noclassdeffounderror

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!