Java 'android-24' 需要 JDK 1.8 或更高版本才能编译

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37933651/
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 19:49:02  来源:igfitidea点击:

'android-24' requires JDK 1.8 or later to compile

javaandroidandroid-studio

提问by T D Nguyen

I use Android Studio and recently got the error:

我使用 Android Studio,最近出现错误:

Error:Execution failed for task ':app:compileDebugJavaWithJavac'. compileSdkVersion 'android-24' requires JDK 1.8 or later to compile.

错误:任务 ':app:compileDebugJavaWithJavac' 的执行失败。compileSdkVersion 'android-24' 需要 JDK 1.8 或更高版本才能编译。

But I have JDK 1.8 installed already:

但是我已经安装了 JDK 1.8:

:\Users..>java -version
java version "1.8.0_91" Java(TM) SE Runtime
Environment (build 1.8.0_91-b15) Java HotSpot(TM) 64-Bit Server VM
(build 25.91-b15, mixed mode)

:\Users..>java -version
java version "1.8.0_91" Java(TM) SE Runtime
Environment (build 1.8.0_91-b15) Java HotSpot(TM) 64-Bit Server VM
(build 25.91-b15,混合模式)

How to fix it? Thanks

如何解决?谢谢

采纳答案by Juan Cruz Soler

In Android Studio open:
File> Project Structure> see if JDK location points to your JDK 1.8 directory.

在 Android Studio 中打开:
File> Project Structure> 查看 JDK 位置是否指向您的 JDK 1.8 目录。

Note:you can use compileSdkVersion 24

注意:您可以使用compileSdkVersion 24

回答by Bradford2000

I was also running into the same issue from the command line on my Mac, but the answer was that JAVA_HOMEwas getting overridden. To track down where it is getting overridden first check java from the command line:

我也在 Mac 上的命令行中遇到了同样的问题,但答案是它JAVA_HOME被覆盖了。要跟踪它被覆盖的位置,请首先从命令行检查 java:

$ java -version
java version "1.8.0_92"
Java(TM) SE Runtime Environment (build 1.8.0_92-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.92-b14, mixed mode)

Then check which version of Java that Gradle is using:

然后检查 Gradle 使用的 Java 版本:

$ gradle -version

------------------------------------------------------------
Gradle 2.13
------------------------------------------------------------

Build time:   2016-04-25 04:10:10 UTC
Build number: none
Revision:     3b427b1481e46232107303c90be7b05079b05b1c

Groovy:       2.4.4
Ant:          Apache Ant(TM) version 1.9.6 compiled on June 29 2015
JVM:          1.8.0_92 (Oracle Corporation 25.92-b14)
OS:           Mac OS X 10.11.5 x86_64

At this point, I still hadn't found the source of the problem. Did a little digging, and found that it was indeed overridden in my gradle.propertiesfile even though it was showing Java 8 when I executed gradle -version:

此时,我还没有找到问题的根源。做了一点挖掘,发现它确实在我的gradle.properties文件中被覆盖了,即使它在我执行时显示的是 Java 8 gradle -version

org.gradle.daemon=true
org.gradle.java.home=/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home
org.gradle.jvmargs=-XX:MaxPermSize=512m -XX:-UseSplitVerifier -Xms512m -Xmx6144m

To fix, I just deleted the java.homeline from gradle.properties. Hopefully this helps if anyone else is running into the same issue from the command line.

为了解决这个问题,我刚刚java.homegradle.properties. 如果其他人从命令行遇到相同的问题,希望这会有所帮助。

回答by Szelk Baltazár

I have a same problem for days. I found a solution which worked from me on Windows. (I also set environment variables, but don't know whether it's needed.)

我有同样的问题好几天了。我找到了一个在 Windows 上对我有用的解决方案。(我也设置了环境变量,不知道有没有必要。)

Add these lines to build.gradle file:

将这些行添加到 build.gradle 文件:

android {
     ...
     defaultConfig {
          ...
          HymanOptions {
              enabled true
          }
     }
     compileOptions {
         targetCompatibility 1.8
         sourceCompatibility 1.8
     }
}

Source: https://code.google.com/p/android/issues/detail?id=203850

来源:https: //code.google.com/p/android/issues/detail?id=203850

回答by Pawan

I installed latest jdk but it was using the older one until I did this:

我安装了最新的 jdk,但在我这样做之前它一直在使用旧的 jdk:

app rt-click >> module settings >> sdk location >> (old jdk path)

Replace the old JDK path with your new jdk path.

用新的 jdk 路径替换旧的 JDK 路径。

Works like a charm now :)

现在像魅力一样工作:)

回答by Mihir Vaghela

In Android Studio open: File > Project Structure > SDK Location > Select jdk1.8.0_101.jdk > Home

在 Android Studio 打开:文件 > 项目结构 > SDK 位置 > 选择 jdk1.8.0_101.jdk > Home

回答by Francis Bacon

I was also running into the same issue from the command line on my Windows, it shows the same hint.

我也在 Windows 的命令行中遇到了同样的问题,它显示了相同的提示。

Error:Execution failed for task ':app:compileDebugJavaWithJavac'. `compileSdkVersion 'android-24' requires JDK 1.8 or later to compile.`

check JDK locationin Project Structure.

检查JDK locationProject Structure

In Android Studio open: File> Project Structure> see if JDK location points to your JDK 1.8 directory.

在 Android Studio 中打开:File> Project Structure> 查看 JDK 位置是否指向您的 JDK 1.8 目录。

It should look like

它应该看起来像

JDK Location
C:\Program Files\Java\jdk1.8.0_91

I already set the correct location to JDK.

我已经为 JDK 设置了正确的位置。

check the java version

检查java版本

D:\AndroidSelfTrainingProject\CustomBuildIdDemo>java -version
java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)

check the gradle version

检查gradle版本

.D:\AndroidSelfTrainingProject\CustomBuildIdDemo>gradle -v

------------------------------------------------------------
Gradle 2.10
------------------------------------------------------------

Build time:   2015-12-21 21:15:04 UTC
Build number: none
Revision:     276bdcded730f53aa8c11b479986aafa58e124a6

Groovy:       2.4.4
Ant:          Apache Ant(TM) version 1.9.3 compiled on December 23 2013
JVM:          1.7.0_79 (Oracle Corporation 24.79-b02)
OS:           Windows 7 6.1 amd64

JVM version should be 1.8. To fix it, we can add in the gradle.property In gradle.propertiesin the .gradledirectory in your HOME_DIRECTORYset org.gradle.java.home=/path_to_jdk_directory. As for me, it is like:

JVM 版本应该是 1.8。为了解决这个问题,我们可以在gradle.property加入gradle.properties.gradle目录中的HOME_DIRECTORY一组org.gradle.java.home=/path_to_jdk_directory。就我而言,它是这样的:

org.gradle.java.home=C:/Program Files/Java/jdk1.8.0_91

To verify if it is set right, we can make a gradle buildtask.

为了验证它是否设置正确,我们可以创建一个gradle build任务。

D:\AndroidSelfTrainingProject\CustomBuildIdDemo>gradle build
To honour the JVM settings for this build a new JVM will be forked. Please consider using the daemon
: https://docs.gradle.org/2.10/userguide/gradle_daemon.html.

To run dex in process, the Gradle daemon needs a larger heap.
It currently has approximately 910 MB.
For faster builds, increase the maximum heap size for the Gradle daemon to more than 2048 MB.
To do this set org.gradle.jvmargs=-Xmx2048M in the project gradle.properties.
For more information see https://docs.gradle.org/current/userguide/build_environment.html

回答by Stevo

Just an FYI I had this error with an Ionic project. All the commands came back pointing to the correct JDK and JAVA_HOME was set correctly.

仅供参考,我在 Ionic 项目中遇到了这个错误。所有命令都返回指向正确的 JDK 并且 JAVA_HOME 设置正确。

There was an entry in my PATH to JDK 1.7\bin, replacing that with the 1.8\bin fixed it for me.

在我的 JDK 1.7\bin 路径中有一个条目,用 1.8\bin 替换它为我修复了它。

回答by scvblwxq

On Windows 10, I first checked Project Structure and it pointed to an old 1.7 jdk. I closed Android Studio then renamed the old jdks (and jre libraries for consistency) leaving the newest 1.8 library alone. When I restarted Android Studio and attempted to build the application it asked me for the Java JDK library and I gave the path to the newest 1.8 JDK library. On my system it was C:\Program Files\Java\jdk1.8.0_112.

在 Windows 10 上,我首先检查了项目结构,它指向一个旧的 1.7 jdk。我关闭了 Android Studio,然后重命名了旧的 jdks(为了一致性而重命名了 jre 库),只留下最新的 1.8 库。当我重新启动 Android Studio 并尝试构建应用程序时,它要求我提供 Java JDK 库,我提供了最新 1.8 JDK 库的路径。在我的系统上它是 C:\Program Files\Java\jdk1.8.0_112。