Lamda Expressions in JAVA 8 and Android Studio

Vedprakash Rout
2 min readJun 7, 2016

--

JAVA is cool,but when it comes to callbacks via interfaces the boilerplate code seems way too much which kind of irritates all programmers. From now on starting with JAVA 8 we can solve this problem with using Lamda expressions. Though the title suggest its for both JAVA 8 and Android, but I will primarily write about using Lamda expressions in Android Development with Android Studio as that kind of covers both. So lets get started.

Benefits:

Before jumping into how to implement this let’s just look how much Lamda expressions can help in reducing boiler plate code:

Before

view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setVisibility(View.GONE);
}
});

After

view.setOnClickListener(v -> v.setVisibility(View.GONE));

Cool,Right!!

So, there are two ways to add lamda expression support in Android Studio. RetroLambda and Using JackCompiler

JackCompiler:

To use JAVA 8 features on your andorid project add this to your build.gradle file of your module .

android {
...
defaultConfig {
...
jackOptions {
enabled true
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

RetroLambda:

Add me.tatarka:gradle-retrolamba plugin to your application level build.gradle file as dependency.

buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.4.0'
classpath 'me.tatarka:gradle-retrolambda:3.2.5'
}
}

In your app module build.gradle file add this.

apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'

android {
compileSdkVersion 22
buildToolsVersion "23.0.0 rc2"

defaultConfig {
applicationId "com.demo.retrolambda"
minSdkVersion 22
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

}



dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}

Done. Now you can have java 8 features along with Lamda expressions on your project.

Now to help you with deciding which one you should use here is an comparison of number of methods from Jake Wharton’s talk which clearly says Jack Compiler is your best friend if you are using build tool version 24.0.1.

Jake Wharton: Exploring Java’s Hidden Costs https://www.youtube.com/watch?v=WALV33rWye4

Issue with Lamda expression.
1. Can’t be used with interfaces that have multiple methods with same method signatures.

Some Samples

n -> n % 2 != 0;
(char c) -> c == 'y';
(x, y) -> x + y;
(int a, int b) -> a * a + b * b;
() -> 42
() -> { return 3.14 };
(String s) -> { System.out.println(s); };
() -> { System.out.println("Hello World!"); };

Check out this link for more:

--

--

Vedprakash Rout

Start up enthusiast. Android developer. Believe in building awesome products.