Pages

getteammates.com

Saturday, 9 January 2016

Lambda support in Android with retro lambda

Software

Java 8

Java 8 is required for retro lambda to work. This is required since retro lambda transforming Java 8 compiled byte-code so that it can run on an older Java run-time.

Setup

Environment Variables

Set environment variable JAVA8_HOME to point to the Java 8 installation directory. Please make sure that $JAVA8_HOME/bin/javac is valid.
You can keep JAVA_HOME to point to your java SDK which is used to build the Android application.

Gradle Build

Add the following dependency in to your main build.gradle, its ok to add it to your apps build.gradle as well.
buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'me.tatarka:gradle-retrolambda:3.2.0'
    }
}

Add retro lambda plugin to your apps build.gradle as,
apply plugin: 'me.tatarka.retrolambda'

That's it, now you should be able to use lambda expression in your projects. That means now you should be able to set a View click listener as,
findViewById(R.id.changeColorButton).
    setOnClickListener((v) -> v.getRootView().setBackgroundColor(mColors[mRandom.nextInt(mColors.length)]));

For a simple example, please see https://github.com/trsquarelab/androidexamples/tree/master/HelloRetroLambda




No comments:

Post a Comment