Using a lambda expression in Java 8 in Android

Source: Internet
Author: User

From Http://www.coshx.com/blog/2015/03/23/use-java-8s-lambda-functions-in-android/using Java in Android 8 lambda Expressions As a Java developer, you may often have a lot of trouble with the closeness of your information. Fortunately: Java's 8th version introduced lambda functions brings us good news; however, there are some eggs, on Android, we can still use only Java7. So what's the situation now? Haha, we can use Android Developer! Our old buddy Esko Luontola found a way to use lambda on Android, which is awesome! So, let's see how it's going to come true! Just as there is Retroweaver et al. for running Java 5 Code with generics on Java 1.4. RETROLAMBDA allows us to use lambda expressions on Java 8 or lower. This is great: in Java 7 virtual machines, we are able to run code compiled on 8. Now, we use Android Studio and Gradle to achieve it, and RETROLAMBDA signed a contract to become a horse monkey burning Bar (fog!). Install the Java 8 SDK, as long as you want to use the Java 8 SDK, here to download: Java 8 OK, now in Android studio to set up the use of Java 8 bar.

Mac:

File > Project structure. You need to set this up:

Other settings can be referenced.

Set up gradle you're awesome! You're almost ready. The final step is to edit some of the Gradle's configuration files. We will use Retrolambda plugin from Evan Tatarka to support lambda expressions. First, open the Build.gradle file in the app module and change the configuration under the Compileoption node:
android {    }    compileOptions {        sourceCompatibility JavaVersion.VERSION_1_8        targetCompatibility JavaVersion.VERSION_1_8    }}
Second, add in the same file:
buildscript {    repositories {        mavenCentral()    }    dependencies {        classpath ‘me.tatarka:gradle-retrolambda:2.5.0‘    }}repositories {    mavenCentral()}apply plugin: ‘me.tatarka.retrolambda‘

Then just OK, try to build your project, it must be awesome ~

It is important to note that RETROLAMBDA only allows you to use lambda expressions in Java 8, and that other features do not work in Android.

Using a lambda expression in Java 8 in Android

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.