Using lambda expressions on Android-RETROLAMBDA plug-ins

Source: Internet
Author: User

Update: The addition of the Android Jack Compiler tool allows us to use lambda expressions on the old platform, the most important of which is the official support Oh, the specific content of this article: "Use the official lambda support on Android-Android N & Jack Tools (old platform compatible)

One of the major changes in Java8 is the addition of lambda expressions, a compact, way of passing behavior. It can make your code more concise and logically clearer. Especially when using Rxjava, the various data transformations are simplified using lambda expressions, which minimizes boilerplate code and makes the processing logic of the entire data flow very clear (there is an example below).

What is Retrolambda again? It is an unofficial compatibility scheme for JAVA8 Lambda expressions in Java 5/6/7. Since all versions of Android (except N Preview) are currently available, JAVA8 is not supported.

Search Retrolambda on GitHub, the top two most stars are our goal. Evant/gradle-retrolambda and Orfjackal/retrolambda, respectively. What's the relationship between them? Simply put, Gradle-retrolambda is just a gradle plugin for AS, and he also relies on a second open source library Orfjackal/retrolambda. So here we directly choose the first to configure.

    • Step1
      Download jdk8 (can coexist with jdk7)
    • Step2
      Modify the system environment variables, set up JAVA7_HOME and JAVA8_HOME (in order to facilitate the configuration of the following plugins)
    • Step3
      To modify the Build.gradle file, add the following build script (see: README):
‘me.tatarka.retrolambda‘buildscript {  repositories {     mavenCentral()  }  dependencies {     ‘me.tatarka:gradle-retrolambda:3.2.5‘  }}retrolambda {    jdk System.getenv("JAVA8_HOME")    oldJdk System.getenv("JAVA7_HOME")    javaVersion JavaVersion.VERSION_1_7}android {    ...    compileOptions {        sourceCompatibility JavaVersion.VERSION_1_8        targetCompatibility JavaVersion.VERSION_1_8    }}

(Note: If the Gradle plugins version you are using is 2.0.0, you should use the latest 3.3.0-BETA4 Gradle-retrolambda plugin.) )

    • Step4
      The project defaults to the v2.1.0 version of the Retrolambda library, and you can manually change this dependency (as modified to the latest version 2.3.0, if you do not want to modify it you can skip this step directly):
dependencies {  // Latest version 2.3.0  ‘net.orfjackal.retrolambda:retrolambda:2.3.0‘  // Or a local version  // retrolambdaConfig files(‘libs/retrolambda.jar‘)}
    • Step5
      You can start writing lambda expressions by performing gradle Sync Project, waiting for as to download the relevant plugins and dependent libraries. Here is a simple example of how to add a lambda expression before and after the comparison, you feel at random:

No lambda expression is used:

Observable.from (Arrays.aslist (1,2,3,4,5,6,7,8,9,Ten). Filter (NewFunc1<integer, boolean> () {@Override         PublicBooleanPager(Integer integer) {returnInteger%2==0; }}). map (NewFunc1<integer, integer> () {@Override         PublicIntegerPager(Integer integer) {returnInteger * integer; }}). Subscribeon (Schedulers.io ()). Observeon (Androidschedulers.mainthread ()). Subscribe (NewAction1<integer> () {@Override         Public void Pager(Integer integer)        {System.out.println (integer); }    });

After adding a lambda expression:

Observable.from(Arrays.asList(12345678910))    20)    .map(integer -> integer * integer)    .subscribeOn(Schedulers.io())    .observeOn(AndroidSchedulers.mainThread())    .subscribe(integer -> System.out.println(integer));

Does it feel like the code is fresher, boilerplate code is less, and the entire data processing logic is clearer? Lambda expressions do not have much syntax, less learning costs, and children's shoes that have not been tried can be imported into the project to play tricks.

Using lambda expressions on Android-RETROLAMBDA plug-ins

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.