Objective
New features of Lambda expression,java8. With lambda expression, you can replace an interface implementation that has only one function, leaving the anonymous inner class, and the code looks more concise and understandable.
JAVA8 has some other new features, but it may not work on Android.
The Jack compiler is supported after studio 2.x, using it to use java8 lambda expression, but other features are not guaranteed to work.
Note: Some of the JDK's source code is integrated into the Android SDK, and some of the native JDK classes may have added some implementations of the new features, but not in Android.
You can also use the plug-in RETROLAMBDA to support java8 lambda expression.
Jack Configuration
Add the following configuration
android { defaultConfig { 24//7.0 } jackOptions { true } compileOptions {//jack、retrolambda编译必需 sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 }}
To enable the Jack compiler to support lambda expression
RETROLAMBDA Configuration
Project Address: Https://github.com/evant/gradle-retrolambda
Retrolambda is created to be compatible with lambda expression for JAVA5, 6, and 7.
Configured as follows
{ repositories { jcenter() } { classpath ‘com.android.tools.build:gradle:2.2.3‘ classpath ‘me.tatarka:gradle-retrolambda:3.6.0‘ }‘me.tatarka.retrolambda‘{ defaultConfig { minSdkVersion 11 //这个没有硬性要求 } {//jack、retrolambda编译必需 sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 }}
Note: In order to be compatible with version 7.0 below, Jack mode is not applicable. So the general still chooses RETROLAMBDA
How LAMBDA expression is used
First of all, we need to know that the expression, as a whole, expresses an " object type ".
The simplest lambda expression
Code:
Runnable runnable = () -> System.out.println("hi, 我是 stone");runnable.run();
() is actually the method parameter list, there is no argument, then will go to match the method, because runnable only a void run (), so it will match to it, there is no method name, so the name of the method is ignored.
This is followed by the method body. There is only one line of printed code, you can omit the curly braces for the method body: {}.
Note: The code "new Runnable" is omitted here because the compiler makes automatic type inference. If you call ()---System.out.println ("Hi, I Am Stone"). Run (); So it is compiled, because the compiler does not know which class to look for in the corresponding method
lambda expression with parameters and a return value
Code:
button.setOnTouchListener((view, event)-> { if (event.getAction() == MotionEvent.ACTION_DOWN) { if (flag) { return true; } } return super.onTouchEvent(event);});
When parameters are passed, the name can be defined arbitrarily, and the type definition or undefined is possible; when undefined, the compiler automatically infers it.
When there is a return value, you can use return in the method body, or you can omit return when there is only one piece of code, as follows:
button.setOnTouchListener((v, e) -> super.onTouchEvent(e));
Lambda expression for Android using the new JAVA8 feature