Is it possible to use Java 8 for Android developme

2018-12-31 02:30发布

Searching the web, it is not clear if Java 8 is supported for Android development or not.

Before I download/setup Java 8, can some one point me at any "official" documentation that says Java 8 is or is not supported for Android development.

21条回答
千与千寻千般痛.
2楼-- · 2018-12-31 03:26

Yes. We will use Java 8 soon!

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.

https://android-developers.googleblog.com/2017/03/future-of-java-8-language-feature.html

查看更多
孤独寂梦人
3楼-- · 2018-12-31 03:27

Most easy way

You can enable java 1.8 support for android project.

  • Open Project Structure

    • Either by pressing Ctrl + Shift + Alt + S

    • Or File > Project Structure

  • Update the Source Compatibility and Target Compatibility to 1.8 in the Project Structure dialog as shown (click File > Project Structure).

image

Note: Java 1.8 support can be enabled for Android Studio 3.0.0 or higher. See Documentation for further reading.

查看更多
永恒的永恒
4楼-- · 2018-12-31 03:30

Android OFFICIALLY supports Java 8 as of Android N.

Feature announcements are here, the Java 8 language announcement is:

Improved Java 8 language support - We’re excited to bring Java 8 language features to Android. With Android's Jack compiler, you can now use many popular Java 8 language features, including lambdas and more, on Android versions as far back as Gingerbread. The new features help reduce boilerplate code. For example, lambdas can replace anonymous inner classes when providing event listeners. Some Java 8 language features --like default and static methods, streams, and functional interfaces -- are also now available on N and above. With Jack, we’re looking forward to tracking the Java language more closely while maintaining backward compatibility.

查看更多
呛了眼睛熬了心
5楼-- · 2018-12-31 03:30

Google just announced that Java 8 will be natively support by Android and that the Jack toolchain will deprecate:

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.

More Info here: https://android-developers.googleblog.com/2017/03/future-of-java-8-language-feature.html

查看更多
旧人旧事旧时光
6楼-- · 2018-12-31 03:33

I figured I would post an updated answer for those looking at for something a little more current.

Currently Android and Android Studio are supporting a subset of Java 8 features. According to the Android documentation located on their website, Google says:

Support for Java 8 language features requires a new compiler called Jack. Jack is supported only on Android Studio 2.1 and higher. So if you want to use Java 8 language features, you need to use Android Studio 2.1 to build your app.

If you already have Android Studio installed, make sure you update to the latest version by clicking Help > Check for Update (on Mac, Android Studio > Check for Updates). If you don't already have the IDE installed on your workstation, download Android Studio here.

Supported Java 8 Language Features and APIs

Android does not support all Java 8 language features. However, the following features are available when developing apps targeting Android 7.0 (API level 24):

  • Default and static interface methods Lambda expressions (also available on API level 23 and lower)
  • Repeatable annotations
  • Method References (also available on API level 23 and lower)
  • Type Annotations (also available on API level 23 and lower)

Additionally, the following Java 8 language APIs are also available:

Reflection and language-related APIs:

  • java.lang.FunctionalInterface
  • java.lang.annotation.Repeatable
  • java.lang.reflect.Method.isDefault() and Reflection APIs associated with repeatable annotations, such as AnnotatedElement.getAnnotationsByType(Class)

Utility APIs:

  • java.util.function
  • java.util.stream

In order to use the new Java 8 language features, you need to also use the Jack toolchain. This new Android toolchain compiles Java language sources into Android-readable DEX bytecode, has its own .jack library format, and provides most toolchain features as part of a single tool: repackaging, shrinking, obfuscation and multidex.

Here is a comparison of the two toolchains used to build Android DEX files:

Legacy javac toolchain:
    javac (.java → .class) → dx (.class → .dex)
    New Jack toolchain:
    Jack (.java → .jack → .dex)
查看更多
看风景的人
7楼-- · 2018-12-31 03:34

Adding the following fixed the problem for me (Android studio 2.3.2):

build.gradle (Project)

buildscript {
repositories {
    ...
    jcenter()
}
dependencies {
    ...
    classpath 'me.tatarka:gradle-retrolambda:3.4.0' // DEPENDENCY
    ...
   }
}

build.gradle (Module: app)

apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda' //PLUGIN

android {
    ...
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    } // SET JAVA VERSION
    ...
}
查看更多
登录 后发表回答