Gradle:无法初始化类org.codehaus.groovy.runtime.InvokerHelper。

21

我看到了这个Gradle错误:"Could not initialize class org.codehaus.groovy.runtime.InvokerHelper"。我尝试了网上列出的多种方法来解决问题,但是没有运气。

在调试期间,我发现了奇怪的JVM版本。

我已经安装了Java 14,并且在bash_profile中设置了JAVA_HOME路径。然而,Gradle显示JVM 13版本。我不确定这是否导致了错误:Could not initialize class org.codehaus.groovy.runtime.InvokerHelper。

我的Java版本

java version "14.0.1" 2020-04-14
Java(TM) SE Runtime Environment (build 14.0.1+7)
Java HotSpot(TM) 64-Bit Server VM (build 14.0.1+7, mixed mode, sharing)
< p >Gradle版本,但不确定为什么JVM显示13< /p > < p >< /p >
Gradle 6.5
------------------------------------------------------------

Build time:   2020-06-02 20:46:21 UTC
Revision:     a27f41e4ae5e8a41ab9b19f8dd6d86d7b384dad4

Kotlin:       1.3.72
Groovy:       2.5.11
Ant:          Apache Ant(TM) version 1.10.7 compiled on September 1 2019
JVM:          **13.0.2 (Oracle Corporation 13.0.2+8)**
OS:           Mac OS X 10.14.6 x86_64
Gradle错误。
npx react-native run-android
info Running jetifier to migrate libraries to AndroidX. You can disable it using "--no-jetifier" flag.
Jetifier found 960 file(s) to forward-jetify. Using 4 workers...
info JS server already running.
info Launching emulator...
info Successfully launched emulator.
info Installing the app...

FAILURE: Build failed with an exception.

* What went wrong:
Could not initialize class org.codehaus.groovy.runtime.InvokerHelper

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 3s

error Failed to install the app. Make sure you have the Android development environment set up: https://reactnative.dev/docs/environment-setup. Run CLI with --verbose flag for more details.
Error: Command failed: ./gradlew app:installDebug -PreactNativeDevServerPort=8081

FAILURE: Build failed with an exception.

* What went wrong:
Could not initialize class org.codehaus.groovy.runtime.InvokerHelper

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 3s

将“--debug”或“--stacktrace”添加到Gradle调用命令中,以获取更多详细信息。 - madhead
4个回答

71

更改gradle的版本

编辑文件YOUR_PROJECT/android/gradle/wrapper/gradle-wrapper.properties并更改版本号。

从:

distributionUrl=https://services.gradle.org/distributions/gradle-6.0.1-all.zip

到:

distributionUrl=https://services.gradle.org/distributions/gradle-6.4.1-all.zip

来源:https://github.com/gradle/gradle/issues/10248#issuecomment-633656326


1
我遇到了同样的问题,这个方法对我有用。谢谢,Pablo! - Rebecca Campbell
1
确实,您必须将其更改为系统中当前安装的版本。 - Ουιλιαμ Αρκευα
1
兄弟,你让我的一天都变得美好了。我一直在遇到一个错误,尝试了很多方法来解决,但都没有用。你的解决方案对我非常有用。你应该得到更多的感谢。 - Abdulkadir Ugas
1
这应该是“答案”。我也可以用。 - Juan Carlos Salinas Ojeda

6

Gradle对Java版本非常具体。我们需要检查兼容矩阵并将Java版本设置为gradle版本所需的版本。

https://docs.gradle.org/current/userguide/compatibility.html

与启动gradle守护程序相关的问题大多是由于JAVA引起的。我们不需要升级或降级gradle版本,也不需要更改设置文件。


3

你能否将官方文档的图像贴到回答中,而是将文本粘贴到回答中并相应地进行格式化呢?(在必要时使用 块引用代码格式化 - double-beep
这张图片只是为了展示文档中文字的位置,以便读者不浪费时间。 - Muhammad Ahmed Hassan
然后您可以添加文本和图像。请记住,图像无法搜索,在移动设备上阅读困难,需要更多的带宽,并且用户无法将文本复制并粘贴到他们的代码中(例如作为注释)。 - double-beep

0
上述错误与我开始使用React Native开发时遇到的非常相似。该错误是由于项目中不兼容的JAVA JDK和Gradle版本引起的。请前往./android/gradle/wrapper/gradle-properties.....并更改所需的Gradle版本。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接