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

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


当前回答

我们可以使用Java 8使用:

在构建。gradle (Project: myProject)添加以下内容 类路径的me.tatarka: gradle-retrolambda: x.x。x ' / / x.x。X是最新版本 在构建。gradle (Module: myModule 应用插件:'me.tatarka.retrolambda' compileOptions { sourceCompatibility JavaVersion。VERSION_1_8 targetCompatibility JavaVersion。VERSION_1_8 }

其他回答

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

目前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)

我在3年前问过这个问题,很明显,这些年来答案已经发生了变化。正如上面许多人已经回答过的那样,就在不久前,答案变成了“是的”。我从来没有更新过被接受的答案,因为它是当时的正确答案。(我不确定堆栈溢出策略是什么)

我只是想给那些还在搜索这个话题的人补充一个答案。截至2017年5月17日,谷歌还宣布Kotlin也是Android开发的官方语言。

我还没有找到官方的新闻稿,但我确实看了一些谷歌的I/O视频。这里是Kotlin团队关于公告的一篇博客文章的链接。

在2019年最新的Android Studio 3.4+上,上述所有解决方案似乎都不起作用。

我想出了一个完美的和最新的解决方案,将您的Android项目迁移或升级到Java 8。

解决方案: 点击文件->项目结构->模块->属性选项卡。 将源兼容性和目标兼容性更改为1.8 (Java 8)

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

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

Android在Android N正式支持Java 8。

特性公告如下,Java 8语言公告如下:

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.