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.
1 2 3 4 5 6 |
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.
1 2 3 |
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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
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.