136

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!

auspicious99
  • 3,902
  • 1
  • 44
  • 58
ThePartyTurtle
  • 2,276
  • 2
  • 18
  • 32

10 Answers10

228

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.

Ben Kane
  • 9,331
  • 6
  • 36
  • 58
  • Right but I don't know where the JDK is installed. JDK seven did not install in the same directory as JDK 6. Is there a way to find where JDK 7 installed? Because according to terminal and the java control panel, I have Java version 1.7.0_79, I just don't know where it is, because it isn't next to JDK 6. – ThePartyTurtle Jun 03 '15 at 21:56
  • 1
    Ah. Sorry about that. That's not what you asked :) I'll see if I can find info on the install location. I don't remember offhand where 7 gets installed on Windows – Ben Kane Jun 03 '15 at 22:03
  • 2
    @ThePartyTurtle Are you on Windows, Mac, or Linux. Well, either way, [use this answer](http://stackoverflow.com/a/4681109/2448305) to get the location. – Ben Kane Jun 03 '15 at 22:05
  • Should've specified! I'm on a Mac. And yea it's interesting it didn't install it into the same directory. Also if my terminal and Java control panel are telling me I have 1.7.0_79, does that necessarily mean I have JDK 7? – ThePartyTurtle Jun 03 '15 at 22:42
  • I would say yea that means you have it. What do you get when you run which java in terminal? – Ben Kane Jun 03 '15 at 22:46
  • When I run java -version, I get this: java version "1.7.0_79" Java(TM) SE Runtime Environment (build 1.7.0_79-b15) Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode) – ThePartyTurtle Jun 05 '15 at 16:29
  • 2
    I've added the command to the bottom of my answer that will get you your JDK location. – Ben Kane Jun 05 '15 at 16:38
72

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
Bjorn Reppen
  • 22,007
  • 9
  • 64
  • 88
Matthieu Esnault
  • 1,057
  • 9
  • 12
  • 1
    How do I change this globally for **all** projects created now and in the future in Arctic Fox? – Kenny83 Aug 15 '21 at 12:39
  • 8
    I'm not seeing Gradle JDK in Arctic Fox. – Axes Grinds Aug 20 '21 at 05:46
  • 1
    The top example is only the JDK for gradle, what about the JDK for the rest of the system used for building and compiling code? – Andrew S Jan 27 '22 at 04:33
  • 1
    @AndrewS It seems like that this jdk version is used for everything in the progect – userVadim Jan 18 '23 at 12:44
  • @userVadim, From my experience following this question, I am sure you are correct. I really with it was labeled and located as so. It causes unnecessary confusion. – Andrew S Jan 23 '23 at 15:24
29

For new Android Studio versions, go to C:\Program Files\Android\Android Studio\jre\bin(or to location of Android Studio installed files) and open command window at this location and type in following command in command prompt:-

java -version
auspicious99
  • 3,902
  • 1
  • 44
  • 58
Rishabh
  • 1,827
  • 1
  • 12
  • 9
8

Check the JDK you use in Android Studio

The gradle sync uses the following JDK location:

In Android Studio, File -> Project Structure

Navigate to the SDK Location.

Android Studio 4.1.0 JDK Location

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)
Francis Bacon
  • 4,080
  • 1
  • 37
  • 48
7

On a Mac, you can use terminal to go to /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home (or wherever your Android SDK is installed) and enter the following in the command prompt:

./java -version
JoSSte
  • 2,953
  • 6
  • 34
  • 54
Lorraine
  • 159
  • 2
  • 3
  • 2
    Also using a Mac but I always get "No Java runtime present, requesting install.", even if I go the the specified folder. What worked is `export JAVA_HOME=/Applications/Android\ Studio.app/Contents/jre/jdk/Contents/Home ` and then `java -version` – David Apr 27 '20 at 14:04
4

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
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
Jonathan Rosenne
  • 2,159
  • 17
  • 27
3

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

java -version 

This will display java version installed in android studio

Vishwa Pratap
  • 374
  • 6
  • 14
  • 3
    There are already two answers that suggest this exact solution. Please be sure to review existing answers before contributing your own. If they’re correct, upvote them. Of your answer is meaningfully different, it may be worth editing it to explain why. – Jeremy Caney May 10 '20 at 03:07
  • 10
    This doesn't work. It gives you the version of java used by the system, not the one used by Android Studio. – ThomasW Jul 29 '20 at 06:30
2
  1. 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
  • I tried EVERYTHING. Only This Worked! I am using studio version 4.1.2. Would see if upgrading Android Studio works. – omer Nov 26 '21 at 19:04
  • for ubunut 20.x, in you gradle.properties file `org.gradle.java.home=/usr/lib/jvm/default-java` or whereever you install jdk 11. (in ubuntu it defaults to above path.) – omer Nov 26 '21 at 19:08
2

For the latest version (2022.2.1 Patch 2) at this time, The setting has been moved to gradle setting.

So just open the project structure through File > Project Structure, Select SDK Location from left menu, click on Gradle Settings.

Here select the Gradle JDK you want. I would suggest to select the Android Studio Java Home option.

Android Studio Gradle Setting

Ata Iravani
  • 2,146
  • 7
  • 29
  • 40
0

to set the Java version from the android studio terminal write this command on the terminal (MAC)

cammand -> export JAVA_HOME='-- java home path (can get from Preferences-> Build,Execution,Deployement -> Gradle'

example (set java 11)->

export JAVA_HOME='/Applications/jdk-11.0.16.1.jdk/Contents/Home'

then you can check whether is it changed or not using java -version command