How to use new Java 8 features in Android N Preview

How to use new Java 8 features in Android N Preview

The Android team released Android N Preview with a lot of improvements, including Java 8 support provided by the Jack compiler. In this post, we'll take a look at what it means for Android developers and how to try out the new language features.

Overview

In this article, it does not make much sense to introduce the new features of Oracle Java 8 - a lot of information is already available on the Internet.

On the other hand, the official Java 8 announcement for Android leaves a lot of open questions for developers, and it feels like not all native Java 8 features are available. The more detailed technical announcement confirms this. We can categorize these language features according to their availability in Android N as follows:

Android Gingebread (API 9) and above:

  • Lambda Expressions

  • java.util.function

Android N and above:

  • Default and static interface methods

  • Repeatable annotations

  • Streams

  • Reflection APIs

So the correlation between Java 8 features and the used minSdkVersion has to be carefully chosen by the developer. We must also note that language backward compatibility is provided by the Jack compiler. Conceptually, the Jack compiler combines the functionality of javac, ProGuard, and dex into a single transformation step. This means that no intermediate Java bytecode is available, and tools like JaCoCo and Mockito will not work, as does DexGuard (the enterprise version of ProGuard). Let's hope that this is just an early preview version and that these issues will be fixed in the future.

Lambda expressions and related functional APIs - this is something every Android developer will love. This kind of feature will be extremely useful to increase the readability of the code - it replaces anonymous inner classes that provide event listeners. Previously, it could only be achieved through additional tools or by Android Studio editor to fold the code.

Default and static interface methods can help us reduce the number of additional utility classes, but are clearly not the most needed features. There are some other new features that I would like to talk about in more detail, so they are beyond the scope of this article.

The most interesting thing for me — Java 8 Streams — is not available in the current preview. We can see that it was actually just merged into AOSP sources, so expect to see it in the next N Preview or Beta release. If you really can’t wait to explore — try to use Lightweight-Stream-API, which is currently open source and backwards compatible.

Summarize

In this post, we looked at the use cases for Java 8 and its current implementation in the Android N Preview SDK. We also looked at the current limitations of the Jack compiler and how they might be fixed before the latest release.

<<:  Android 8.1 is coming: new features make users applaud

>>:  Cooling down high concurrency: Meituan’s high-performance, high-reliability four-layer load balancing MGW optimization practice

Recommend

Are east, west, south and north fixed?

The four basic directions of east, west, south an...

If you are depressed, why can't you take antidepressants?

This is the 3722nd article of Da Yi Xiao Hu Aunt ...

“Immune gap” VS “immune deficiency”: Who is more reasonable?

The theory that the new coronavirus destroys the ...