Lambdas on Android – Part 1

This is a four part series of how to setup and user Lambda Expressions in Android. In Part 2, we’ll use Lambda expressions with RxJava.

If you are tired of writing boilerplate code, then you should try out Java 8. Let’s write some code.


Enabling Lambda Feature on Gradle

We need to edit your build.gradle file


Implementing Lambdas

We can now write shorter code. In this example we’ll implement OnClickListener on a button to demonstrate this. The old way of doing this looks like this:Now with Lambdas.  

Method Reference

Lambda expressions contain double colons :: . These refer to a new syntax in Java 8 known as method references. You can reference a class or instance and pass along the method that will handle the event:

It’s that simple. Source code is available on GitHub.


Known issues with using the Jack toolchain

  • Instant Run does not currently work with Jack and will be disabled while using the new toolchain.
  • There’s no support for Data Binding
  • Annotation support for libraries such as Dagger 2 to show you in your Android project may not work with Jack. The most recent experimental releases are starting to add this support.

The work-around for this is using Retrolambda instead.

That’s it for now



In Part 2, we’ll use Lambda expressions with RxJava. Happy coding.


  1. lavet February 25, 2017 at 3:34 PM


  2. Marco March 8, 2018 at 1:07 AM

    Hey! I just stumbled with your page! it’s awesome! I’ve been using this in methods like : .setAction(R.string.retry, v -> fetchPayments()) and I had no idea why it just WORKS, I decided to look it up and stumbled upon your page, will be reading all your articles, thanks man!

Leave a comment

Your email address will not be published. Required fields are marked *