在网上搜索,还不清楚Android开发是否支持Java 8。

在我下载/安装Java 8之前,能不能有人给我指出任何“官方”文档,说Java 8是或不支持Android开发。


当前回答

我想我应该发布一个更新的答案给那些寻找一些更当前的东西的人。

目前Android和Android Studio支持Java 8特性的一个子集。根据他们网站上的Android文档,谷歌说:

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)

此外,以下Java 8语言api也可用:

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)

其他回答

Android使用的Java是Java 6的分支。

从Android SDK版本19开始,您可以通过这样做来使用Java 7特性。目前还没有完全支持Java 8。

添加这个配置版本。Gradle和同步Gradle:

android {
    ...
    defaultConfig {
        ...
        jackOptions {
            enabled true
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
        // or
        //sourceCompatibility 1.8
        //targetCompatibility 1.8
    }
}

简单的方法

你可以为android项目启用java 1.8支持。

开放式项目结构 按Ctrl + Shift + Alt + S 或文件>项目结构 如图所示,在项目结构对话框中将Source Compatibility和Target Compatibility更新为1.8(单击File > Project Structure)。

或者你可以使用gradle

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

同步项目。就是这样!

注意:Java 1.8支持可用于Android Studio 3.0.0或更高版本。有关进一步阅读,请参阅文档。

更新2017/11/04 - Android Studio 3.0现在支持Java 8。Gradle-retrolambda现在不再需要了。参见https://developer.android.com/studio/write/java8-support.html

如果您正在使用gradle-retrolambda,上面的链接还包括迁移指令。原答案如下:


Android不支持Java 8。它只支持到Java 7(如果你有kitkat),它仍然没有invokedynamic,只有新的语法sugar。

If you want to use lambdas, one of the major features of Java 8 in Android, you can use gradle-retrolamba. It's a gradle build dependency that integrates retrolambda, a tool that converts Java 8 bytecode back to Java 6/7. Basically, if you set the compiler in Android Studio to compile Java 8 bytecode, thus allowing lambdas, it'll convert it back to Java 6/7 bytecode which then in turn gets converted to dalvik bytecode. It's a hack for if you want to try out some JDK 8 features in Android in lieu of official support.

我想我应该发布一个更新的答案给那些寻找一些更当前的东西的人。

目前Android和Android Studio支持Java 8特性的一个子集。根据他们网站上的Android文档,谷歌说:

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)

此外,以下Java 8语言api也可用:

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)