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

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


当前回答

添加这一行到模块LVL构建等级

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)

是的,Android现在支持Java 8 (24.1.17)

现在这是可能的

但是你需要让你的设备rom在java 1.8上运行,并启用“jackOptions”来运行它。 Jack是运行Java 8的新的Android编译器的名字

https://developer.android.com/guide/platform/j8-jack.html

将这些行添加到build_gradle

    android {
  ...
  defaultConfig {
    ...
    jackOptions {
      enabled true
    }
  }
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

Java 8似乎是Android studio 2.0的运行Java引擎, 但是经过我的检查,它仍然不接受java 8的语法,你现在不能从android studio中选择编译器。然而,如果你的android客户端需要函数式编程机制,你可以使用scala插件。

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

我在Stack Overflow上对一个类似的问题写了一个类似的答案,但这里是答案的一部分。

Android Studio 2.1:

新版Android Studio(2.1)支持Java 8特性。以下是Android开发者博客文章的摘录:

... Android Studio 2.1版本包括对新的Jack编译器的支持和对Java 8的支持。 ... 在使用N Developer Preview进行开发时,要使用Java 8语言特性,您需要使用Jack编译器。新建项目向导[文件→新建→项目]为目标N的项目生成正确的配置。


在Android Studio 2.1之前:

Android还不支持Java 1.8(它只支持1.7),所以你不能使用Java 8的特性,比如lambdas。

这个答案提供了更多关于Android Studio兼容性的细节;州:

如果你想在Android中使用lambdas (Java 8的主要特性之一),你可以使用gradle-retrolamba

如果您想了解更多关于使用gradle-retrolambda的知识,这个答案提供了很多细节。

点击此链接获取最新更新。使用Java 8语言特性

旧的答案

在Android N预览版中,Android支持Java 8的有限功能,请参阅Java 8语言功能

要开始使用这些功能,你需要下载并设置Android Studio 2.1和Android N预览SDK,其中包括 要求杰克工具链和更新的Android插件为Gradle。如果你 还没有安装Android N预览SDK,请参见设置开发 Android N。

支持Java 8语言特性和api

Android目前不支持所有Java 8语言特性。 但是,在开发应用程序时,现在可以使用以下功能 针对Android N预览版:

默认和静态接口方法

Lambda表达式(API级别23及以下也可用)

可重复的注释

方法参考(API级别23及以下也可用)

还有一些额外的Java 8功能,Android支持,你可以看到完整的Java 8语言功能细节

更新

注意:Android N基于lambda表达式的实现 匿名类。这种方法允许他们后退 兼容和可执行的早期版本的Android。测试 早期版本的Lambda表达式,记得去你的 构建。将“compileSdkVersion”和“targetSdkVersion”设置为 23或更低。

更新2

现在Android studio 3.0稳定版支持Java 8库和Java 8语言特性(没有Jack编译器)。