Since Java 8 release Java programmers can take advantages of lambda expression. However it can’t be applied directly to Android development.

There are two options to use lambda expression for Android development.

Using Jack tool-chain supports more features of Java 8 than using Retrolambda, but I recommend Retrolambda here. Because Jack has a few disadvantages.

  • Does not support Lint
  • Does not support Instant Run
  • Does not support full features for every API level


Let’s dive into lambda expression with Retrolambda.

First, add the following to your build.gradle

buildscript {
  repositories {
    mavenCentral()
  }

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

repositories {
  mavenCentral()
}

apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'

alternatively, new plugin syntax for gradle 2.1+ can be used

plugins {
  id "me.tatarka.retrolambda" version "3.5.0"
}

Click gradle sync button in AndroidStudio.

Now coding with lambda expression on android is available.

// Traditional
button.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    log("Clicked");
  }
});

// With lambda expression
button.setOnClickListener(v -> log("Clicked"));


// Traditional
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
  @Override
  public void run() {
    handlePushEvent(pushEvent);
  }
});

// With lambda expression
Handler handler = new Handler(Looper.getMainLooper());
handler.post(() -> handlePushEvent(pushEvent));

How simple and beautiful(?) it is!!

Not only lambda expression, but also method reference is available.

button.setOnClickListener(this::printHello);

private void printHello() {
  log("Hello");
}