在网上搜索,还不清楚Android开发是否支持Java 8。
在我下载/安装Java 8之前,能不能有人给我指出任何“官方”文档,说Java 8是或不支持Android开发。
在网上搜索,还不清楚Android开发是否支持Java 8。
在我下载/安装Java 8之前,能不能有人给我指出任何“官方”文档,说Java 8是或不支持Android开发。
当前回答
谷歌刚刚宣布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
其他回答
更新2020/01/17
Android Studio 4.0支持使用大量的Java 8语言API,通过使用称为desugaring的技术,而不需要为你的应用程序设置最低的API级别: https://developer.android.com/studio/preview/features#j8-desugar
The following set of APIs is supported in this release: Sequential streams (java.util.stream) A subset of java.time java.util.function Recent additions to java.util.{Map,Collection,Comparator} Optionals (java.util.Optional, java.util.OptionalInt and java.util.OptionalDouble) and some other new classes useful with the above APIs Some additions to java.util.concurrent.atomic (new methods on AtomicInteger, AtomicLong and AtomicReference) ConcurrentHashMap (with bug fixes for Android 5.0) To support these language APIs, D8 compiles a separate library DEX file that contains an implementation of the missing APIs and includes it in your app. The desugaring process rewrites your app’s code to instead use this library at runtime. To enable support for these language APIs, include the following in your module’s build.gradle file: android { defaultConfig { // Required when setting minSdkVersion to 20 or lower multiDexEnabled true } compileOptions { // Flag to enable support for the new language APIs coreLibraryDesugaringEnabled true // Sets Java compatibility to Java 8 sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } dependencies { coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.4' }
2017年原创文章
Android Studio 3.0开始提供对Java 8语言特性的内置支持,包括:
Lambda表达式 方法引用 类型注释(信息在编译时可用,但在运行时不可用) 重复注释 默认和静态接口方法
同样,从API级别24开始,可以使用以下Java 8 API:
java.util.stream java.util.function java.lang.FunctionalInterface java.lang.annotation.Repeatable java.lang.reflect.AnnotatedElement.getAnnotationsByType(类) java.lang.reflect.Method.isDefault ()
除此之外,try-with-resources支持扩展到所有Android API级别。
更多的Java 8特性将在未来被添加。
要开始使用受支持的Java 8语言特性,请更新Android 插件到3.0.0-alpha1(或更高版本),并将以下内容添加到您的 模块的构建。gradle文件: android { ... compileOptions { sourceCompatibility JavaVersion。VERSION_1_8 targetCompatibility JavaVersion。VERSION_1_8 } }
详情请浏览: https://developer.android.com/studio/write/java8-support.html
我在3年前问过这个问题,很明显,这些年来答案已经发生了变化。正如上面许多人已经回答过的那样,就在不久前,答案变成了“是的”。我从来没有更新过被接受的答案,因为它是当时的正确答案。(我不确定堆栈溢出策略是什么)
我只是想给那些还在搜索这个话题的人补充一个答案。截至2017年5月17日,谷歌还宣布Kotlin也是Android开发的官方语言。
我还没有找到官方的新闻稿,但我确实看了一些谷歌的I/O视频。这里是Kotlin团队关于公告的一篇博客文章的链接。
添加这一行到模块LVL构建等级
compileOptions { sourceCompatibility JavaVersion。VERSION_1_8 targetCompatibility JavaVersion。VERSION_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.