Image for post
Image for post

Native support for java 8 Lambdas on Android

César Ferreira
Aug 22, 2016 · 2 min read

A few months ago I wrote a tutorial about a simple way to incorporate java8 lambda features on Android (which only supports java 6/7 for now).

Well guess what? Android Nougat introduces support for Java 8 language features that you can use when developing apps that target Android version 24 from minSdkVersion 9 and up. We don’t need retrolambda no more :)

Here’s how to enable this feature on your build.gradle:

You can now start writing shorter code…

Lambdas

So things like this:

Become this:

Method References

Sometimes a lambda expression does nothing but call an existing method. In those cases, it’s often clearer to refer to the existing method by name. Method references are compact, easy-to-read lambda expressions for methods that already have a name:

Can be called as a method reference:


Known issues

Currently there is no support for Instant run nor data binding.

Because Jack does not generate intermediate class files when compiling an app, tools that depend on these files do not currently work with Jack. Some examples of these tools are:

  • Lint detectors that operate on class files;
  • Tools and libraries that require the app’s class files (such as instrumentation tests with JaCoCo).

I’ll personally not ditch retrolambda until instant run is supported but as soon as it is I’ll jump into the bandwagon :)



Hit me with a Tweet if you have any questions

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store