Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio: lambda does not work [duplicate]

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

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

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

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

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:

JDK location screenshot

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.

Source/Target Compatibility screenshot

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

like image 349
OrWn Avatar asked May 24 '15 08:05

OrWn


1 Answers

Update: 19th June 2017

Jack is now deprecated, and Java 8 support will be integrated to current tools in future. From this blog post Future of Java 8 Language Feature Support on Android:

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.


Update: 26th April 2016

The new version of Android Studio (2.1) has support for Java 8 features. Here is an extract from the Android Developers blogspot post:

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

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.

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.


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).

This answer gives more detail on Android Studio's compatibility; it states:

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

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

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).

like image 144
Farbod Salamat-Zadeh Avatar answered Sep 21 '22 03:09

Farbod Salamat-Zadeh