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

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


当前回答

是的。我们将很快使用Java 8 !

我们已经决定将对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

其他回答

当我在大约2年前问这个问题时,答案确实是“官方的”否定的,但是正如ekcr1的答案所指出的那样,如果您使用retrolamba,您可以获得最受期待的特性之一(lambdas)。当时我仍在使用eclipse,因为Android Studio处于“预览”模式,所以我从未走上这条道路。

今天,我认为“官方”的答案仍然是否定的,虽然retrolamba看起来仍然是一个不错的选择,但对于那些愿意走一条有点“非官方”路线的人来说,还有另一个选择,那就是Kotlin。

今天Kotlin达到了1.0.0。对于那些不熟悉Kotlin的人,更多的信息可以在他们的网站上找到:

https://kotlinlang.org

或者观看Jake Wharton的演讲视频

https://www.youtube.com/watch?v=A2LukgT2mKc

java 8

Android支持所有Java 7语言特性和Java 8语言特性的子集,这些特性因平台版本而异。

检查支持java 8的哪些功能

使用Java 8语言特性

我们已经决定将对Java 8语言特性的支持直接添加到当前的javac和dx工具集中,并弃用Jack工具链。有了这个新方向,依赖于Java类文件格式的现有工具和插件应该可以继续工作。接下来,Java 8语言特性将被Android构建系统原生支持。我们的目标是在未来几周内将其作为Android Studio的一部分发布,我们希望尽早与大家分享这一决定。

Android上Java 8语言特性支持的未来

Eclipse用户:

对于喜欢Eclipse的老开发人员,谷歌停止支持Eclipse Android Developer工具

如果你安装了Java 8 JDK,那么试一试,如果出现任何问题,尝试在Eclipse中从窗口菜单→首选项→Java→编译器设置编译器为1.6。 Java 7也可以工作:

如果你的目标是Android 5.0和 更高。

安装多个JDK并尝试。

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

添加以下内容修复了我的问题(Android studio 2.3.2):

构建。gradle(项目)

buildscript {
repositories {
    ...
    jcenter()
}
dependencies {
    ...
    classpath 'me.tatarka:gradle-retrolambda:3.4.0' // DEPENDENCY
    ...
   }
}

构建。gradle(模块:app)

apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda' //PLUGIN

android {
    ...
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    } // SET JAVA VERSION
    ...
}

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

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