Accessing to the Java 8 language features with Android N

Google has unveiled developer preview of Android N last month with a new exciting developer program. Official release is planned for Q3 2016. Amongst the new features of Android N, the support of several Java 8 language features is a great addition for developers.

Image for post
Image for post

Supported features include :

  • Lambda expressions
  • Default and static interface methods
  • Repeatable annotations
  • Method References

Additionally, some new APIs introduced with Java 8 are available like reflection and language-related APIs and Utility APIs (java.util.function and

Set up your development environment

To support Android N new features, you need to set up correctly your development environment. You need to get last version of Android N developer preview and to download Android Studio 2.1. You can make these installations via the Android SDK Manager on your computer. To get more details about the installation process, don’t hesitate to read the official documentation from Android Team :

Enable support for Java 8

In order to use the new Java 8 language features, you need to use the new Jack toolchain. Jack, for Java Android Compiler Kit, replaces javac compiler. It compiles Java language source code into Android-readable dex bytecode with its own .jack library format. Furthermore, it provides other great toolchain features inside a single tool like repackaging, shrinking, obfuscation and multidex. Before Jack, you needed to use ProGuard to achieve these tasks for example.

To enable support for Java 8 in your Android project, you need to configure your build.gradle file like that :

Enjoy your first Lambda Expression on Android

Now, you can enjoy your first Lambda expression on Android. Basically, a Lambda expression is a block of code that can be passed around to be executed later. In fact, it is very similar to anonymous classes. However, Lambda expressions are succinct with less verbose code. Let’s use a Lambda to set a click listener on a Button :

You can make the same with an item click listener on a ListView :

It works great with a Runnable interface too :

Clearly, you can see the code is shorter and more readable with Lambdas expressions. It will be great for the productivity of Android developers.

Create your first Functional Interface on Android

To create our first Functional on Android, let’s take classic example of a Calculator with an addition feature :

Now, you can add a default method to the Calculator interface that will be used to display a result for example :

Finally, we can create a Lambda expression with that interface :

Like you can see with those simple examples, Java 8 language features will offer a new way to Android developers to improve their productivity. Thanks to the developer preview of Android N, you can try these features right now.

Entrepreneur / Developer / Blogger / Author. In Bitcoin We Trust:

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store