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

mButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // do something here
    }
});

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.

mButton.setOnClickListener((View v) -> {
    // do something here
});

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.

 

 

 

 

 

 

 

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.3'

        classpath 'me.tatarka:gradle-retrolambda:3.4.0'
    }
}
allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

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.

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

android {
    compileSdkVersion 24
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "net.androidsrc.rxjava"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    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'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.1'
    testCompile 'junit:junit:4.12'
    compile 'com.jakewharton:butterknife:8.4.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'

    //RxJava
    compile 'io.reactivex:rxandroid:1.2.1'
    compile 'io.reactivex:rxjava:1.1.6'
}

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.

GuRu

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...

5 Responses

  1. January 2, 2017

    […] Adding lambdas support to Android Studio […]

  2. January 3, 2017

    […] First of all you have to add support for RxJava in your gradle file as explained in this tutorial. We are using lambda expression in this tutorial for code clarity, so you should setup that also from our tutorial. […]

  3. January 22, 2017

    […] First of all we have to setup lambda expression support on out android studio as right now java8 is experimental on Android studio. You can simply add this support by using retrolambda as shown in this tutorial. […]

  4. January 23, 2017

    […] with each other. We will be using lambda expressions to reduce our boilerplate code. Have a look at this tutorial for setting up RetroLambda in your […]

  5. February 4, 2018

    […] are using lambda expression in this tutorial for code clarity, so you should setup that also from our tutorial. Also need to add RxBindings to app/build.gradle […]

Leave a Reply

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