Tired of implementing those anonymous interfaces? Annotating those functions with @Override all the time? Well you need to use lambdas. Lambdas were included in JAVA 8. Unfortunately you cannot use Lambdas directly in Android Studio by just changing you JAVA version, but don’t worry a library named ‘Retrolamda’ provides you that functionality. So first lets see how to include this library in our project and then we will look what these lambdas really are.


In your project level gradle file add the following line in the dependencies section :

Your gradle file should look something like this(What we are doing here is actually adding the Retrolambda plugin).

All good, now in the module level gradle file we have to apply this plugin, add the following line in the file(Add this line at the top of the file).

Now we have to enable JAVA 8 support for our project, copy and paste the following lines in the android section of the module level gradle file.

So now your module level gradle file should look something like this.

Now you are ready to use lambdas in your code. Hurray!


So what is a lambda, it is nothing but a anonymous function. Let us take a look at some code for better understanding.

Suppose I have an interface called ClickMeInterface that has one function named click which takes a single string argument.

Suppose there is a function that takes an object of ClickMeInterface as a parameter

Usually you would implement an anonymous class and pass it as a parameter in the function, just like below.

With lambda this can be simplified to something like this.

Simple and concise. So the format for this is straightforward, arguments of the function go before the ‘->’ symbol and the body goes after the symbol.

Suppose you are writing OnClickListener for a button. So something like this

will get converted to this

If you have multiple arguments in the function then all you have to do is separate them by commas.

A small catch!

You can only use lambdas with ‘functional interfaces’, functional interfaces are the ones that define a single functionality, in simple terms the interfaces that only have one function are known as functional interfaces. So if an interface has more that one function then you cannot use lambda for them.

References and further reading

Functional Interfaces In Java

Retrolambda GitHub repo

Lambda in JAVA 8

Gurleen Sethi

Android Developer and enthusiast, believes in developing products and content to help people. Likes to do deep thinking.


Leave a Reply

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

10 + eighteen =