Skip to content

Lambda Expressions

Roger Hu edited this page Jun 25, 2016 · 28 revisions

Overview

Java 8 lambda expressions help eliminate boilerplate code that makes the syntax verbose and less clear. For instance, even basic click listeners syntax can be simplified:

myButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.d("debug", "Button clicked");
                    }
                });

With event listeners that only have one method that need to be implemented, lambda expressions can be used to simplify the syntax:

myButton.setOnClickListener(v -> {
            Log.d("debug", "Button clicked");
});

Lambda expressions are especially helpful in RxJava as well. Take a look at the code below for creating an Observable and subscribing an Observer to it.

Creating and subscribing to an observable without lambdas:

Observable.just("Hello RxAndroid")
	.subscribe(new Action1<String>() {
		@Override
		public void call(String s) {
			Log.d("Emitted", s);
		}
	});

Consider the same code with lambda expressions:

Observable.just("Hello RxAndroid")
	.subscribe(s -> Log.d("Emitted", s));

Setup

Make sure you have JDK 8 installed or higher. Click here in case you need to download it. If you are using a continuous integration service, you should also make sure to be specifying a JDK 8 environment.

To use Java 8 lambda expressions in your Android code, you can either use the Gradle Retrolambda plugin developed by Evan Tatarka or use the new Android Jack toolchain.

Retrolambda Setup

  • Open the root build.gradle file and add the following dependency:
buildscript {
  dependencies {
    classpath 'me.tatarka:gradle-retrolambda:3.2.5'
  }
}
  • Modify the app module build.gradle file to apply the me.tatarka.retrolambda plugin:
apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'
  • Add a new compileOptions block, then sourceCompatibility and targetCompatibility Java version should be set as 1.8
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
  • If you have multiple Java versions installed, set the Java 8 JDK path in retrolambda block:
retrolambda {
    jdk '/path/to/java-8/jdk'
}

Limitations

  • Using the Android lint detector will trigger a java.lang.UnsupportedOperationException: Unknown ASTNode child: LambdaExpression. To get around this issue, you need to add this third-party package that replaces the parsing engine for Java to support lamda expressions:
buildscript {
  dependencies {
     classpath 'me.tatarka.retrolambda.projectlombok:lombok.ast:0.2.3.a2'
  }
}

Using Jack Toolchain

Android provided a way to use some Java 8 language features including lambda expressions in your Android project by enabling the Jack toolchain. To do this, edit your module level build.gradle file as follows:

android {
  ...
  defaultConfig {
    ...
    jackOptions {
      enabled true
    }
  }
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

Sync your gradle file, if you encounter any build error, you may need to download the latest Android SDK Build-tools from the SDK Manager.

Known issues with using the Jack toolchain

  • Android Studio Instant Run does not currently work with Jack and will be disabled while using the new toolchain.

  • Because Jack does not generate intermediate class files when compiling an app, tools that depend on these files for example, lint detectors, do not currently work with Jack.

  • Tools like android-apt which is required for using Dagger 2 in your Android project do not currently work with Jack.

Attribution

This guide was originally drafted by Adegeye Mayowa

Finding these guides helpful?

We need help from the broader community to improve these guides, add new topics and keep the topics up-to-date. See our contribution guidelines here and our topic issues list for great ways to help out.

Check these same guides through our standalone viewer for a better browsing experience and an improved search. Follow us on twitter @codepath for access to more useful Android development resources.

Clone this wiki locally