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

46,375

Solution 1

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.

Solution 2

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.

Solution 3

enter image description hereI am using a windows system from microsoft too. To solve the this problem, just follow these simple steps.

  1. Set your JAVA_HOME environnement variable to point to your installed jdk. In my case that was C:\Program Files\Java\jdk-9.0.1

  2. Then, In Android studio, click on Select a JDK from the File System from your Even Log or go to project struture then sdk location.

  3. Set the jdk location as indicate in the above image and click on apply.

Solution 4

I'm running Windows and had the same error when upgrading to AS 3.6.1.

As well as Android projects I also build Java projects on the same machine, both of which use different versions of Java. So pointing AS / Gradle wrapper to my JAVA_HOME is not an option.

To solve the problem I need my environment variable JAVA_HOME to point to my Java projects and JAVA_HOME for AS / Gradle to point to the version of the JRE shipped with AS.

There is a simple solution. Its a bit messy, but it works. To do this you will need to be able to see hidden files. Then mod the Gradle wrapper batch file as follows:

C:/Users/your_user_name/.gradle/wrapper/dists/the_latest_gradle_version(mine is 5.6.4)/bxirm19lnfz6nurbatndyydux/gradle-5.6.4/bin/gradle.bat

Go to the function:

:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe

And change to:

:findJavaFromJavaHome
set JAVA_HOME=%C:/Program Files/Android/AndroidStudio/jre:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe

This isn't a permanent solution, as it may well be overwritten with new versions of Gradle, however it works for now and hopefully long enough for someone (hopefully Google) to point AS and is Gradle wrappers to the correct location by default.

Solution 5

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.

Share:
46,375
user963601
Author by

user963601

I've been a professional software developer since 2002. Presented at StrangeLoop: http://www.infoq.com/presentations/Testing-Testing-iOS https://thestrangeloop.com/sessions/make-your-mobile-apps-accessible-to-all Presented at Midwest IO: https://www.youtube.com/watch?v=VC99D_Vf8IY Presented at Cocoaconf: http://cocoaconf.com/speaker/viewDetails/31 I work at Twitch (previously at Jive Software, Asynchrony, and Cerner).

Updated on June 14, 2021

Comments

  • user963601
    user963601 almost 3 years

    After Android Studio sync my gradle project, I see the following message in the event log:

    Android Studio is using this JDK location: /path/to/my/project/specific/jdk which is different to what Gradle uses by default: /path/to/android/studio/embedded/jdk Using different locations may spawn multiple Gradle daemons if Gradle tasks are run from command line while using Android Studio. Set Android Studio to use the same JDK as Gradle and sync project

    I have /path/to/my/project/specific/jdk set in Project Structure->SDK Location -> JDK Location -> Other Project Structure -> SDK Location -> JDK Location -> Other

    I have the following in my .bash_profile:

    export JAVA_HOME=/path/to/my/project/specific/jdk
    

    and the STUDIO_JDK documentation says:

    Sets the location of the JDK with which to run Studio. When you launch Android Studio, it checks the STUDIO_JDK, JDK_HOME, and JAVA_HOME environment variables in that order.

    So I would expect that Android Studio would find /path/to/my/project/specific/jdk, but it doesn't. Is there a special JDK setting specifically for gradle?