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:
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.
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.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With