如何在 Android Studio 中启用 Java 8 语言功能 [英] How to enable Java 8 language features in Android studio
问题描述
现在与 Android Studio 2.4 Preview 4 一起发布,现在支持 Java 8 语言特性.不再需要 Jack,并且需要禁用 Jack 才能使用默认工具链中内置的改进的 Java 8 支持.
Now release with Android Studio 2.4 Preview 4, it is now supported Java 8 language features. Jack is no longer required, and need to disable Jack to use the improved Java 8 support built into the default toolchain.
现在我们需要禁用 Jack 并切换到默认工具链.
Now we need to disable Jack and switch to the default toolchain.
如何在 android studio 项目中启用 Java 8 功能?
How enable Java 8 features to use in the android studio project?
推荐答案
启用 Java 8 支持:
要开始使用受支持的 Java 8 语言功能,请将 Android 插件更新到 2.4.0-alpha4(或更高版本)并将以下内容添加到您的模块 build.gradle
文件:
To start using supported Java 8 language features, update the Android plugin to 2.4.0-alpha4 (or higher) and add the following to your module’s build.gradle
file:
android {
...
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
禁用 jackOptions:
我们可以通过从模块的 build.gradle 文件中删除 jackOptions 块来禁用 Jack 并切换到默认工具链:
We can disable Jack and switch to the default toolchain, by removing the jackOptions block from module’s build.gradle file:
android {
...
defaultConfig {
...
// Remove this block.
jackOptions {
enabled true
}
}
}
注意:如果您的项目使用 Jack、Retrolambda 或 DexGuard,那么 Android studio 默认使用这些工具提供的 Java 8 支持.
Note: If your project is using Jack, Retrolambda, or DexGuard, then Android studio default uses Java 8 support provided by those tool.
禁用 Java 8 支持:
我们还可以在您的项目中禁用 Java 8 功能,以防您遇到任何与 Java 8 相关的问题.我们可以通过添加以下行来更新 gradle.properties
文件以禁用 Java 8 功能:>
We can also disable Java 8 features in your project in case you are facing any issue related Java 8. We can update gradle.properties
file by adding below line to disable Java 8 features:
android.enableDesugar=false
查看使用 Java 8 语言功能,了解更多关于Java 8 特性.
Check Use Java 8 language features for more details about Java 8 features.
这篇关于如何在 Android Studio 中启用 Java 8 语言功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!