How to Use Java 8 For Android Development

Is it possible to use Java 8 for Android development?

java 8

Android supports all Java 7 language features and a subset of Java 8 language features that vary by platform version.

To check which features of java 8 are supported

Use Java 8 language features

We've decided to add support for Java 8 language features directly into the current javac and dx set of tools, and deprecate the Jack toolchain. With this new direction, existing tools and plugins dependent on the Java class file format should continue to work. Moving forward, Java 8 language features will be natively supported by the Android build system. We're aiming to launch this as part of Android Studio in the coming weeks, and we wanted to share this decision early with you.

Future of Java 8 Language Feature Support on Android

Eclipse Users:

For old developers who prefer Eclipse, google stops support Eclipse Android Developer tools

if you installed Java 8 JDK, then give it a try, if any problems appears try to set the compiler as 1.6 in Eclipse from window menu → PreferencesJavaCompiler.
Java 7 will works too:

Eclipse Preferences -> Java -> Compiler

Java 7 or higher is required if you are targeting Android 5.0 and
higher.

install multiple JDK and try.

Android Studio use Java 8 instead of 11

How can I get android studio to use JDK 8?

Android Studio 4.2 onwards runs on Java 11. It is stated clearly in the documentation.

I have not found any description of workarounds to make Android Studio 4.2 work on Java 8, and I doubt that is technically possible. For a start, the Android Studio 4.2 distro will contain classes compiled for Java 11, and a Java 8 JDK won't be able to load them due to classfile version mismatches. So you would need to rebuild Android Studio from source code. And to do that you would have the problem of backporting various Java-11-isms in the AS 4.2 codebase to work on Java 8.

In short, it is not practical.

You will need to upgrade the old Gradle version / plugins that your app depends on. Alternatively, use an older version of Android Studio that will run on Java 8 ... until you can upgrade Gradle.

Can I use Java 8 with Android Development now?

If you've been using Java 7 to deploy Android apps then it's certain, up to this point, you haven't used any Java 8 features so I don't see how it would matter.

Follow your instructor's directions and when you do an assignment for school simply select either the JDK or Language Level in the Project Structure.

CTRL + ALT + S, select Project

You can default to the Java 8 SDK but limit it to Java 7's features for your Android apps. Or you can simply set your homework projects to the Java 8 SDK.

Going out on a limb here assuming Android Studio includes the core settings of Intellij.

Android Studio compile project with Java 8?

When goes to JDK setting you can:

add in "android app" module build.gradle

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

setup sdk path in gradle.properties

org.gradle.java.home=/path_to_java_sdk_8/jdk1.8

check also .idea/modules/compiler.xml against 1.7/1.8

<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
...
<bytecodeTargetLevel target="1.8">
<module name="app" target="1.7" />
</bytecodeTargetLevel>
</component>

and misc.xml

<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
....
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7"
default="false" assert-keyword="true"
jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">
<option name="id" value="Android" />
</component>
</project>

BTW: if you want compile againsta java 1.8 you need to uae "jack"
see https://source.android.com/source/jack.html

How use java 8 in Android api 21?

Comments by @leonardkraemer and @gabe-sechan cover most of the topic.

For most of the features you pretty much just have to use desugaring and Android Studio 3+. After you set java version, like shown below, Android Studio will start suggesting Java 8 things in your code.

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

Streams are not a language feature, but a library, so there's a good port of it: https://github.com/aNNiMON/Lightweight-Stream-API. This library covers java.util.stream and java.util.function.

Should we use Java 8 now for Android development?

Yes, in my opinion you should. It brings in some nice features like lambda expressions or method references or streams (not supported for every version, though) that make the code neater. On the other hand, neatness may not be seen as an advantage for some as it can be considered less readable. In addition, your Android plugin for Gradle must be at least 3.0.0 that may not be an option for every project.

As mentioned earlier, some features of JAVA 8 are not supported if your minSdk version is lower than 24.

What is supported for any minSdk:

  1. Lambda expressions
  2. Method references
  3. Type annotations
  4. Default and static interface methods.
  5. Repeating annotations

For more information please refer to - https://developer.android.com/studio/write/java8-support

Using Java 8 in android studio

So, how can I change that on AndroidStudio?

you can't. Android supports up to java 7. If you want to use AS only for Lambda, you can use gradle-retrolamba or retrolambda, which back-port retrolambda on java 6/7

How to use Java 8 Stream API under Android 6.0

Update 2017-04-04

Jack is deprecated, Google is replacing it with something called desugar. It is now available with Android Studio 2.4 preview 4 and later.

Java 8 language/library feature availability is still dependent on the device API level and Android Studio version so make sure to double check what you can and can't use.

To use it you'd just set the source and target language levels to Java 8.

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

Note that if you're already using retrolambda or jack, you will need to disable them for desugar to be used. You find more info on how to use it here.

I have not yet tried it myself since I prefer IntelliJ IDEA and quick research into how to use it with IDEA wasn't fruitful. I will update this answer again once I figure out how to use it in IDEA.

Old answer

Some Java 8 language features are available when using Jack compiler.

to enable Jack, edit your build.gradle like so

android {
...
defaultConfig {
...
jackOptions {
enabled true
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

More info can be found here

However, I would like to add that I've had pretty bad experience with jack so far, especially when debugging. I would recommend using streamsupport and retrolambda instead for now until jack's sister Jill is released.



Related Topics



Leave a reply



Submit