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.