使用Gradle构建Kotlin项目有时会因NoClassDefFoundError而失败 [英] Building Kotlin projects with Gradle sometimes fails with NoClassDefFoundError

查看:483
本文介绍了使用Gradle构建Kotlin项目有时会因NoClassDefFoundError而失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法可靠地重现此问题,因为它是零星的.这是我使用 Gradle 构建一个 Kotlin 项目时得到的:

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
...

我通过以下选项运行Gradle

I run Gradle with the following options

  • gradle assemble --no-daemon --parallel
  • 我在Docker以及我的本地计算机上都尝试过(甚至在CI服务器上也是如此)
  • 此错误是完全零星的,我无法查明原因
  • 在单模块和多模块项目中也会发生
  • 删除--parallel或添加--daemon选项无济于事
  • 我正在使用Gradle 4.2
  • 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

可能是什么问题?

推荐答案

此问题的原因与使用两个或多个不同的Kotlin版本有关.就我而言,我使用的是Android Studio 3和IntelliJ IdeaU2017.我使用且有效的解决方案是:

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:

  • 在任一IDE中检查您的Kotlin版本.在这种情况下,您应该将Kotlin的编译器更新为相同的版本.

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

如果仅使用一个IDE,最好的解决方案是删除IDE和所有缓存.

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.

这篇关于使用Gradle构建Kotlin项目有时会因NoClassDefFoundError而失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆