Gradle:无法从“11.0.2”确定 java 版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/54358107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:01:59  来源:igfitidea点击:

Gradle: Could not determine java version from '11.0.2'

javagradlegradlew

提问by lesley2958

I ran the following comment:

我运行了以下评论:

./gradlew app:installDebug

only to be met with the log:

只会遇到日志:

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine java version from '11.0.2'.

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

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

My version of gradle is 5.1.1:

我的gradle版本是5.1.1:

------------------------------------------------------------
Gradle 5.1.1
------------------------------------------------------------

Build time:   2019-01-10 23:05:02 UTC
Revision:     3c9abb645fb83932c44e8610642393ad62116807

Kotlin DSL:   1.1.1
Kotlin:       1.3.11
Groovy:       2.5.4
Ant:          Apache Ant(TM) version 1.9.13 compiled on July 10 2018
JVM:          11.0.2 (Oracle Corporation 11.0.2+9-LTS)
OS:           Mac OS X 10.13.6 x86_64

I'm not sure how to proceed (I tried upgrading/downgrading, but nothing has worked so far).

我不确定如何继续(我尝试升级/降级,但到目前为止没有任何效果)。

UPDATE: When I ran ./gradlew --version, I got the following:

更新:当我运行时./gradlew --version,我得到以下信息:

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine java version from '11.0.2'.

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

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

My gradle-wrapper.propertiescontains:

我的gradle-wrapper.properties包含:

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-rc-1-all.zip

采纳答案by tryman

There are two different Gradle applications in your system.

您的系统中有两个不同的 Gradle 应用程序。

  1. the system-wide Gradle
    This application is invoked by gradle (arguments).

  2. the gradle-wrapper
    The gradle-wrapper is specific to every project and can only be invoked inside the project's directory, using the command ./gradlew (arguments).

  1. 系统范围内的摇篮
    本申请通过援引gradle (arguments)

  2. gradle这个-包装
    的gradle产出,包装是具体到每一个项目,只能调用该项目的目录中,使用命令./gradlew (arguments)

Your system-wide gradle version is 5.1.1 (as the OP explained in the comments, running the command gradle --versionreturned version 5.1.1).

您的系统范围的 gradle 版本是 5.1.1(正如 OP 在评论中所解释的,运行命令gradle --version返回版本 5.1.1)。

However, the failure is the result of a call to the gradle-wrapper (./gradlew). Could you check your project's gradle wrapper version? To do that, execute ./gradlew --versioninside your project's folder, in the directory where the gradlew and gradlew.bat files are.

但是,失败是调用 gradle-wrapper ( ./gradlew) 的结果。你能检查一下你项目的 gradle 包装器版本吗?为此,请./gradlew --version在项目文件夹中的 gradlew 和 gradlew.bat 文件所在的目录中执行。

Update 1:
As running ./gradlew --versionfailed, you can manually check your wrapper's version by opening the file:

更新 1:
由于运行./gradlew --version失败,您可以通过打开文件手动检查包装器的版本:

(project's root folder)/gradle/wrapper/gradle-wrapper.properties

(项目的根文件夹)/gradle/wrapper/gradle-wrapper.properties

with a simple text editor. The "distributionUrl" inside should tell us what the wrapper's version is.

使用简单的文本编辑器。里面的“distributionUrl”应该告诉我们包装器的版本是什么。

Update 2:As per the OP's updated question, the gradle-wrapper's version is 4.1RC1.
Gradle added support for JDK 11 in Gradle 5.0. Hence since 4.1RC does not support JDK 11 this is definitely a problem.

更新 2:根据 OP 的更新问题,gradle-wrapper 的版本是 4.1RC1。
Gradle在 Gradle 5.0 中添加了对 JDK 11 的支持。因此,由于 4.1RC 不支持 JDK 11,这绝对是一个问题。

The obvious way, would be to update your project's gradle-wrapper to version 5.0. However, before updating, try running gradle app:installDebug. This will use your system-wide installed Gradle whose version is 5.1.1 and supports Java 11. See if this works. If it does, then your buildscript (file build.gradle) is not affected by any breaking changes between v.4.1RC1 and v.5.1.1 and you can then update your wrapper by executing from the command line inside your project's folder: gradle wrapper --gradle-version=5.1.1[*].

显而易见的方法是将项目的 gradle-wrapper 更新到版本 5.0。但是,在更新之前,请尝试运行gradle app:installDebug. 这将使用系统范围内安装的 Gradle,其版本为 5.1.1 并支持 Java 11。看看这是否有效。如果是这样,那么您的构建脚本(文件 build.gradle)不受 v.4.1RC1 和 v.5.1.1 之间的任何重大更改的影响,然后您可以通过从项目文件夹内的命令行执行来更新您的包装器:gradle wrapper --gradle-version=5.1.1[ *]。

If gradle app:installDebugfails to execute correctly, then maybe you need to upgrade your Gradle buildscript. For updating from v.4.1RC1 to 5.1.1, the Gradle project provides a guide (1, 2) with breaking changes and deprecated features between minor releases, so that you can update gradually to the latest version.

如果gradle app:installDebug无法正确执行,那么您可能需要升级您的 Gradle 构建脚本。为了从 v.4.1RC1 更新到 5.1.1,Gradle 项目提供了一个指南 ( 1, 2),其中包含次要版本之间的重大更改和不推荐使用的功能,以便您可以逐步更新到最新版本。

Alternatively, if for some reason you can't or don't want to upgrade your Gradle buildscript, you can always choose to downgrade your Java version to one that Gradle 4.1RC1 supports.

或者,如果由于某种原因您不能或不想升级您的 Gradle 构建脚本,您始终可以选择将您的 Java 版本降级到 Gradle 4.1RC1 支持的版本。

[*] As correctly pointed out in another answer by @lupchiazoem, use gradle wrapper --gradle-version=5.1.1(and not ./gradlewas I had originally posted there by mistake). The reason is Gradle runs on Java. You can update your gradle-wrapper using any working Gradle distribution, either your system-wide installed Gradle or the gradle-wrapper itself. However, in this case your wrapper is not compatible with your installed Java version, so you do have to use the system-wide Gradle (aka gradleand not ./gradlew).

[*] 正如@lupchiazoem 在另一个答案中正确指出的那样,使用gradle wrapper --gradle-version=5.1.1(而不是./gradlew我最初错误地发布在那里)。原因是 Gradle 在 Java 上运行。您可以使用任何可用的 Gradle 发行版更新您的 gradle-wrapper,无论是系统范围内安装的 Gradle 还是 gradle-wrapper 本身。但是,在这种情况下,您的包装器与您安装的 Java 版本不兼容,因此您必须使用系统范围的 Gradle(又名gradle而不是./gradlew)。

回答by lupchiazoem

As distributionUrlis still pointing to older version, upgrade wrapper using:

由于distributionUrl仍然指向旧版本,请使用以下方法升级包装器:

gradle wrapper --gradle-version 5.1.1

gradle wrapper --gradle-version 5.1.1

Note: Use gradleand not gradlew

注意:使用gradle和不使用gradlew

回答by dr0i

In my case the JAVA_HOMEvariable was set to /usr/lib/jvm/jdk-11.0.2/. It was sufficient to unset the variable like this:

在我的情况下,JAVA_HOME变量设置为/usr/lib/jvm/jdk-11.0.2/. 像这样取消设置变量就足够了:

$ export JAVA_HOME=

$ export JAVA_HOME=

回答by juancho85

I've had the same issue. Upgrading to gradle 5.0 did the trick for me.

我有同样的问题。升级到 gradle 5.0 对我有用。

This link provides detailed steps on how install gradle 5.0: https://linuxize.com/post/how-to-install-gradle-on-ubuntu-18-04/

此链接提供了有关如何安装 gradle 5.0 的详细步骤:https://linuxize.com/post/how-to-install-gradle-on-ubuntu-18-04/

回答by KHAN

I ran into a similar issue. I deleted these:

我遇到了类似的问题。我删除了这些:

  • libraries and caches from the .idea folder ( YourApp > .idea > .. ) AND
  • contents of the build folder.

    then rebuild.

  • .idea 文件夹中的库和缓存( YourApp > .idea > .. )和
  • 构建文件夹的内容。

    然后重建。

* DON'T FORGET TO BACKUP YOUR PROJECT FIRST *

* 不要忘记首先备份您的项目 *

回答by SuperAndrew

Because wrapper version does not support 11+ you can make simple trick to cheat newer version of InteliJ forever.

由于包装器版本不支持 11+,您可以使用简单的技巧来永远欺骗更新版本的 InteliJ。

  press3x Shift -> type "Switch Boot JDK" -> and change for java 8. 

https://blog.jetbrains.com/idea/2015/05/intellij-idea-14-1-4-eap-141-1192-is-available/

https://blog.jetbrains.com/idea/2015/05/intellij-idea-14-1-4-eap-141-1192-is-available/

Or If you want to work with java 11+ you simply have to update wrapper version to 4.8+

或者,如果您想使用 java 11+,您只需将包装器版本更新为 4.8+

回答by MgFrobozz

I ran into the same issue in Ubuntu 18.04.3 LTS. In my case, apt installed gradle version 4.4.1. The already-install java version was 11.0.4

我在 Ubuntu 18.04.3 LTS 中遇到了同样的问题。就我而言,apt 安装了 gradle 版本 4.4.1。已经安装的 java 版本是 11.0.4

The build message I got was

我得到的构建消息是

Could not determine java version from '11.0.4'.

At the time, most of the online docs referenced gradle version 5.6, so I did the following:

当时网上的大部分文档都引用了gradle 5.6版,所以我做了如下操作:

sudo add-apt-repository ppa:cwchien/gradle
sudo apt update
sudo apt upgrade gradle

Then I repeated the project initialiation (using "gradle init" with the defaults). After that, "./gradlew build" worked correctly.

然后我重复了项目初始化(使用带有默认值的“gradle init”)。之后,“./gradlew build”正常工作。

I later read a comment regarding a change in format of the output from "java --version" that caused gradle to break, which was fixed in a later version of gradle.

后来我读到了一条关于“java --version”输出格式更改的评论,该更改导致 gradle 中断,该问题已在更高版本的 gradle 中修复。

回答by Rocky Inde

To put long answer short, upgrade your gradlewusing the system gradletool. Note that the below upgrade works even if your system gradleversion is < 5.

简而言之,请gradlew使用系统gradle工具升级您的答案。请注意,即使您的系统gradle版本是< 5.

gradle wrapper --gradle-version=5.1.1

回答by Onat Korucu

In my case, I was trying to build and get APK for an old Unity 3D project (so that I can play the game in my Android phone). I was using the most recent Android Studio version, and all the SDK packages I could download via SDK Manager in Android Studio. SDK Packages was located in

就我而言,我试图为旧的 Unity 3D 项目构建和获取 APK(以便我可以在我的 Android 手机上玩游戏)。我使用的是最新的 Android Studio 版本,以及我可以通过 Android Studio 中的 SDK Manager 下载的所有 SDK 包。SDK 包位于

C:/Users/Onat/AppData/Local/Android/Sdk 

And the error message I got was the same except the JDK (Java Development Kit) version "jdk-12.0.2" . JDK was located in

除了 JDK(Java 开发工具包)版本 "jdk-12.0.2" 之外,我得到的错误消息是相同的。JDK位于

C:\Program Files\Java\jdk-12.0.2

And Environment Variable in Windows was JAVA_HOME : C:\Program Files\Java\jdk-12.0.2

Windows 中的环境变量是 JAVA_HOME : C:\Program Files\Java\jdk-12.0.2

After 3 hours of research, I found out that Unity does not support JDK 10. As told in https://forum.unity.com/threads/gradle-build-failed-error-could-not-determine-java-version-from-10-0-1.532169/. My suggestion is:

经过 3 个小时的研究,我发现 Unity 不支持 JDK 10。如https://forum.unity.com/threads/gradle-build-failed-error-could-not-determine-java-version-from-10-0-1.532169/. 我的建议是:

  1. Uninstall unwanted JDK if you have one installed already. https://www.java.com/tr/download/help/uninstall_java.xml
  2. Head to http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
  3. Login to/Open a Oracle account if not already logged in.
  4. Download the older but functional JDK 8 for your computer set-up(32 bit/64 bit, Windows/Linux etc.)
  5. Install the JDK. Remember the installation path. (https://docs.oracle.com/cd/E19182-01/820-7851/inst_cli_jdk_javahome_t/)
  6. If you are using Windows, Open Environment Variables and change Java Path via Right click My Computer/This PC>Properties>Advanced System Settings>Environment Variables>New>Variable Name: JAVA_HOME>Variable Value: [YOUR JDK Path, Mine was "C:\Program Files\Java\jdk1.8.0_221"]
  7. In Unity 3D, press Edit > Preferences > External Tools and fill in the JDK path (Mine was "C:\Program Files\Java\jdk1.8.0_221").
  8. Also, in the same pop-up, edit SDK Path. (Get it from Android Studio > SDK Manager > Android SDK > Android SDK Location.)
  9. If needed, restart your computer for changes to take effect.
  1. 如果您已经安装了不需要的 JDK,请卸载它。https://www.java.com/tr/download/help/uninstall_java.xml
  2. 前往 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
  3. 如果尚未登录,请登录/打开 Oracle 帐户。
  4. 为您的计算机设置下载较旧但功能强大的 JDK 8(32 位/64 位、Windows/Linux 等)
  5. 安装 JDK。记住安装路径。( https://docs.oracle.com/cd/E19182-01/820-7851/inst_cli_jdk_javahome_t/)
  6. 如果您使用的是 Windows,请打开环境变量并通过右键单击我的更改 Java 路径 Computer/This PC>Properties>Advanced System Settings>Environment Variables>New>Variable Name: JAVA_HOME>Variable Value: [YOUR JDK Path, Mine was "C:\Program Files\Java\jdk1.8.0_221"]
  7. 在 Unity 3D 中,按Edit > Preferences > External Tools and fill in the JDK path (Mine was "C:\Program Files\Java\jdk1.8.0_221")
  8. 此外,在同一个弹出窗口中,编辑 SDK 路径。(从 中获取Android Studio > SDK Manager > Android SDK > Android SDK Location。)
  9. 如果需要,请重新启动计算机以使更改生效。

回答by Nestor Milyaev

I had a similar problem: my default gradle wrapperwas version 4.x, while the support for higher versions of Java has been added in Gradle 5.

我遇到了类似的问题:我的默认gradle wrapper版本是 4.x,而 Gradle 5 中已经添加了对更高版本 Java 的支持。

I've updated my gradlewas described here: https://docs.gradle.org/current/userguide/gradle_wrapper.html#sec:upgrading_wrapper

我已经gradlew按照这里的描述更新了我的:https: //docs.gradle.org/current/userguide/gradle_wrapper.html#sec: upgrading_wrapper

TLTD:

TLTD:

./gradlew wrapper --gradle-version 5.6.2