31

I was trying to get Android Studio to use Java 1.7 on Mac OS X (Mountain Lion)

I went to File > Project Structure... and then tried to add a JDK with the + button and then selecting JDK.

I navigated to /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home and selected that, but nothing happens. (this is where Oracle's Java updater installed Java 7 automatically)

How do I use Java 1.7 with Android Studio?

hoss
  • 2,430
  • 1
  • 27
  • 42
Jonathan Lin
  • 19,922
  • 7
  • 69
  • 65

6 Answers6

59

For those wonder how you start the actual program Android Studio with Java 7: edit the plist located in /Applications/Android Studio.app/Contents/Info.plist

Change the xml key JVMVersion which is 1.6* to 1.7* (Java 7) or 1.6+ (latest version)

I don't know if there are any problems by doing this so take it easy.

gregers
  • 12,523
  • 8
  • 46
  • 41
Perty
  • 911
  • 2
  • 9
  • 14
  • This fixed my preview problem not the JAVA_HOME. Now a sample device is loaded on my studio, hence gave you a point! – AmirHd Jan 22 '14 at 01:46
  • Plist modification for accessing common functionality. It's obvious no one who works on IDEA actually uses IDEA – Alexander Jun 17 '14 at 21:22
  • 1
    @Perty: thanks mate, it worked. You might want to update your post with the correct path `/Applications/Android Studio.app/Contents/Info.plist` (`Info.plist` with uppercase `I`). – gsbabil Jul 24 '14 at 07:05
  • 1
    Worked for me too. But it could be 1.8* as well as anything else. Just check your JDK installation dir to know your JDK version – Drew Aug 13 '14 at 16:36
  • works, but crashes often - same for all other intellij products – Stefano Oct 28 '14 at 17:42
  • This *will* cause problems with stability and updates. It's much safer to install the [1.6 runtime from Apple](http://support.apple.com/kb/DL1572?viewlocale=en_US&locale=en_US). – Krylez Nov 26 '14 at 08:06
32

There is simple way to do this. Please add following lines in build.gradle in android section:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}

and sync project with Gradle files. Android Studio will detect change (language Level) and ask to reload the project.

wkoszut
  • 321
  • 3
  • 3
  • 1
    Adding this to your APP's build.gradle file in the android{} block will force Android Studio to reload the project. I was having issues where the Android Studio's "Finder" would only see the 1.6.0.jdk in SYSTEM/Library/Java/JavaVirtualMachines/ but Terminal would see 1.7 & 1.8 as well. Is there a "Reload Project" macro in Android Studio that might have fixed this problem with a keystroke? – self.name Jul 04 '14 at 16:03
  • @self.name looks like the Oracle installers for OSX put them in ~/Library rather than /System/Library, in case you want to go select one via the AS interface (dig down into the ..../Home) of the one you want. – Colin M. Nov 13 '14 at 17:57
13

I went to Java SE Downloads and downloaded Java 7 again. The method mentioned above worked for Eclipse.

Running the installer from this manual download placed the JDK in /Library/Java/JavaVirtualMachines/jdk1.7.0_21.jdk/Contents/Home, which was accepted by Android Studio.

Jonathan Lin
  • 19,922
  • 7
  • 69
  • 65
  • 1
    It still tells me that I need to install Java 6 on my Mac to launch Android Studio. Even after updating the JDK to 7. – IgorGanapolsky Oct 30 '13 at 16:53
  • And running java 7 update from Oracle did not replace my jdk in /Library/Java/JavaVirtualMachines/. It is still 1.6! The only thing it did was update the java and javac commandline. – IgorGanapolsky Oct 31 '13 at 15:19
  • I think you need to have 1.6 to build the android app using Studio, but Studio itself can run on 1.7 – Andrew Mackenzie Nov 12 '13 at 16:48
  • Works for me. 1.6 is out of line on Oracle dev. If there is a problem on 1.7 with Android Studio, let me know. –  Jan 15 '14 at 07:14
8

On a mac, check your java version by running this command:

java -version

And you should see a version higher than 1.7. If not, then just run your system updates to get the latest version of java. (Click the "Apple" logo in the upper left and choose About this Mac then click the Software Update... button and install the latest java from there.) In other words, get your latest java from Apple's Updates, if you can–it's easier that way.

Once you have the latest version, then simply run the following command:

/usr/libexec/java_home

This will tell you your Java Home location, which is what Android Studio wants. For me, this value was

/Library/Java/JavaVirtualMachines/jdk1.7.0_65.jdk/Contents/Home

If you'd like to set this as a default for all projects, follow the instructions in this related answer (which deals with how to set your Android SDK but the Java SDK is set in the same screen). In a nutshell, close all projects to get to the Welcome to Android Studio screen then select > Configure > Project Defaults > Project Structure > Android SDK and then input your JDK location, given by the java_home utility.

I just took these steps while creating a project targeting "Lollipop" and it worked like a charm.

Community
  • 1
  • 1
gMale
  • 17,147
  • 17
  • 91
  • 116
  • 2
    Thats just worked ... This is perfect answer for Mac OS versions of Android Studio – John Apr 28 '15 at 04:10
  • Works perfectly on OS X 10.10.4. – simeg Jul 02 '15 at 21:08
  • Works well for me too. Thanks. I'm using Android Studio 1.1.0 on Mac with OS X 10.9.5, JDK 1.8. The tool java_home give me /Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home – hoang21 Sep 19 '15 at 00:21
0

I have the same problem on Android Studio 2.1 and I have java 6 and 7 installed.

When I run java -version or javac -version it shows 1.7.0_79, but Android Studio insists using 1.6.

I had to change the info.plist from 1.6*,1.7+ to 1.7+. Now it works and stop complaining about download updates securely.

Ratata Tata
  • 2,781
  • 1
  • 34
  • 49
0

In Mac, you can choose from within IDE interface by -Shift-a and find Switch IDE boot JDK...

noomz
  • 1,955
  • 4
  • 18
  • 20