Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android and Lambda

I need to integrate some code with extensive usage of Java lambda functions. Several restrictions I have demand that I develop my project using Eclipse Mars, with the latest ADT plugin, and not Android Studio.

The problem is that using Lambda functions demands using 1.8 JDK compliance, but if set so, I get this message:

Android requires compiler compliance level 5.0 or 6.0. Found '1.8' instead.

How can the two live together in harmony?

Edit: This is not a a duplicate of the questions suggested, as I'm asking about ADT Eclipse, and since the last update in that question, Android does support Java 8, so no only is this not a duplicate, but that question is now (after 1.5 yrs after the last update) obsolete.

like image 904
Ysch Avatar asked Aug 08 '15 20:08

Ysch


1 Answers

Update on Java 8 language features on Android

Lambda is back ported to older versions of Android.

This is a feature from Android Gradle Plugin 3.0 and above, lambda is back ported to older Android OS versions as part of other Java 8 language features.

Add this to your Gradle build scripts to enable the feature.

android {
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

For more details, see Use Java 8 language features, and Android' Java 8 support.

As @dhke said, there's no support for Java 8 on Android yet.

Use Java 8, Build For Java 6/7

But you can still use JDK 8 to develop Android application. You just need to set source compatibility to either 6 or 7 depends on your minSDKVersion. Thus, you would lose any new features introduced in Java 8, like lambda in your case.

Backport of Lamda

Since you have extensive usage of lambda, Retrolambda might be an option for you. It provides backport of lambda for pre-Java 8 versions. It has Maven/Gradle/command line plugin to enable the support.

Other Backports

If you need other Java 8 features, AFAIK, ThreeTen ABP provides backport support for Java 8 Date Time API.

like image 174
Aaron He Avatar answered Oct 10 '22 18:10

Aaron He