Use the lambda expression of Java 8 in Android, androidlambda

Source: Internet
Author: User

Use the lambda expression of Java 8 in Android, androidlambda
Lambda expressions translated from objective 8, as a Java developer, may often cause many problems due to the lack of closures. Fortunately, Java's 8th version introduced lambda funced brings us good news. However, what is the use of this? On android, we can only use Java7. what is the current situation? Haha, we can also use android developer! Our old guy Esko Luontola found the lambda Method on android, which is really great! So let's see how it is implemented! Just as there was Weaver et al. for running Java 5 code with generics on Java 1.4. Lambda allows us to use lambda expressions in Java 8 or earlier versions. This is really great: in the Java 7 virtual machine, we can run the Code Compiled on 8. Now, we can use Android Studio and gradle to implement it. We have signed a contract with Lambda to become a marucheon bar (fog! Install JAVA 8 SDK. The premise is to use the Java 8 SDK. Here you can download: JAVA 8. Now set java 8 in android studio.

Mac:

File> Project structure. You need to set it as follows:

For more information, see settings.

Set up Gradle! It's almost better for you. The last step is to edit some configuration files of Gradle. We will use export lambda 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 the following in the same file:
buildscript {    repositories {        mavenCentral()    }    dependencies {        classpath 'me.tatarka:gradle-retrolambda:2.5.0'    }}repositories {    mavenCentral()}apply plugin: 'me.tatarka.retrolambda'

Then, OK. Try to build your project. It must be awesome ~

Note that lambda only allows you to use lambda expressions in Java 8. Other features won't work in android.

Related Article

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.