How to use lambda expression in android
Its because maybe you are using Java 7 and Actually, Retrolambda
is a library which we can use with Java 8 lambda expressions
.
So How To SetUp :-
Add this following to your project’s main build.gradle
classpath 'me.tatarka:gradle-retrolambda:3.2.3'
then add this to your application module’s build.gradle
apply plugin: 'me.tatarka.retrolambda'
then we need to add these lines to your application module’s build.gradle
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
and done, you can use it like below :-
eg.) Clicking a button with Retrolambda.
// RETROLAMBDA WAY
clickMeBtn.setOnClickListener(view ->
Toast.makeText(MainActivity.this,
"This is the way to click a button to make a toast with RetroLambda !", Toast.LENGTH_LONG).show());
Anonymous class new View.OnClickListener()
can be replaced with lambda
.
Add the below code to your app level gradle:
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.sanjay.sanjay"
minSdkVersion 15
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
add like this in you app.gradle file in android studio