Java 8 Lambdas on Android – Part 1

post
By kioko on January 30th In Android, Tutorials
Android-Lambda

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

1-4QWcaiW5fMkOjH26Q9cWBQ

 

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

One thought on “Java 8 Lambdas on Android – Part 1

  • lavet on February 25th, 2017

    Nice!!

Leave a reply

Recent Posts

Recent Comments

Archives

Categories