Android Studio is using this JDK location ... which is different to what Gradle uses by default

Update

For macOS only happens on macOS Mojave 10.14.6. On macOS Catalina 10.15.3, you only need to set JAVA_HOME in your shell.

This answer deals with macOS cases. It doesn't imply Linux or Windows solutions.

TLDR

On macOS, Android Studio doesn't receive your environment variables defined in your .bash_profile when launched from Finder.app. You must define your environment variables in launchctl:

launchctl setenv JAVA_HOME /path/to/my/project/specific/jdk

or, if you want to use your system-defined JDK:

launchctl setenv JAVA_HOME `/usr/libexec/java_home`

But this only works for the current session of your machine. Next, you have to create a ~/Library/LaunchAgents/environment.plist file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>my.startup</string>
  <key>ProgramArguments</key>
  <array>
    <string>sh</string>
    <string>-c</string>
    <string>
    launchctl setenv JAVA_HOME /path/to/my/project/specific/jdk
    </string>

  </array>
  <key>RunAtLoad</key>
  <true/>
</dict>
</plist>

or, if you want to use your system-defined JDK:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>my.startup</string>
  <key>ProgramArguments</key>
  <array>
    <string>sh</string>
    <string>-c</string>
    <string>
    launchctl setenv JAVA_HOME `/usr/libexec/java_home`
    </string>

  </array>
  <key>RunAtLoad</key>
  <true/>
</dict>
</plist>

The plist will activate after system reboot. You can also use launchctl load ~/Library/LaunchAgents/environment.plist to launch it immediately.

Deeper explanation

I suspected that Android Studio didn't actually see my JAVA_HOME environment variable, so I inspected the Android Studio process' environment variables:

$ ps ax | grep Android
13466   ??  S    177:42.60 /path/to/my/Android/sdk/emulator/qemu/darwin-x86_64/qemu-system-x86_64 -netdelay none -netspeed full -no-snapstorage -avd Pixel_2_API_28
13478   ??  S      0:04.88 /path/to/my/Android/sdk/emulator/emulator64-crash-service -pipe com.google.AndroidEmulator.CrashService.13466 -ppid 13466 -data-dir /tmp/foo/9ecb0c71-921f-44b8-8b77-f34ac80bb8fa
40253   ??  R      6:21.34 /Applications/Android Studio-3.5-Preview.app/Contents/MacOS/studio
40342   ??  S      0:00.07 /Applications/Android Studio-3.5-Preview.app/Contents/bin/fsnotifier
40610 s001  S+     0:00.00 grep Android

$ ps eww 40253
/Applications/Android Studio-3.5-Preview.app/Contents/MacOS/studio TMPDIR=/var/folders/j4/htlnmbf97vlcdszj7_x8g0vh4k3_fp/T/ __CF_USER_TEXT_ENCODING=0x921A9D6:0x0:0x0 SHELL=/bin/false HOME=/Users/myusername Apple_PubSub_Socket_Render=/private/tmp/com.apple.launchd.zL6tIxvlEo/Render SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.sKG8qr6MNW/Listeners PATH=/usr/bin:/bin:/usr/sbin:/sbin LOGNAME=myusername XPC_SERVICE_NAME=com.google.android.studio-EAP.21860 USER=myusername XPC_FLAGS=0x1

which meant Android Studio didn't see my JAVA_HOME, as I suspected.

Next, I tried launching Android Studio from the terminal:

$ echo $JAVA_HOME
/path/to/my/project/specific/jdk
$ open /Applications/Android Studio-3.5-Preview.app
$ ps eww <Android Studio Pid>

and this dumped a lot more output, including my JAVA_HOME. Thus, I needed to figure out how to set an environment variable for apps launched from Finder.app, which I describe above.


I started getting this warning after updating to Android studio version 3.6. I was getting below warning:

    Android Studio and Gradle are using different locations for the JDK.
    Android Studio: C:\Program Files\Android\Android Studio\jre
    Gradle: C:\Program Files\AdoptOpenJDK\jdk-11.0.3.7-hotspot
    Using different JDK locations might cause Gradle to spawn multiple daemons
    when executing tasks for Android Studio and other external processes.

To fix this warning I changed the project JDK location to Gradle JDK location.

  1. Right click on app in project window
  2. open module settings
  3. SDK location
  4. JDK location
  5. from dropdown select JDK location of your Gradle. In my case it is below location :

    Gradle: C:\Program Files\AdoptOpenJDK\jdk-11.0.3.7-hotspot
    

Click apply and Ok.


I am working on a windows system. I went to the control panel and opened the environment variables and copied the location of the JAVA_HOME key. I placed this directory into the Project Structure/SDK Location window in Android Studio. After saving the changes Gradle was able to sync with no issues. The location of the SDK Locations window is in the question for this post.