Heath Borders
Heath Borders

Reputation: 32167

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

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?

Upvotes: 34

Views: 70709

Answers (9)

MarGin
MarGin

Reputation: 2558

In mac os(version: Monterey, the Android Studio was showing an old JAVA version as the JAVA_HOME. The issue has been resolved by updating zshrc profile

the Update JAVA_HOME System-Wide Open Terminal on your Mac.

Edit your shell profile file (e.g., .zshrc or .bash_profile) using a text

nano ~/.zshrc

Add or update the JAVA_HOME export line:

export JAVA_HOME="/Applications/Android Studio.app/Contents/jbr/Contents/Home"

Save the file and exit the editor.

After completing this step and restarting Android Studio, the changes were successfully updated and I could then select the Java home.

  Preferences-> Build,Execution,Deployment-> Build tools-> Gradle

Upvotes: 0

Sher Sanginov
Sher Sanginov

Reputation: 605

In Android Studio Jellyfish | 2023.3.1 Patch 1, you need to go to Settings -> Build Execution, Deployment to update Gradle JDK:

enter image description here

Upvotes: 1

Sameer Shelar
Sameer Shelar

Reputation: 196

In my case this issue came when I created a new project. To resolve this I selected the below shown JDK.

enter image description here

Upvotes: 0

auspicious99
auspicious99

Reputation: 4331

With Android Studio 4.1 on MacOS Catalina 10.15.7, launching Android Studio from the finder still does not pick up JAVA_HOME (despite what the update of the accepted answer said). Hence, Android Studio was using its embedded JDK, whereas gradle was using the JDK specified in JAVA_HOME.

In my case, I didn't particularly care about the version (they were close enough), so I just set gradle to use the embedded JDK (which Android Studio was using by default). If you do care and do not wish to use the embedded JDK, follow the accepted answer. Otherwise, what I did may be faster.

Here's how to choose to use the embedded JDK for gradle. At least now the wording in Android Studio is clearer, that this setting is only for the JDK used by Gradle. Setting the JDK used by gradle in Android Studio project settings

Upvotes: 3

Heath Borders
Heath Borders

Reputation: 32167

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.

Upvotes: 22

liedji
liedji

Reputation: 789

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.

Upvotes: 7

Peter Wanden
Peter Wanden

Reputation: 316

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.

Upvotes: 3

David Hash
David Hash

Reputation: 107

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.

Upvotes: 2

user3930098
user3930098

Reputation: 445

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.

Upvotes: 13

Related Questions