48

I can't compile a default new project for Android 2.1 because(?) the JDK used by Android Studio is JDK 1.7 but it should be JDK 1.6.

To change it I go to File -> Project Structure and try to find the JDK 1.6 location.

But when I select its directory and press OK I get the following error:

Please choose a valid JDK directory

Please choose a valid JDK directory

As you can see on the screenshoot I'm trying to set Java 6 OpenJDK 64-bits as the new JDK.

What can I do? Isn't Android Studio compatible with OpenJDK and should I use Oracle JDK instead?

Lucio
  • 19,191
  • 32
  • 112
  • 191

8 Answers8

26

I'm using OpenJDK 7 with Android Studio and it works just fine. (I don't even have Oracle JDK installed at the moment.)

On that screen that you are showing (Project Structure-->SKD Location) I have the following path for the JDK location: /usr/lib/jvm/java-1.7.0-openjdk-amd64.

Daniel C
  • 4,170
10

By inspecting studio.sh, you can also set one of STUDIO_JDK, JDK_HOME or JAVA_HOME environment variables to point to the JDK location.


studio.sh checks the STUDIO_JDK, JDK_HOME, and JAVA_HOME environment variables in order:

if [ -n "$STUDIO_JDK" -a -x "$STUDIO_JDK/bin/java" ]; then
  JDK="$STUDIO_JDK"
elif [ -n "$JDK_HOME" -a -x "$JDK_HOME/bin/java" ]; then
  JDK="$JDK_HOME"
elif [ -n "$JAVA_HOME" -a -x "$JAVA_HOME/bin/java" ]; then
  JDK="$JAVA_HOME"

So you can just set them as appropriate and then Studio will pick it up.

Eliah Kagan
  • 119,640
Nuno
  • 101
  • 1
  • 3
5

It seems that Android Studio does not recognize OpenJDK, so I need to install the OracleJDK.

Then I choose the path to Oracle JDK and it worked!

Lucio
  • 19,191
  • 32
  • 112
  • 191
4

This worked for me:

  1. Find out the java directory:

    $ echo $JAVA_HOME
    /usr/lib/jvm/java-7-oracle
    
  2. Open Android Studio and then File-> Project Structure.

  3. Under SDK Location I past the java directory from above
  4. Wait a couple of seconds and I got "0 errores 0 advertencias"

It worked! :)

Lucio
  • 19,191
  • 32
  • 112
  • 191
2

Steps I followed:

  1. Opened terminal and enter echo $JAVA_HOME

  2. Copied the path displayed in terminal - Ctrl+Shift+C copy in Ubuntu terminal

  3. In Android studio - Project Structure - Pasted the copied path

  4. Waited for the software to recognize and update the settings

  5. This solved the problem of Invalid Jdk settings

anand24
  • 129
1

I fixed this problem. I use AS 2.1 and JDK1.7 while it required me to update JDK1.8 for some functions. I download the java setup file. Maybe I only installed jre but not jdk. so AS always warn me that "Please choose a valid JDK directory". So I guess the asker's problem same as me that you should install a JDK but not jre.

1

If you have a ./bin folder with a JDK and Android Studio, you can set the STUDIO_JDK variable before running studio.sh in a terminal:

STUDIO_JDK=./bin/jdk/ ./bin/android-studio/bin/studio.sh
0

I think that you might have installed the jdk somewhere else for e.g. /usr/local/java. In my case also I saw the same folder name in the directory shown in your screenshot.

So just check if the jdk is present somewhere else.