在网上搜索,还不清楚Android开发是否支持Java 8。
在我下载/安装Java 8之前,能不能有人给我指出任何“官方”文档,说Java 8是或不支持Android开发。
在网上搜索,还不清楚Android开发是否支持Java 8。
在我下载/安装Java 8之前,能不能有人给我指出任何“官方”文档,说Java 8是或不支持Android开发。
当前回答
我在3年前问过这个问题,很明显,这些年来答案已经发生了变化。正如上面许多人已经回答过的那样,就在不久前,答案变成了“是的”。我从来没有更新过被接受的答案,因为它是当时的正确答案。(我不确定堆栈溢出策略是什么)
我只是想给那些还在搜索这个话题的人补充一个答案。截至2017年5月17日,谷歌还宣布Kotlin也是Android开发的官方语言。
我还没有找到官方的新闻稿,但我确实看了一些谷歌的I/O视频。这里是Kotlin团队关于公告的一篇博客文章的链接。
其他回答
谷歌刚刚宣布Java 8将被Android原生支持,Jack工具链将弃用:
我们已经决定将对Java 8语言特性的支持直接添加到当前的javac和dx工具集中,并弃用Jack工具链。有了这个新方向,依赖于Java类文件格式的现有工具和插件应该可以继续工作。接下来,Java 8语言特性将被Android构建系统原生支持。我们的目标是在未来几周内将其作为Android Studio的一部分发布,我们希望尽早与大家分享这一决定。
更多信息: https://android-developers.googleblog.com/2017/03/future-of-java-8-language-feature.html
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.
在2019年最新的Android Studio 3.4+上,上述所有解决方案似乎都不起作用。
我想出了一个完美的和最新的解决方案,将您的Android项目迁移或升级到Java 8。
解决方案: 点击文件->项目结构->模块->属性选项卡。 将源兼容性和目标兼容性更改为1.8 (Java 8)
添加Java 8支持的最简单方法
compileOptions {
targetCompatibility = '1.8'
sourceCompatibility = '1.8'
}
只需将其添加到您的构建中。gradle文件。
我想我应该发布一个更新的答案给那些寻找一些更当前的东西的人。
目前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)