Lambda expressions are a functional derivation language that is supported in JDK 8, and can massively reduce the redundant code of anonymous internal classes. In Android, you can use a lot of scenarios such as setting up listening, setting up asynchronous callbacks, and so on.
Android Development is now migrating from Eclipse's ADT to Android studio, but the current version of Android Studio does not support lambda expressions directly, requires plug-in support, and, of course, JDK version must use JDK 8 Or more (of course there will be a later version of the JDK).
1. Introduction of the Retrolambda plugin:
Add the Build.gradle in Project
Apply plugin: ' Me.tatarka.retrolambda '
2. Set compilation options (may also not be written)
Add the following code to the Build.gradle Android node of project
compileoptions { sourcecompatibility javaversion.version_1_8 targetcompatibility JavaVersion.VERSION_1_8 }
3. Introduce the RETROLAMBDA classpath and add the following code to the Buildscript->dependencies node in the Module:app Build.gradle
Classpath ' me.tatarka:gradle-retrolambda:3.2.0 '
4. Build the Build.gradle
5. Write the test code, the following simple wording
protected void OnCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); TextView = (TextView) Findviewbyid (r.id.text); Textview.setonclicklistener (V-toast.maketext (Getapplicationcontext (), "Lambda", Toast.length_long). Show ());
6. Run to see if it is normal
Using lambda expressions in Android Studio