简体   繁体   English

如何在El Capitan(OS X 10.11)中设置Android Studio?

[英]How to setup Android Studio in El Capitan (OS X 10.11)?

Fresh install of El Capitan(10.11), tried to install Android Studio version 1.21. 全新安装的El Capitan(10.11),试图安装Android Studio版本1.21。

Error: 错误:

Android Studio was unable to find a valid JVM. Please download it from:

Download available from Apple -Java for OS X 2014-001 also does not install. 从Apple -Java for OS X 2014-001下载也无法安装。

Error: 错误:

Java for OS X can't be installed on this disk. A newer version of this package is already installed

What is the best way of getting this to run? 让它运行的最佳方法是什么?

For it to work on El Capitan (OSX 10.11) you need to install the Java Development Kit, not only Java VM 要使其在El Capitan(OSX 10.11)上运行,您需要安装Java Development Kit,而不仅仅是Java VM

http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

After installing JDK, Android Studio runs as expected! 安装JDK后,Android Studio按预期运行!

NOTE 注意

Current versions of Android Studio (2.2.2+) have OpenJDK8 embedded which resolves many of the issues surrounding use of Java 1.6. 当前版本的Android Studio(2.2.2+)嵌入了OpenJDK8,它解决了围绕Java 1.6使用的许多问题。 ( source ). 来源 )。 Navigate to File > Project Structure > SDK Location and choose option to use the embedded JDK. 导航到文件>项目结构> SDK位置,然后选择使用嵌入式JDK的选项。 If you still prefer to use your own version of JDK, the instructions below still apply. 如果您仍然喜欢使用自己的JDK版本,则以下说明仍适用。

After some experimentation: 经过一些实验:

I found that one needs to remove Java 1.6 since it is no longer supported by OS X 10.11+. 我发现需要删除Java 1.6,因为OS X 10.11+不再支持它。 Instead install the latest version of the JRE, JDK, and Java Package (from Apple). 而是安装最新版本的JRE,JDK和Java Package (来自Apple)。 This will ensure that Android Studio picks up the latest JVM (and incidentally will also fix most other java based apps). 这将确保Android Studio获取最新的JVM(并且顺便提一下也将修复大多数其他基于Java的应用程序)。

You may experience other error messages at this point that still reference Java 1.6. 此时您可能会遇到仍引用Java 1.6的其他错误消息。 In my case, I had an old JDK version referencing 1.6 (which is now obsolete) in the following folder: 就我而言,我在以下文件夹中有一个旧的JDK版本引用1.6(现在已经过时):

/Library/Java/JavaVirtualMachines /图书馆/的Java / JavaVirtualMachines

To fix: 修理:

  1. Click Finder (Anywhere on the Desktop) 单击Finder(桌面上的任意位置)
  2. Type Cmd-N to open a Finder window 键入Cmd-N以打开Finder窗口
  3. Press Cmd-Shift-G to open a Go to folder Menu 按Cmd-Shift-G打开“转到文件夹菜单”
  4. Paste in the following : /Library/Java/JavaVirtualMachines 粘贴如下:/ Library / Java / JavaVirtualMachines
  5. Delete old JDK files (You will need the admin password) 删除旧的JDK文件(您需要管理员密码)

The net effect is that on relaunch of Android Studio, the JVM version indicated in the Contents\\Info.plist will no longer need to be modified (although since 1.6 is obsolete, I think it should be removed from all future releases). 实际效果是,在重新启动Android Studio时,将不再需要修改Contents \\ Info.plist中指示的JVM版本(尽管1.6已过时,我认为应该从以后的所有版本中删除它)。 When updating Android Studio, the fix applied here will continue to be effective. 更新Android Studio时,此处应用的修补程序将继续有效。

Alternative Solution 替代方案

You can also point Android Studio to use a later version of Java. 您还可以指示Android Studio使用更高版本的Java。 To do so you will need to download the latest JDK onto your system. 为此,您需要将最新的JDK下载到您的系统中。 See the details below. 请参阅以下详细信息。

After you load the latest version of Java you can redirect Android Studio to use that version of Java. 加载最新版本的Java后,您可以重定向Android Studio以使用该版本的Java。 You do this by editing the ~/.bash_profile in your home directory. 您可以通过编辑主目录中的〜/ .bash_profile来完成此操作。

  1. Open Terminal 开放式终端
  2. Type nano ~/.bash_profile 输入nano~ / .bash_profile

Bash Profile Edits Bash配置文件编辑

export JAVA_HOME=$(/usr/libexec/java_home)
export STUDIO_JDK=/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk

Nota Bene ~ Please use the latest JDK available and adjust the version reference as necessary. Nota Bene ~请使用最新的JDK并根据需要调整版本参考。 Don't forget to accept the license before clicking the download. 在单击下载之前,请不要忘记接受许可。

Official Documentation On Configuring Android Studio With Java notes that: 关于使用Java配置Android Studio的官方文档说明:

Android Studio requires two different JDKs: The version of Java that the IDE itself runs with. Android Studio需要两个不同的JDK:IDE本身运行的Java版本。 The version of the JDK that it uses to get the Java compiler from 用于从中获取Java编译器的JDK版本

Updates 更新

I created a stub in Automator to ensure that Android Studio always opens correctly, uses the JDK that you want, and removes various warning messages about any earlier Java that one might still have on their system (for example, one might still keep older versions to support other software). 我在Automator中创建了一个存根,以确保Android Studio始终正确打开,使用您想要的JDK,并删除有关其系统上可能仍然存在的任何早期Java的各种警告消息(例如,可能仍旧保留旧版本)支持其他软件)。

Here's a quick preview of what that Automator script looks like: 这里是Automator脚本的快速预览:

自动机脚本

In Automater, I save the script as an application and store in my Utilities folder (This allows me to give my stub the same name as the Android Studio app, which still needs to be installed in the normal way). 在Automater中,我将脚本保存为应用程序并存储在我的Utilities文件夹中(这允许我为我的存根提供与Android Studio应用程序相同的名称,这仍然需要以正常方式安装)。

存储功能于公用事业

The benefit of this is that one can add the stub to the dock and Spotlight will also detect it. 这样做的好处是可以将存根添加到扩展坞,Spotlight也可以检测到它。 Launching the stub ensures that one no longer gets the prompts about invalid Java versions and that one always uses the explicit JDK intended. 启动存根确保不再获得有关无效Java版本的提示,并且始终使用显式JDK。

访问-通聚光灯

I saved the stub on Github ( https://github.com/mingsai/AndroidStudioStub ) in case others may just want to download the zip and store in the Utilities Folder (Apple Menu > Go > Utilities) or Shift-Command-U. 我将存根保存在Githubhttps://github.com/mingsai/AndroidStudioStub )上以防其他人可能只想下载zip并存储在Utilities文件夹(Apple菜单> Go> Utilities)或Shift-Command-U中。 The stub has the Android Studio icon added to it. 存根添加了Android Studio图标。

You can either 你也可以

  1. reintroduce Java6 or 重新引入Java6
  2. make Android Studio run on a more recent JDK 让Android Studio在更新的JDK上运行

If you favor the latter, you must first download and install the JDK8. 如果您喜欢后者,则必须先下载并安装JDK8。 Then unless Android Studio picks it up automatically, you'll have to configure it to use the newly installed JDK but not through its Info.plist as is often recommended but through a STUDIO_JDK environment variable (see link below for details). 然后,除非Android Studio自动选择它,否则您必须将其配置为使用新安装的JDK,而不是通过推荐的Info.plist ,但通过STUDIO_JDK环境变量(请参阅下面的链接了解详细信息)。 This can be done from the command line, in order to make sure Android Studio starts properly using JDK8: 这可以从命令行完成,以确保Android Studio使用JDK8正常启动:

$ export STUDIO_JDK=/Library/Java/JavaVirtualMachines/jdk1.8.*_**.jdk
$ open /path/to/Android\ Studio.app

but you most likely want to also be able to open it from the GUI. 但你很可能也希望能够从GUI打开它。 In this case you can refer to this SO thread on how to create a plist launch agent which will be able to set the STUDIO_JDK variable for launchd by substituting the command in the linked answer snippet with 在这种情况下,你可以参考这个SO线程 ,了解如何创建一个plist启动代理,它将能够通过替换链接的答案片段中的命令为launchd设置STUDIO_JDK变量

<string>launchctl setenv STUDIO_JDK /Library/Java/JavaVirtualMachines/jdk1.8.*_**.jdk</string>

(of course replace * with your currently installed version). (当然用您当前安装的版本替换* )。

Lastly, you should check the official documentation on the selection of the JDK for Android Studio for more details on the procedure, the drawbacks, the rationale behind the method I described above and information on how to set up the project (not just IDE) JDKs as well. 最后,您应该查看有关选择JDK for Android Studio 的官方文档 ,了解有关程序,缺点,上述方法背后的基本原理以及有关如何设置项目(不仅仅是IDE)JDK的信息的更多详细信息。同样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM