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
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
, andJAVA_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?
Set the JDK version Open your project in Android Studio and select File > Settings... > Build, Execution, Deployment > Build Tools > Gradle (Android Studio > Preferences... > Build, Execution, Deployment > Build Tools > Gradle on a Mac). Under Gradle JDK, choose the Embedded JDK option. Click OK.
Method #1: For Android Studio Version 4.2 and Above Navigate to File > Project Structure > SDK Location. Upon navigating there you will find that a tab named “JDK Location”, select that and then you can set the JDK path for the current project on which you're working.
Note that installing Android Studio doesn't automatically configure the built-in JDK command line tools. This matters if you want to use the Android SDK command line tools.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With