Saturday, 3 September 2016

Using Java 8 Lambda expressions in Android



Lambda expressions are one of the most important features added to Java 8. Lambda expressions simplifies programming. Currently there is no direct way to use Lambda expressions in Android. So we need to use Gradle Retrolambda plugin in order to use Lambda expressions. In this tutorial we are going to do that.

Create a new project in Android Studio with package name of your choice.

Adding Dependencies in build.gradle

We need to modify both the build.gradle files. Open the Project’s build.gradle file (outer build.gradle).
Add the following dependency for the Gradle Retrolambda plugin.
classpath 'me.tatarka:gradle-retrolambda:3.2.5'
The complete build.gradle is given as,
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.0.0-beta5'
        classpath 'me.tatarka:gradle-retrolambda:3.2.5'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
allprojects {
    repositories {
        jcenter()
    }
}
task clean(type: Delete) {
    delete rootProject.buildDir
}
The next is to modify the App’s build.gradle file (inner build.gradle). The me.tatarka.retrolambda plugin should be applied.
A new compileOptions block should be added then sourceCompatibility and targetCompatibility Java version should be set as 1.8.
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
The Java 8 JDK path should be set in retrolambda block.
retrolambda {
    jdk '/usr/lib/jvm/java-8-openjdk-amd64'
}
Here /usr/lib/jvm/java-8-openjdk-amd64 is my Java JDK path. Replace with your own.The complete build.gradle is given as,
apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'
android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"
    defaultConfig {
        applicationId "com.learn2crack.androidlambda"
        minSdkVersion 21
        targetSdkVersion 23
        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
    }
    retrolambda {
        jdk '/usr/lib/jvm/java-8-openjdk-amd64'
    }
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
}
Now again sync the project. Now all set. You can use lambda expressions in your project.
Here I am using OnClickListener with Button. The code is as simple as,
Button button = (Button) findViewById(R.id.btn);
button.setOnClickListener(e-> Log.d("Lambda","Hello"));

Note: Now Google is in the process of shifting from Apache Harmony Java implementation to OpenJDK implementation. So these features will be supported in future verions of Android, Mostly Android N.

Source: learn2crack.com

No comments:

Post a Comment