Android Studio:lambda 不起作用 [英] Android Studio: lambda does not work

查看:57
本文介绍了Android Studio:lambda 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试使用 lambda 表达式时,我遇到了一些 Gradle 构建错误:

When trying to use lambda expressions, I got some Gradle build errors:

错误:(41, 100) 错误:-source 1.7 不支持 lambda 表达式(使用 -source 8 或更高版本来启用 lambda 表达式)

Error:(41, 100) error: lambda expressions are not supported in -source 1.7 (use -source 8 or higher to enable lambda expressions)

错误:任务:app:compileDebugJava"的执行失败.编译失败;详情请参阅编译器错误输出.

Error:Execution failed for task ':app:compileDebugJava'. Compilation failed; see the compiler error output for details.

快速搜索帮助我了解 lambda 表达式仅在 JDK 1.8 中受支持,显然我没有使用它.

A quick search helped me understand that lambda expressions are only supported at JDK 1.8, and apparently I am not using it.

我的问题是 JDK 1.8 确实安装在我的计算机上(我使用的是 Yosemite),在模块设置 → SDK 位置 → JDK 位置它说:

My problem is that JDK 1.8 does install on my computer (I am using Yosemite), In module setting → SDK Location → JDK Location it says:

但是在设置模块→应用程序→源/目标兼容性中,没有1.8"这样的选项,只有Java 1.6或1.7的选项.

But in Settings module → app → Source/Target Compatibility, there is not an option such as "1.8", there are only options for Java 1.6 or 1.7.

我知道没有匿名函数我也可以管理,但我仍然想解决这个问题.

I know I can manage without anonymous functions, but I still want to fix this issue.

推荐答案

更新:2017 年 6 月 19 日

Jack 现已弃用,将来 Java 8 支持将集成到当前工具中.从这篇博文Java 8 语言功能支持的未来安卓:

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

We've decided to add support for Java 8 language features directly into the current javac and dx set of tools, and deprecate the Jack toolchain. With this new direction, existing tools and plugins dependent on the Java class file format should continue to work. Moving forward, Java 8 language features will be natively supported by the Android build system. We're aiming to launch this as part of Android Studio in the coming weeks, and we wanted to share this decision early with you.


更新:2016 年 4 月 26 日

新版 Android Studio (2.1) 支持 Java 8 功能.这是的摘录Android 开发者博客帖子:

... Android Studio 2.1 版本包括对新 Jack 编译器的支持和支持Java 8.

... Android Studio 2.1 release includes support for the new Jack compiler and support for Java 8.

借助 Jack 编译器,可以在所有版本的 Android 平台上使用 lambda、方法引用、编译时类型注释、交集类型和类型推断.Android N 及更高版本提供默认和静态方法以及可重复注释.

With the Jack compiler, lambdas, method references, compile-time type annotations, intersection types and type inference are available on all versions of the Android platform. Default and static methods and repeatable annotations are available on Android N and higher.

要在使用 N Developer Preview 进行开发时使用 Java 8 语言功能,您需要使用 Jack 编译器.新建项目向导[文件→新建→项目]为面向 N 的项目生成正确的配置.

To use Java 8 language features when developing with the N Developer Preview, you need to use the Jack compiler. The New Project Wizard [File→ New→ Project] generates the correct configurations for projects targeting the N.


上一个答案:

Android 尚不支持 Java 1.8(它最多只支持 1.7),因此您不能使用 lambdas(正如也提到的评论之一).


Previous answer:

Android does not support Java 1.8 yet (it only supports up to 1.7), so you cannot use lambdas (as one of the comments also mentioned).

此答案提供了有关 Android Studio 兼容性的更多详细信息;它指出:

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

If you want to use lambdas, one of the major features of Java 8 in Android, you can use gradle-retrolamba

如果您想了解有关使用 gradle-retrolambda 的更多信息,此答案提供了很多关于这样做.

If you want to know more about using gradle-retrolambda, this answer gives a lot of detail on doing that.

否则(在我看来这可能是最好的选择),您将需要重新使用匿名类(即在您改为使用 lambda 函数之前是如何使用它的).

Otherwise (and this is probably the best option in my opinion), you will need to go back to using the anonymous classes (i.e. how you had it before you changed to using lambda functions).

这篇关于Android Studio:lambda 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆