简体   繁体   English

如何在Android Studio中指定JDK版本?

[英]How to specify the JDK version in Android Studio?

Android Studio is giving me a Gradle build error that looks like this: Android Studio 给了我一个 Gradle 构建错误,如下所示:

Error:(3, 22) compileSdkVersion android-22 requires compiling with JDK 7

Now it gives me these clickable prompts:现在它给了我这些可点击的提示:

Download JDK 7
Select a JDK from the File System
Open build.gradle File

And I have already downloaded and installed JDK 7. The problem is when I go to select it in the "File System" i can only find a directory named 1.6.0 JDK.而且我已经下载并安装了JDK 7。问题是当我在“文件系统”中选择它时,我只能找到一个名为1.6.0 JDK的目录。 Furthermore, the installation of JDK 7 skipped the bullet point where I would've selected the install directory, so I'm really not sure where it is.此外,JDK 7 的安装跳过了我选择安装目录的要点,所以我真的不确定它在哪里。 My java control panel says I have "Java 7 Update 79" so I'm pretty sure I'm close, I just need to tell android studio where it is.我的 java 控制面板说我有“Java 7 Update 79”,所以我很确定我已经接近了,我只需要告诉 android studio 它在哪里。 I also ran the java -version command in the terminal and it says my version is "1.7.0_79".我还在终端中运行了java -version命令,它说我的版本是“1.7.0_79”。

Any help would be appreciated!任何帮助,将不胜感激!

You can use cmd + ;您可以使用cmd + ; for Mac or Ctrl + Alt + Shift + S for Windows/Linux to pull up the Project Structure dialog.对于 Mac 或Ctrl + Alt + Shift + S对于 Windows/Linux 以拉出项目结构对话框。 In there, you can set the JDK location as well as the Android SDK location.在那里,您可以设置 JDK 位置以及 Android SDK 位置。

项目结构对话框

To get your JDK location, run /usr/libexec/java_home -v 11 in terminal.要获取 JDK 位置,请在终端中运行/usr/libexec/java_home -v 11 Send 1.7 for Java 7, 1.8 for Java 8, or 11 for Java 11.为 Java 7 发送 1.7,为 Java 8 发送 1.8,或为 Java 11 发送 11。

对于新的 Android Studio 版本,请转到 C:\\Program Files\\Android\\Android Studio\\jre\\bin(或 Android Studio 安装文件的位置)并在此位置打开命令窗口并在命令提示符下键入以下命令:-

java -version

Android Studio Arctic Fox (2020.3.1) Android Studio 北极狐(2020.3.1)

In the Canary 14 of Android Studio Arctic Fox (2020.3.1), the JDK Location setting is moved to:在Android Studio Arctic Fox (2020.3.1) 的Canary 14 中,JDK Location 设置移动到:

Settings > Build, Execution, Deployment > Build Tools > Gradle > Gradle JDK

Up to Android Studio 4.2高达 Android Studio 4.2

You can follow the answer from Ben Kane, the JDK Location setting is located in:您可以按照 Ben Kane 的回答进行操作,JDK 位置设置位于:

File > Project Structure > SDK Location

This is old question but still my answer may help someone这是一个老问题,但我的回答仍然可以帮助某人

For checking Java version in android studio version , simply open Terminal of Android Studio and type要在 android studio 版本中检查 Java 版本,只需打开 Android Studio 的终端并输入

java -version 

This will display java version installed in android studio这将显示在 android studio 中安装的 java 版本

In Android Studio 4.0.1, Help -> About shows the details of the Java version used by the studio, in my case:在 Android Studio 4.0.1 中,Help -> About 显示了工作室使用的 Java 版本的详细信息,在我的例子中:

Android Studio 4.0.1
Build #AI-193.6911.18.40.6626763, built on June 25, 2020
Runtime version: 1.8.0_242-release-1644-b01 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 10 10.0
GC: ParNew, ConcurrentMarkSweep
Memory: 1237M
Cores: 8
Registry: ide.new.welcome.screen.force=true
Non-Bundled Plugins: com.google.services.firebase

在 Mac 上,您可以使用终端转到/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home (或安装 Android SDK 的任何位置)并在命令提示符中输入以下内容:

./java -version

检查您在 Android Studio 中使用的 JDK

The gradle sync uses the following JDK location: gradle sync使用以下 JDK 位置:

In Android Studio, File -> Project Structure在 Android Studio 中, File -> Project Structure

Navigate to the SDK Location .导航到SDK Location

Android Studio 4.1.0 JDK 位置

cd /Applications/Android\ Studio\ 4.1.0.app/Contents/jre/jdk/Contents/Home

➜  Home ./bin/java --version
openjdk 11.0.8 2020-07-14
OpenJDK Runtime Environment (build 11.0.8+10-b944.6916264)
OpenJDK 64-Bit Server VM (build 11.0.8+10-b944.6916264, mixed mode)
  1. gradle.properties:org.gradle.java.home=/Applications/Android\\ Studio.app/Contents/jre/Contents/Home gradle.properties:org.gradle.java.home=/Applications/Android\\ Studio.app/Contents/jre/Contents/Home
  2. ./gradlew assembleDebug -Dorg.gradle.java.home=/Applications/Android\\ Studio.app/Contents/jre/Contents/Home ./gradlew assembleDebug -Dorg.gradle.java.home=/Applications/Android\\ Studio.app/Contents/jre/Contents/Home

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM