Retrolambda in Android



Hi, folks, it's been a long time since we have discussed with new changes happening in the Android world. Today I would like to convey about a new feature which is used in Java 8  called Lambda functions. It's very interesting that Java is improving its coding structure with new updations. Hence Java 8 was a previous year release and currently, they have updated with Java 9 we are eagerly updating our technological deeds soon. In this blog post, I would like to give some detail about Lambda functions which was introduced in Java 8. Lambda function helps in making the declaration and instance creation much more simple and faster execution process. It also helps in reducing the line of codes which lead a developer much easier and approachable access to the specific program he wrote. As we know from the latest Android Studio updates IntelliJ is automatically handling the methods and instance creation in converting to the lambda expression. But some more methods also can be handled by a programmer through Lambda expressions. Hence Android is currently supporting Java 7 which leads these new updates to make some difficulties in implementing in our Android project. Here I will suggest a method and a library which helps to pass Lambda expression in Android from my findings. The below link is a library called Retrolambda which can be used in Android project. https://plugins.gradle.org/plugin/me.tatarka.retrolambda. It gives a well-described method how to use it. Anyhow I will just pass the initial sample method here which can help a bit for beginners in using Lambda expressions in Android.
Retrolambda is a library which allows using Java 8 lambda expressions, method references and try-with-resources statements on Java 7, 6 or 5.
The Gradle Retrolambda Plug-in allows integrating Retrolambda into a Gradle-based build.
Adding Retrolambda to Gradle Build Configuration.
buildscript {
  repositories {
    jcenter()
  }
  dependencies {
   classpath 'com.android.tools.build:gradle:1.4.0'
   classpath 'me.tatarka:gradle-retrolambda:3.2.0'
   // NOTE: Do not place your application dependencies
 here; they belong
    // in the individual module build.gradle files
    }
}
Add the source and target compatibility to Java 8 and apply the new plug-in in yourapp/build.gradle file.
apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'

android {
    compileSdkVersion 22
    buildToolsVersion "23.0.0 rc2"

    defaultConfig {
     applicationId "com.sample.android.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'])
}
Using the lambda configuration by using it with your button to show a Toast once the button is clicked.
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  Button view = (Button) findViewById(R.id.button);
  view.setOnClickListener(e-> Toast.makeText(this,
  "Hello World", Toast.LENGTH_LONG).show());
    }
}

Comments

Post a Comment

Popular posts from this blog