Adding lambda expressions Support to Android Studio using RetroLambda

Hello guys, In this tutorial we will adding lambda expressions support in our android studio project using RetroLambda. After that we can use simple syntax of lambda express to reduce our boilerplate code.

Retrolambda lets you run Java 8 code with lambda expressions, method references and try-with-resources statements on Java 7, 6 or 5. It does this by transforming your Java 8 compiled bytecode so that it can run on an older Java runtime. After the transformation they are just a bunch of normal .class files, without any additional runtime dependencies.

Why to we need lambdas ?

Lambdas will make like of programmer very easy by removing writing of boilerplate code. Lets take a example.

In the above example we are creating a new anonymous class which is based on OnClickListener interface defined in Android framework. Using lambdas this can be reduced to following code.

If there is just one statement inside the method then the braces can be omitted. Also, if you refer to some variables outside the functional method – no need to mark them as final anymore.

Adding Lambda support to Android

Lambdas support can be added to Android using gradle plugin. For this you need to add some plugins to gradle along some configurations.

android project structure

android project structure

Here in project view which can be selected on clicking on top bar of project layout. We have to add some dependencies in root build.gradle file.

We have to classpath dependency here for retrolambda as highlighted in code below.








This is not all. We also have to apply plugin to module level build.gradle file and also have to apply some configuration also. Make highlighted changes to build.gradle file.

That’s all we need to add lambdas support to android studio. You can now use lambda expressions in your code and enjoy.

Please comment for more clarification.


Technology enthusiast. Loves to tinker with things. Always trying to create something wonderful using technology. Loves coding for Android, Raspberry pi, Arduino , Opencv and much more.

You may also like...

5 Responses

  1. January 2, 2017

    […] Adding lambdas support to Android Studio […]

  2. January 3, 2017

    […] First of all you have to add support for RxJava in your gradle file as explained in this tutorial. We are using lambda expression in this tutorial for code clarity, so you should setup that also from our tutorial. […]

  3. January 22, 2017

    […] First of all we have to setup lambda expression support on out android studio as right now java8 is experimental on Android studio. You can simply add this support by using retrolambda as shown in this tutorial. […]

  4. January 23, 2017

    […] with each other. We will be using lambda expressions to reduce our boilerplate code. Have a look at this tutorial for setting up RetroLambda in your […]

  5. February 4, 2018

    […] are using lambda expression in this tutorial for code clarity, so you should setup that also from our tutorial. Also need to add RxBindings to app/build.gradle […]

Leave a Reply

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