Adding lambda expressions to Android 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...