本文介绍了Android Studio正在使用此JDK位置...与默认情况下Gradle使用的位置不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android Studio同步gradle项目后,我在事件日志中看到以下消息:

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

我在Project Structure-> SDK Location-> JDK Location-> Other中设置了/path/to/my/project/specific/jdk

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

我的.bash_profile中包含以下内容:

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

STUDIO_JDK文档说:

因此,我希望Android Studio可以找到/path/to/my/project/specific/jdk,但找不到.是否有专门用于gradle的特殊JDK设置?

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?

推荐答案

更新

这仅在macOS Mojave 10.14.6上发生.在macOS Catalina 10.15.3上,只需在外壳程序中设置JAVA_HOME.

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

TLDR

Finder.app启动时,Android Studio不会收到在.bash_profile中定义的环境变量.您必须launchctl 中定义环境变量:

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

或者,如果要使用系统定义的JDK:

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

launchctl setenv JAVA_HOME `/usr/libexec/java_home`

但这仅适用于计算机的当前会话.接下来,您必须创建一个~/Library/LaunchAgents/environment.plist文件:

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>

或者,如果要使用系统定义的JDK:

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>

plist将在系统重启后激活.您也可以使用launchctl load ~/Library/LaunchAgents/environment.plist立即启动它.

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

更深入的解释

我怀疑Android Studio实际上没有看到我的JAVA_HOME环境变量,所以我检查了 Android Studio进程'环境变量:

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

这意味着Android Studio无法像我所怀疑的那样看到我的JAVA_HOME.

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

接下来,我尝试从终端启动Android Studio:

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>

,这转储了很多输出,包括我的JAVA_HOME.因此,我需要弄清楚如何为从上面描述的Finder.app启动的应用程序设置环境变量.

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.

这篇关于Android Studio正在使用此JDK位置...与默认情况下Gradle使用的位置不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 23:04