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.

java8_android

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 java.util.stream).

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 :

http://developer.android.com/preview/setup-sdk.html

 

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 :


android {
  ...
  defaultConfig {
  ...
    jackOptions {
      enabled true
    }
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

 

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 :


button.setOnClickListener(view -> Log.d(TAG, "Button Clicked."));

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


listView.setOnItemClickListener((parent, view, position, id) -> {
  // …
}

It works great with a Runnable interface too :


Runnable runnable = () -> Log.d(TAG, "Log from a Runnable with a Lambda");
newThread(runnable).start();

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 :


@FunctionalInterface
public interface Calculator {
  int calculate(int a, int b);
}

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


@FunctionalInterface
public interface Calculator {

  int calculate(int a, int b);

  default void print(String result){
    // … print result …
  }
}

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


Calculator c = (a, b) -> a + b;

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.

Comments

  1. By user1623

Leave a Reply