Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify the JDK version in Android Studio?

Android Studio is giving me a Gradle build error that looks like this:

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. 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. 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. I also ran the java -version command in the terminal and it says my version is "1.7.0_79".

Any help would be appreciated!

like image 802
ThePartyTurtle Avatar asked Jun 03 '15 21:06

ThePartyTurtle


People also ask

Can I use JDK 11 for Android Studio?

Up to Android Studio 3.5 beta 2 one could use JDK11 for compiling project by configuring it in Project Structure -> SDK Location -> JDK Location. This way Android Studio is run under bundled JDK8 but Gradle is using selected JDK11. Starting from Android Studio 3.5 beta 3 this setup is no longer supported.

Can I use Java 8 in Android Studio?

Java 8 language features are now supported by the Android build system in the javac/dx compilation path. Android Studio's Gradle plugin now desugars Java 8 class files to Java 7-compatible class files, so you can use lambdas, method references and other features of Java 8.

Can I use Java 17 for Android Studio?

As per the docs, JDK 17 isn't supported yet in Android Studio.


2 Answers

You can use cmd + ; for Mac or Ctrl + Alt + Shift + S for Windows/Linux to pull up the Project Structure dialog. In there, you can set the JDK location as well as the Android SDK location.

Project Structure Dialog

To get your JDK location, run /usr/libexec/java_home -v 11 in terminal. Send 1.7 for Java 7, 1.8 for Java 8, or 11 for Java 11.

like image 68
Ben Kane Avatar answered Sep 20 '22 17:09

Ben Kane


Android Studio Arctic Fox (2020.3.1)

In Android Studio Arctic Fox (2020.3.1 Patch 4), the JDK Location setting is moved to:

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

Up to Android Studio 4.2

You can follow the answer from Ben Kane, the JDK Location setting is located in:

File > Project Structure > SDK Location 
like image 42
Matthieu Esnault Avatar answered Sep 23 '22 17:09

Matthieu Esnault