Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use lambda expression in android

I'm having this error when trying to use lambda expressions.

Error:

Error:(122, 42) error: lambda expressions are not supported in -source 1.7 (use -source 8 or higher to enable lambda expressions)

How can I solve it notice that my android studio is 3.0

like image 679
Ali Habbash Avatar asked Jul 05 '18 10:07

Ali Habbash


People also ask

What are lambda expressions in Android?

Lambda expression is a new and important feature of Java which was included in Java SE 8. It provides a clear and concise way to represent one method interface using an expression. It is very useful in collection library. It helps to iterate, filter and extract data from collection.

How do you use lambda expressions?

Using Lambda Expressions Lambda expressions can be stored in variables if the variable's type is an interface which has only one method. The lambda expression should have the same number of parameters and the same return type as that method.

Can be replaced with lambda in Android Studio?

If you want to replace all the lambda expression from the project you can try Ctrl + Shift+ Alt + I on inspection search tab, search anonymous type can be replaced with lambda. It will replace all the anonymous type to lambda.

What does V -> mean in Android Studio?

It is called lambda expressions, which is a feature of Java 8 and is not yet supported in Android. In your case, it is just the onClick 's View parameter. It is the android studio to compact anonymous inner classes.


1 Answers

Add the below code to your app level gradle:

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
like image 75
Mani Vasagam Avatar answered Oct 20 '22 18:10

Mani Vasagam