Android Studio 正在使用这个 JDK 位置......这与 Gradle 默认使用的不同 [英] Android Studio is using this JDK location ... which is different to what Gradle uses by default

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

问题描述

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

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

Android Studio 正在使用这个 JDK 位置:/path/to/my/project/specific/jdk这与 Gradle 默认使用的不同:/path/to/android/studio/embedded/jdk使用不同的位置可能会产生多个 Gradle 守护进程,如果Gradle 任务在使用 Android Studio 时从命令行运行.设置 Android Studio 使用与 Gradle 相同的 JDK 并同步项目

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

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

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

我的 .bash_profile 中有以下内容:

I have the following in my .bash_profile:

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

STUDIO_JDK 文档 说:

and the STUDIO_JDK documentation says:

设置用于运行 Studio 的 JDK 的位置.当你启动Android Studio,它会检查STUDIO_JDKJDK_HOMEJAVA_HOME环境变量的顺序.

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 设置?

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 仅在 macOS Mojave 10.14.6 上发生.在 macOS Catalina 10.15.3 上,你只需要在你的 shell 中设置 JAVA_HOME.

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.

此答案涉及 macOS 案例.这并不意味着 Linux 或 Windows 解决方案.

This answer deals with macOS cases. It doesn't imply Linux or Windows solutions.

TLDR

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

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

或者,如果您想使用系统定义的 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 默认使用的不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆