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

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


当前回答

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

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

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

其他回答

更新2019/10/28

Android Studio 4.0解决了这个问题。

D8编译器在编译时将Java 8原生api的反向端口补丁到APK中,你的应用程序将在运行时使用该代码,而不是原生api。这个过程被称为脱糖。

更新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

原生Java 8登陆android!终于!

从每个模块中删除Retrolambda插件和Retrolambda块 构建。gradle文件: 要禁用Jack并切换到默认工具链,只需删除 模块构建中的jackOptions块。gradle文件

要开始使用受支持的Java 8语言特性,请将Android插件更新到3.0.0(或更高版本)

从Android Studio 3.0开始,Java 8语言功能现在原生支持Android:

Lambda表达式 方法引用 类型注释(目前类型注释信息在运行时不可用,只在编译时可用); 重复注释 默认和静态接口方法(API级别24或更高,不支持即时运行);

另外,从最低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 ()

将这些行添加到应用程序模块的构建中。Gradle告知项目的语言级别:

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

通过在gradle中添加以下内容禁用对Java 8语言特性的支持。属性文件:

android.enableDesugar=false

你已经完成了!您现在可以使用本机java8!

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

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

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

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

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