This article covers the use of new Java 8 features in the Android N preview,


The Android team recently released Android N Preview, which brings a number of improvements, including Java 8 support from the Jack compiler. In this article, we’ll look at what it really means for Android developers and how to try out new language features.

Disclaimer: This information is valid as of March 30, 2016, and I am not sure what new Java 8 features the Google team will add in the next release that are not mentioned here.

An overview of

In this article, it doesn’t make much sense to cover the new features of Oracle Java 8 – much of the information is already available on the Internet. My personal favorite is Simon Ritter’s “55 New Features of Java SE 8 [2]”.

On the other hand, Android’s official Java 8 announcement [3] leaves a lot of open questions for developers, and it feels like not all of the native Java 8 features are available. A more detailed technical announcement [4] confirms this. We can categorize these language features based on 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 notes

  • Flow (Streams)

  • Reflection APIs

So developers must make careful choices about the relationship between Java 8 features and the minSdkVersion used. We must also note that language backward compatibility is provided by the Jack compiler. Conceptually, the Jack compiler combines the capabilities of JavAC, ProGuard, and DEX [5] into one transformation step. This means that no intermediate Java bytecode is available [6], and tools like JaCoCo and Mockito will not work, as will DexGuard (the enterprise version of ProGuard). Let’s hope this is just an early preview and that these issues will be fixed in the future.

Lambda expressions and the related function APIs — this is something every Android developer will love. This kind of functionality will be extremely useful for code readability — it replaces anonymous inner classes that provide event listeners. Previously, this could only be done with additional tools [7], or with the Android Studio editor to fold the code.

The default and static interface methods can help reduce the number of additional utility classes, but they are clearly not the most desirable feature. There are a few other new features that I’d like to cover in more detail and therefore are outside the scope of this article.

The most interesting thing for me — Streams for Java 8 — is not available in the current preview. We can see that in fact it has just been merged [8] into the AOSP source code, so expect to see it in the next N Preview or Beta release. If you can’t wait to browse – try using Lightweight stream-API [9], currently an open source backward compatibility.

The sample project

The official manual [10] provides instructions and even diagrams showing how to configure your project using Android N Preview and Java 8. There’s nothing to say here. Just follow the directions.


The next step is to configure the build.gradle file for your app module. You can see the build.gradle file for the instance below. From the announcement on the N SDK, it looks like it can be set

minSdkVersion
targetSdkVersion

Will not work on devices with an API lower than N [11]

minSdkVersion

SO forums[12]

I decided to keep the example code clean, so I didn’t add any hacks to make it compatible with lower versions. I left the reader free to try it out or use N’s test device/emulator.

Copy the code

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

Copy the code

android {

compileSdkVersion ‘android-N’

BuildToolsVersion ‘24.0.0 rc1’

defaultConfig {

applicationId “org.sergiiz.thermometer”

MinSdkVersion ‘N’ // Versions younger than N cannot be used in N Preview

targetSdkVersion ‘N’

versionCode 1

VersionName “1.0”

jackOptions{

enabled true

}

}

compileOptions {

TargetCompatibility 1.8

SourceCompatibility 1.8

}

/ /…

}

Note that this setting follows the new documentation [13], using the new Gradle DSL method

jackOptions
useJack true

So let’s try implementing some elegant Java 8 code into our old Thermometer project.

This is an interface that contains default methods:

Copy the code

1

2

3

4

5

6

7

8

9

10

11

12

13

Copy the code

public interface Thermometer {

void setCelsius(final float celsiusValue);

float getValue();

String getSign();

default String getFormattedValue(){

return String.format(Locale.getDefault(),

“The temperature is %.2f %s”, getValue(), getSign());

}

}

Classes that implement this interface:

Copy the code

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

Copy the code

public class FahrenheitThermometer implements Thermometer {

private float fahrenheitDeg;

public FahrenheitThermometer(float celsius) {

setCelsius(celsius);

}

[@Override](/user/Override)

public void setCelsius(float celsius) {

fahrenheitDeg = celsius * 9 / 5 + 32f;

}

[@Override](/user/Override)

public float getValue() {

return fahrenheitDeg;

}

[@Override](/user/Override)

public String getSign() {

return Constants.DEGREE + “F”;

}

}

Add a lambda function for click events:

Copy the code

1

2

3

4

5

Copy the code

buttonFahrenheit.setOnClickListener(view1 -> {

fahrenheitThermometer.setCelsius(currentCelsius);

String text = fahrenheitThermometer.getFormattedValue();

makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();

});

The full source code for the example is available at GitHub Repository [14].

conclusion

In this article, we looked at the Java 8 use case and its current implementation in the Android N Preview SDK. We also saw the limitations of the current Jack compiler and its features that might be fixed before the final release. In the demo project we examined how to use the new Java 8 features and the target SDK versions they can be applied to.


I have several aliyun lucky coupons to share with you. There will be special surprises when you buy or upgrade aliyun corresponding products with the coupons! Take your lucky tickets for all the products you want to buy! Do it. We’re about to get all of them.