Which Jdk Version (Language Level) Is Required for Android Studio

Which JDK version (Language Level) is required for Android Studio?

Answer Clarification - Android Studio supports JDK8

The following is an answer to the question "What version of Java does Android support?" which is different from "What version of Java can I use to run Android Studio?" which is I believe what was actually being asked. For those looking to answer the 2nd question, you might find Using Android Studio with Java 1.7 helpful.

Also: See http://developer.android.com/sdk/index.html#latest for Android Studio system requirements. JDK8 is actually a requirement for PC and linux (as of 5/14/16).


Java 8 update (3/19/14)

Because I'd assume this question will start popping up soon with the release yesterday: As of right now, there's no set date for when Android will support Java 8.

Here's a discussion over at /androiddev - http://www.reddit.com/r/androiddev/comments/22mh0r/does_android_have_any_plans_for_java_8/

If you really want lambda support, you can checkout Retrolambda - https://github.com/evant/gradle-retrolambda. I've never used it, but it seems fairly promising.

Another Update: Android added Java 7 support

Android now supports Java 7 (minus try-with-resource feature). You can read more about the Java 7 features here: https://stackoverflow.com/a/13550632/413254. If you're using gradle, you can add the following in your build.gradle:

android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}

Older response

I'm using Java 7 with Android Studio without any problems (OS X - 10.8.4). You need to make sure you drop the project language level down to 6.0 though. See the screenshot below.

Sample Image

What tehawtness said below makes sense, too. If they're suggesting JDK 6, it makes sense to just go with JDK 6. Either way will be fine.

Sample Image


Update: See this SO post -- https://stackoverflow.com/a/9567402/413254

Language Level Java 8 (Retrolambda) on AndroidStudio 4+?

It is removed on new project, but it is available in the properties after the project is created. Note that I haven't actually compiled with any new Java 8 features yet, so let's hope that works.

Right click on your project, go to Open Module Settings. I think there's a bug somewhere, as once I navigate away from those language settings tab, I can never get back to it.

To work around that, you can edit MyProject/.idea/misc.xml and look for the languageLevel attribute.

To enable Project settings once you navigate away, edit MyProject/.idea/workspace.xml and search for project.structure.last.edited. Set the value to "Project".

Does it matter what version of JDK I use in Android Studio?

Why do we need JDK when we are already using Android SDK? After all, we are not developing for JVM.

The Android build process depends on a number of tools from the JDK. Check out the build system overview documentation.
The first big piece we need from JDK is javac- all your source code written in Java needs to be compiled before it can be converted to the DEX foramt.

Once your code has been compiled, dexed, and packaged into an APK, we need jarsigner to sign the APK.

Is there any difference between using JDK 1.6, 1.7 and 1.8?
That depends on what features you are using from each. Older projects that don't use Java 7 features can use Java 6 without issue. I personally recommend Java 7 for most modern projects. If you can use it, why not?

There are some efforts out there to bring Java 8 features to Android, most notably gradle-retrolambda. Some of these require JDK 8 to compile properly.

What is Project Language level in IntelliJ IDEA?

The Language level setting sets which features the code assistance in the editor should support. For example, if you're using JDK 1.7 but want your code to be compatible with JDK 1.6, you can set the language level lower than your actual JDK supports (6.0 in the case of JDK 1.6) and only get refactorings/syntax suggested that are supported on 1.6 and lower. Depending on your compiler, it may also give the compiler options to remove support for newer syntax elements.

The 8.0 (which, as you're guessing corresponds to Java 8) is available for people that want to experiment with one of the Java 8 snapshots that are available. Since Java 8 isn't released, language level 8.0 may very well change before release.

Java 7 language features with Android

If you are using Android Studio, the Java 7 language should be enabled automatically without any patches. Try-with-resource requires API Level 19+, and NIO 2.0 stuff are missing.

If you can't use Java 7 features, see @Nuno's answer on how to edit your build.gradle.

The following is for historical interest only.


A small part of Java 7 can certainly be used with Android (note: I have only tested on 4.1).

First of all, you could not use Eclipse's ADT because it is hard-coded that only Java compiler 1.5 and 1.6 are compliant. You could recompile ADT but I find there is no simple way to do that aside from recompiling the whole Android together.

But you don't need to use Eclipse. For instance, Android Studio 0.3.2, IntelliJ IDEA CE and other javac-based IDEs supports compiling to Android and you could set the compliance even up to Java 8 with:

  • File → Project Structure → Modules → (pick the module at the 2nd pane) → Language level → (choose "7.0 - Diamonds, ARM, multi-catch, etc.")

Enabling Java 7 on IntelliJ

This only allows Java 7 language features, and you can hardly benefit from anything since a half of improvement also comes from the library. Features you could use are those which do not depend on the library:

  • Diamond operator (<>)
  • String switch
  • Multiple-catch (catch (Exc1 | Exc2 e))
  • Underscore in number literals (1_234_567)
  • Binary literals (0b1110111)

And these features cannot be used yet:

  • The try-with-resources statement — because it requires the non-existing interface "java.lang.AutoCloseable" (this can be used publicly in 4.4+)
  • The @SafeVarargs annotation — because "java.lang.SafeVarargs" does not exist

... "yet" :) It turns out that, although Android's library is targeting for 1.6, the Android source does contain interfaces like AutoCloseable and traditional interfaces like Closeable does inherit from AutoCloseable (SafeVarargs is really missing, though). We could confirm its existence via reflection. They are hidden simply because the Javadoc has the @hide tag, which caused the "android.jar" not to include them.

There is already as existing question How do I build the Android SDK with hidden and internal APIs available? on how to get those methods back. You just need to replace the existing "android.jar" reference of the current Platform with our customized one, then many of the Java 7 APIs will become available (the procedure is similar to that in Eclipse. Check Project Structure → SDKs.)

In additional to AutoCloseable, (only) the following Java 7 library features are also revealed:

  • Exception chaining constructors in ConcurrentModificationException, LinkageError and AssertionError
  • The static .compare() methods for primitives: Boolean.compare(), Byte.compare(), Short.compare(), Character.compare(), Integer.compare(), Long.compare().
  • Currency: .getAvailableCurrencies(), .getDisplayName() (but without .getNumericCode())
  • BitSet: .previousSetBit(), .previousClearBit(), .valueOf(), .toLongArray(), .toByteArray()
  • Collections: .emptyEnumeration(), .emptyIterator(), .emptyListIterator()
  • AutoCloseable
  • Throwable: .addSuppressed(), .getSuppressed(), and the 4-argument constructor
  • Character: .compare(), .isSurrogate(), .getName(), .highSurrogate(), .lowSurrogate(), .isBmpCodePoint() (but without .isAlphabetic() and .isIdeographic())
  • System: .lineSeparator() (undocumented?)
  • java.lang.reflect.Modifier: .classModifiers(), .constructorModifiers(), .fieldModifiers(), .interfaceModifiers(), .methodModifiers()
  • NetworkInterface: .getIndex(), .getByIndex()
  • InetSocketAddress: .getHostString()
  • InetAddress: .getLoopbackAddress()
  • Logger: .getGlobal()
  • ConcurrentLinkedDeque
  • AbstractQueuedSynchronizer: .hasQueuedPredecessors()
  • DeflaterOutputStream: the 3 constructors with "syncFlush".
  • Deflater: .NO_FLUSH, .SYNC_FLUSH, .FULL_FLUSH, .deflate() with 4 arguments

That's basically all. In particular, NIO 2.0 does not exist, and Arrays.asList is still not @SafeVarargs.

How do I change Project language level on Android Studio

You can also edit this inside the Project Structure

Sample Image

androidstudio set java version 1.7

No... there is no settings to change. Android SDK don't support full java 7 syntax, so you can't use it.

Note that java.nio.* (new in java 7 API) is supported by latest android version.

Diamond type are not supported at this language level

In Android Studio (File -> Project Structure..., Properties tab), set the following values:

Source Compatibility == 1.7
Target Compatibility == 1.7

Sample Image

After this your build.gradle will have these entries:

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}

Sample Image

intellij feature (...) not supported at this language level. I can't compile

IntelliJ is showing me this error

are not supported at this language level

Matching the SDK/JDK with Project language level

To activate Java language features in the editor, you need to change your module's Project Structure or Module Settings to ensure your Project SDK matches the Project language level.

Project SDK (JDK) Java 1.5 matches project language level 5 ...

Java 1.6 matches language level 6 ...

Java 1.7 matches language level 7, and so on.

Example

Depending on your IntelliJ version, hit F4 or Ctrl + Alt + Shift - S on your module in the Project pane, or hit Ctrl + Shift - A and type either "Project Structure" or "Module Settings" to open the Project Structure dialog.

Under Project Settings section, click on the first item in the list, Project.

In the screenshot below, the Project SDK 1.8 (java version '1.8.0_72') matches the selected language level SDK Default (8 - Lambdas, type annotations etc.) because Java 1.8 matches SDK level 8. This activates the Java 8 language features for use in the editor.

project structure

If you don't have the JDK set up under Project SDK that is a different problem. Solve that one first by clicking New and adding a JDK.



Related Topics



Leave a reply



Submit