mscharhag, Programming and Stuff;

A blog about programming and software development topics, mostly focused on Java technologies including Java EE, Spring and Grails.

Thursday, 26 February, 2015

Using Java 8 Lambda expressions in Java 7 or older

I think nobody declines the usefulness of Lambda expressions, introduced by Java 8. However, many projects are stuck with Java 7 or even older versions. Upgrading can be time consuming and costly. If third party components are incompatible with Java 8 upgrading might not be possible at all.
Besides that, the whole Android platform is stuck on Java 6 and 7. Nevertheless, there is still hope for Lambda expressions!

Retrolambda provides a backport of Lambda expressions for Java 5, 6 and 7.

From the Retrolambda documentation: 

Retrolambda lets you run Java 8 code with lambda expressions and method references on Java 7 or lower. It does this by transforming your Java 8 compiled bytecode so that it can run on a Java 7 runtime. After the transformation they are just a bunch of normal .class files, without any additional runtime dependencies.

To get Retrolambda running, you can use the Maven or Gradle plugin.

If you want to use Lambda expressions on Android, you only have to add the following lines to your gradle build files:

<project>/build.gradle:

buildscript {
  dependencies {
    classpath 'me.tatarka:gradle-retrolambda:2.4.0'    
  }
}


<project>/app/build.gradle:

apply plugin: 'com.android.application'

// Apply retro lambda plugin after the Android plugin
apply plugin: 'retrolambda' 

android {
  compileOptions {
    // change compatibility to Java 8 to get Java 8 IDE support  
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

Comments

  • Lukas Eder - Monday, 2 March, 2015

    Writing Java 8 source compatible code and expecting it to run on a Java 7 runtime may not be the best idea in general. The amount of new JDK API is huge, you'll have ClassDefNotFoundError and NoSuchMethodError all over the place (in production). I suspect, you took additional steps to prevent that?

  • Michael Scharhag - Monday, 2 March, 2015

    Hi Lukas,
    you are right, using JDK 8 features to write code that runs on java 7 might not be the best idea. In IntelliJ you can use different values for SDK and language level. You can use JDK 7 with language Level 8. This allows you to use Lamdba expressions but doesn't give you access to Java 8 APIs like Streams, new date/time, etc.
    For Android it is a little bit different. Androidstudio only shows the classes which are available in the selected target Android version. So there is nothing to do here besides changing the Language Level to Java 8.

  • Sar Dubnotal - Friday, 6 March, 2015

    Just wanted to mention two additional resources:

    a) the ThreeTen Backport http://www.threeten.org/threetenbp/ of the Java 8 date and time API and

    b) the streamsupport library https://sourceforge.net/projects/streamsupport/ as a backport of the Java 8 stream API.

Leave a reply