android-studio - Android Studio 正在使用这个 JDK 位置......这与 Gradle 默认使用的不同

标签 android-studio gradle jvm

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

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



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

我的 .bash_profile 中有以下内容:
export JAVA_HOME=/path/to/my/project/specific/jdk

STUDIO_JDK documentation说:

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.



所以我希望 Android Studio 会找到 /path/to/my/project/specific/jdk ,但事实并非如此。是否有专门针对 gradle 的特殊 JDK 设置?

最佳答案

更新
对于 macOS仅发生在 macOS Mojave 10.14.6 .在 macOS Catalina 上 10.15.3 ,您只需要设置JAVA_HOME在你的 shell 里。
这个答案涉及 macOS案件。它并不意味着 Linux 或 Windows 解决方案。
TLDR
macOS ,Android Studio 不会收到您在 .bash_profile 中定义的环境变量从 Finder.app 启动时.您必须define your environment variables in launchctl :

launchctl setenv JAVA_HOME /path/to/my/project/specific/jdk
或者,如果您想使用系统定义的 JDK:
launchctl setenv JAVA_HOME `/usr/libexec/java_home`
但这仅适用于您机器的当前 session 。接下来,您必须创建一个 ~/Library/LaunchAgents/environment.plist文件:
<?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:
<?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立即启动它。
深入讲解
我怀疑 Android Studio 实际上并没有看到我的 JAVA_HOME环境变量,所以我检查了 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 ,正如我怀疑的那样。
接下来,我尝试从终端启动 Android Studio:
$ 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 启动的应用程序设置环境变量。 ,我在上面描述过。

关于android-studio - Android Studio 正在使用这个 JDK 位置......这与 Gradle 默认使用的不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56192983/

相关文章:

java - Gradle 找不到 tools.jar

gradle - 运行gradle时如何检查java版本?

java - 调用 System.gc() 的良好做法

android - 在 Android Studio 中究竟将 gradle 依赖项放在哪里?

Android Studio 显示对话框 "unable to create debug bridge : unable to start adb server: unable to obtain result of ' adb version'"

gradle - 从配置中部分继承依赖项

java - 在 64 位 Windows 中使用 32 位 Java

java - 维护线程池任务队列的类型信息

android - 在 Linux 操作系统 (Ubuntu) : Android Facebook SDK 中生成发布 key 哈希

java - 包含字符串作为代码