12

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Ysch
  • 752
  • 2
  • 9
  • 24
  • 1
    possible duplicate of [Which JDK version (Language Level) is required for Android Studio?](http://stackoverflow.com/questions/17869069/which-jdk-version-language-level-is-required-for-android-studio) – Michael Easter Aug 08 '15 at 20:12
  • How is that question a possible duplicate in any way whatsoever? – Ysch Aug 08 '15 at 20:14
  • Reading the error message, they can't. You simply have to wait until Android supports Java 8. – Andy Turner Aug 08 '15 at 20:14
  • I know I can use lambda functions ion AS, so why not in ADT? – Ysch Aug 08 '15 at 20:15
  • 4
    @Ysch The Java runtime environments on Android (i.e. Dalvik or ART) are completely separate from J2EE and J2SE runtimes. Functionality from one is not necessarily present in the other. – dhke Aug 08 '15 at 21:06
  • Stop Using Eclipse. Use Android Studio. – Jared Burrows Aug 08 '15 at 21:22
  • @JaredBurrows Android Studio is going to give him Lambda features? How does that work? – Basil Bourque Aug 09 '15 at 15:19
  • @BasilBourque I never said that, this question needs to be closed. I looked at the fact he was using eclipse. Eclipse is no longer supported. – Jared Burrows Aug 09 '15 at 15:48
  • @JaredBurrows I'd love to use AS, but as I wrote in my question, I have several restrictions that demands I use Eclipse. I know it's no longer supported, and that's exactly the reason I'm asking here and not in an official code.google forum. – Ysch Aug 09 '15 at 16:37
  • This Question appears to be not really about which JDK to use for Android, nor which IDE (Eclipse vs Android Studio) to use. It seems to be specifically about **integrating code with Lambda syntax**. Accordingly, I edited the title of the Question. Aaron He posted [an Answer](http://stackoverflow.com/a/31898800/642706) with a solution addressing that specific issue (a back-port of Lambda syntax). So I see no reason to consider this a duplicate, nor any reason to close this Question. – Basil Bourque Aug 09 '15 at 17:59
  • would you tell me what is the usage of lambda expression in andrid and how it can help us in coding with some example? – Mahdi Jun 26 '16 at 07:19

6 Answers6

11

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.

Aaron He
  • 5,509
  • 3
  • 34
  • 44
6

You cannot currently (as up to at least Android 5.1.1) use lambda functions on Android.

Lambda functions require new Dalvik (not necessarily JVM!) opcodes (liberate-variable, box-lambda, unbox-lambda, capture-variable, create-lambda, invoke-lambda) that neither Dalvik nor ART currently have support for.

It looks like google might have scheduled (though nothing seems to be official yet) Java 8 support for post 5.1.1 (API Level 23 and later). At least the smali disassembler already added support with a distinct reference to the API level:

https://github.com/JesusFreke/smali/commit/144951a9e9e6c87866245f2bdeebf0ebedaa0e38:

Add new -X/--experimental flag to [dis]assemble opcodes not in art yet

  • Add new opcodes liberate-variable, box-lambda, unbox-lambda, capture-variable, create-lambda, invoke-lambda
  • Add support for encoding 25x instructions
  • Adds LambdaTest to check new opcodes assemble/disassemble properly

And also

https://github.com/JesusFreke/smali/commit/144951a9e9e6c87866245f2bdeebf0ebedaa0e38#diff-5d7892344c0b747d3667bf8623c690c5R66

options.apiLevel = 23;  // since we need at least level 23 for lambda opcodes

This only marks the opcodes, not the necessary library changes. It also does not tell us anything about Android itself, so I'd suggest not to take this as an official release schedule.

dhke
  • 15,008
  • 2
  • 39
  • 56
  • 1
    The Java 8 implementation for lambdas did not introduce a single new "opcode". Instead, it builds upon the invokedynamic instruction which is a Java 7 feature (yes - that doesn't exist on Android). In fact, Java 8 lambda bytecode could run on a Java 7 VM if only the required API classes would exist there. – Stefan Zobel Feb 16 '16 at 19:56
3

Android does support Java 8, so no only is this not a duplicate

As of Android N preview release Android support limited features of Java 8 see Java 8 Language Features

To start using these features, you need to download and set up Android Studio 2.1 and the Android N Preview SDK, which includes the required Jack toolchain and updated Android Plugin for Gradle. If you haven't yet installed the Android N Preview SDK, see Set Up to Develop for Android N.

Supported Java 8 Language Features and APIs

Android does not currently support all Java 8 language features. However, the following features are now available when developing apps targeting the Android N Preview:

Default and static interface methods

Lambda expressions

Repeatable annotations

There are some additional Java 8 features which Android support, you can see complete detail from Java 8 Language Features

  • Thanks for the answer, but I already know hiw to enable Lambdas in AS, the Q was enabling it specifically in ADT Eclipse. – Ysch Mar 10 '16 at 05:37
1

I don't think this is going to work.

In order to use lambdas, you need source compatibility level 1.8. In order for the DEX compiler to work you need target compatibility 1.7. Eclipse is not going to let you set the target compatibility below the source compatibility (picture below).

Note that this is unrelated to IntelliJ's habit of thinking it knows way better than you do, what your code should look like. It can show you a lambda, even when the actual code is an anonymous class.

enter image description here

G. Blake Meike
  • 6,615
  • 3
  • 24
  • 40
1

UPDATE: Since a few days, Android Studio 3.0 is out on stable. It officially supports a subset of Java 8 features, lambda expressions among them.

According to this Android Developers Blogpost from March 14th, 2017, google

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.

So we probably won't have to wait much longer for Java 8 in Android Studio.

greyhairredbear
  • 624
  • 3
  • 10
  • 27
0

Reverse The Lambda Syntax

As far as I know, everything done in the new Java 8 Lambda syntax can be done using old-school Java code such as anonymous inner class. (Oracel Tutorial) (Oracle Quick Start)

To ease the burden of undoing the Lambda syntax, some IDEs such as NetBeans can suggest automatic revision of code in either direction to/from Lambda syntax. With one-click of approval, the syntax is auto-magically swapped. See the NetBeans document on Using Lambda Expressions Support.

Here is a screenshot of NetBeans offering to turn a Vaadin button’s Button.ClickListener from Lambda syntax to an anonymous inner class. Notice the mouse pointer clicking on the light bulb icon on line # 107.

screenshot of NetBeans offering to switch Lambda syntax to an anonymous inner class

To enable this feature in your IDE, you will need to enable Java 8 in your project temporarily. After undoing all the Lambda syntax, switch your project back to Java 6/7. In NetBeans the way to enable Java 8 is in your project > Properties > Sources > Source/Binary Format (popup menu) > 1.8.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • I didn't know there were automatic tools to switch between lambda and anonymous methods. Sound's promising, I'll look into it! – Ysch Aug 09 '15 at 18:11
  • I do not know about project-wide editing, but possibly. Certainly NetBeans 8.0.2 does suggest switching to/from Lambda syntax in the class code editor. I use that feature daily. – Basil Bourque Aug 09 '15 at 18:14