无法找到或加载主类 org.gradle.wrapper.GradleWrapperMain [英] Could not find or load main class org.gradle.wrapper.GradleWrapperMain

查看:60
本文介绍了无法找到或加载主类 org.gradle.wrapper.GradleWrapperMain的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过删除诸如 ~/.gradle~/.m2~./android 等本地目录来清理整个项目>~/workspace/project/.gradle 并选择 File ->在 Android Studio 中使缓存无效/重新启动....现在执行命令 ./gradlew 会导致以下输出:

I cleaned the whole project by deleting local directories like ~/.gradle, ~/.m2 ~./android and ~/workspace/project/.gradle and chosing File -> Invalidate Caches / Restart... in Android Studio. Now execution of the command ./gradlew leads to the following output:

usr$ ./gradlew tasks
Error: Could not find or load main class org.gradle.wrapper.GradleWrapperMain

不用说,我删除的太多了,问题是如何再次修复?你有什么想法如何解决这个问题吗?

Needless to say, I deleted too much, the question is how can it be repaired again? Do you have any ideas how to fix this?

推荐答案

您的 gradle 包装器丢失、损坏或损坏.

Your gradle wrapper is missing, broken or corrupted.

什么是 gradle 包装器:
gradlew 是 gradle 包装器可执行文件 - Windows 上的批处理脚本和其他地方的 shell 脚本.调用包装器脚本时,下载定义的 gradle 版本并执行它.通过将包装器与您的项目一起分发,任何人都可以使用它而无需事先安装 Gradle.更好的是,该构建的用户可以保证使用该构建旨在使用的 Gradle 版本.

What is gradle wrapper:
gradlew is the gradle wrapper executable - batch script on windows and shell script elsewhere. The wrapper script when invoked, downloads the defined gradle version and executes it. By distributing the wrapper with your project, anyone can work with it without needing to install Gradle beforehand. Even better, users of the build are guaranteed to use the version of Gradle that the build was designed to work with.

恢复 gradle 包装器:
过去,您需要向 build.gradle 添加一个 wrapper 任务以恢复 gradle 包装器及其所有依赖项.例如:

Restoring gradle wrapper:
It used to be that you needed to add a wrapper task to your build.gradle to restore gradle wrapper and all its dependencies. For instance:

task wrapper(type: Wrapper) {
    gradleVersion = '4.1'
}

较新版本的 gradle 不需要这个.它现在是一个内置任务.只需运行:

Newer versions of gradle do not require this. It is now a built-in task. Just run:

gradle wrapper

您还可以提供额外的标志来指定版本等

You can also supply additional flags to specify versions etc

gradle wrapper --gradle-version 6.2 --distribution-type all

当您运行此任务时,gradle 包装器脚本和所需的 jar 文件将添加到您的源文件夹中.属性存储在 gradle/wrapper/gradle-wrapper.properties

When you run this task, a gradle wrapper script, and the required jar files are added to your source folders. Properties are stored in gradle/wrapper/gradle-wrapper.properties

(您可能需要在本地安装 gradle 才能运行它.brew install gradle 在 mac 上例如.查看更详细的说明 此处)

(You may need to install gradle locally to run this. brew install gradle on mac for instance. See more detailed instructions here)

为什么它首先丢失了?
OP 似乎删除了 gradle 包装器所依赖的东西.

Why was it missing in the first place?
OP seems to have deleted something that gradle wrapper depends on.

但一个常见的原因是 .gitignore 条目会阻止包装器 jar 被检入 git.请注意,有效的 .gitignore 可能位于源文件夹中,也可能位于用户主文件夹或 git 全局配置中的全局文件夹中.在 .gitignore 中有一个 *.jar 条目是很常见的.

But a common reason is that a .gitignore entry prevents wrapper jars from being checked into git. Note that the .gitignore in effect may be in the source folder, or a global one in your user home folder or git global configuration. It is common to have a *.jar entry in .gitignore.

您可以在.gitignore 中为gradlew 的jar 文件添加例外

You can add an exception for gradlew's jar files in .gitignore

*.jar
!gradle/wrapper/gradle-wrapper.jar

或强制将包装器 jar 添加到 git 中

or force add the wrapper jar into git

git add -f gradle/wrapper/gradle-wrapper.jar

参考:Gradle Wrapper

这篇关于无法找到或加载主类 org.gradle.wrapper.GradleWrapperMain的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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