After Android Studio sync my gradle project, I see the following message in the event log:
Android Studio is using this JDK location:
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:
Label
my.startup
ProgramArguments
sh
-c
launchctl setenv JAVA_HOME /path/to/my/project/specific/jdk
RunAtLoad
or, if you want to use your system-defined JDK:
Label
my.startup
ProgramArguments
sh
-c
launchctl setenv JAVA_HOME `/usr/libexec/java_home`
RunAtLoad
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
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.