Android Learning Explore Java 8 application in Android development

Source: Internet
Author: User

Objective:

Java 8 has been introduced for nearly 2 years, introducing many revolutionary changes, incorporating functional programming features, making behavior-based programming possible, and reducing the implementation of various design patterns, the most important update in Java history. But on Android, I haven't seen any messages that support JAVA8. Does Android really support Java8? The answer is yes, Android N has started to support Java 8.

About Java 8 (1.) Using lambda expressions

One of the highlights of Java 8 is the introduction of lambda expressions, which are more concise with the code they are designed for. When a developer writes a lambda expression, it is also compiled into a functional interface.

(2.) Introducing a functional interface

A core concept introduced in Java 8 is the functional interface. If an interface defines a unique abstract method, then this interface becomes a functional interface.

(3.) Generic interface improvements

This is an effort to improve the compiler's ability to determine generics, which was not previously possible. In previous versions of Java there were many scenarios in which the compiler could not calculate generics for a method, and when the method was in a nested or inline method call context, even though it sometimes seemed "obvious" to the programmer.

(4.) Streams programming

The improvement of the collection (collections) is also a highlight of Java 8, and the core component that makes the collection more and more good is "Stream". It is a completely different concept from the InputStream and OutputStream in the java.io package, and it is a whole new concept that we should not confuse. In addition, the advent of stream does not replace arraylists or other collections, it provides an operational big data interface that makes data operations easier and faster.

Explanation: However, the above changes can only be used in Android N and later versions, which makes Android 23 and below still use Java 7 development of small partners, but the Java 8 lambda expression can be supported, Next we look at how to use Java 8. How do I use Java 8 in Android development? 1.) The first step, install JAVA8, and specify that the project uses Java8,minsdkversion <24 can only use the lambda attribute of Java8, minsdkversion>= 24 can use the JAVA8 feature, This shows that all popular Java8 still need a certain amount of time

2.) The second step is to add the relevant configuration in the module's Build.gradle
Android {  ...  Defaultconfig {    ...    jackoptions {      enabled True    }  }  compileoptions {    sourcecompatibility javaversion.version_1_8    targetcompatibility javaversion.version_1_8  }}

I am using Android Studio 2.2.2 version of Android version 24, if it is below the 24 version, it is not possible to use Java 8 features. If you just want to use lambda expressions, we just need to set the minsdkversion to 14.

Here are some examples of my own setup

Android {    compilesdkversion    buildtoolsversion ' 24.0.2 '    defaultconfig {        ApplicationID ' Com.whoislcj.jsbridge "        minsdkversion        targetsdkversion        versioncode 1        versionname" 1.0        " jackoptions {            enabled True        }    }    buildtypes {        release {            minifyenabled false            Proguardfiles getdefaultproguardfile (' proguard-android.txt '), ' Proguard-rules.pro '        }    }    dexoptions {        incremental true    }    compileoptions {        sourcecompatibility javaversion.version_1_8        Targetcompatibility javaversion.version_1_8    }    productflavors {    }}
About Lambda

Lambda expression is an anonymous function, and lambda expression is named after the lambda calculus in mathematics, directly corresponding to it (lambda abstraction), an anonymous function, That is, a function without a function name. A lambda expression can represent closures (notice differs from the traditional mathematical sense).

How do I use a Java 8 lambda expression? 1.) Using the lambda expression example

Here is an example of a common Java anonymous inner class

(1.) Before lambda expression is used

1     Findviewbyid (r.id.to_web). Setonclicklistener (New View.onclicklistener () {2             @Override3 public             Void OnClick (View v) {4                 toast.maketext (Mainactivity.this, "Finally wait for You", Toast.length_long). Show (); 5             }6         });

(2.) using a lambda expression

1      Findviewbyid (r.id.to_web). Setonclicklistener (View v), {2             toast.maketext (this, "Finally wait for You", toast.length _long). Show (); 3         });

(3.) further simplifying

1 Findviewbyid (R.id.to_web). Setonclicklistener (V-toast.maketext (this, "Finally wait for You", Toast.length_long). Show ());
2.) Expand your knowledge

If you want to implement lambda expressions in Android Studio version 2.1, you can implement a third-party as Retrolambda plugin, github address: https://github.com/evant/ GRADLE-RETROLAMBDA, the same approach supports only lambda expressions and does not support the JAVA8 feature.

The problems that are currently met

1.) Chinese garbled problem

This question I always thought is the coding problem, later discovered is jackoptions {enabled true} caused by the Chinese garbled, the solution in Project's gradle.properties add the following code

Org.gradle.jvmargs=-dfile.encoding=utf-8

2.) Instant Run is currently not available for Jack and it is currently recommended to temporarily turn off Instant run.

Summarize:

This article is just about learning how to use lambda expressions that use Java8 in Android development to make your code concise, and to gradually learn some of the features of Java.

Android Learning Explore Java 8 application in Android development

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.