2016-03-09 00:00:37 -08:00
|
|
|
|
page.title=Java 8 Language Features
|
2016-03-08 18:27:27 -08:00
|
|
|
|
page.keywords="android N", "Java 8", "Jack"
|
|
|
|
|
@jd:body
|
|
|
|
|
|
|
|
|
|
<div id="qv-wrapper">
|
|
|
|
|
<div id="qv">
|
|
|
|
|
<ol>
|
|
|
|
|
<li>
|
|
|
|
|
<a href="#supported-features">Supported Java 8 Language Features and APIs</a>
|
|
|
|
|
</li>
|
|
|
|
|
<li>
|
|
|
|
|
<a href="#configuration">Enabling Java 8 Features and the Jack Toolchain</a>
|
|
|
|
|
</li>
|
|
|
|
|
</ol>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
2016-03-08 21:14:47 -08:00
|
|
|
|
<p>Android N introduces support for Java 8 language features
|
2016-03-09 00:00:37 -08:00
|
|
|
|
that you can use when developing apps that target Android N.
|
|
|
|
|
This page describes the new language features supported in the Android N
|
2016-03-08 18:27:27 -08:00
|
|
|
|
Preview, how to properly set up your project to use them, and any known
|
|
|
|
|
issues you may encounter.
|
|
|
|
|
</p>
|
|
|
|
|
|
2016-03-09 00:00:37 -08:00
|
|
|
|
<p>To start using these features, you need to download and set up Android
|
2016-04-22 15:27:28 -07:00
|
|
|
|
Studio 2.1 and the Android N Preview SDK, which includes the required
|
2016-03-09 00:00:37 -08:00
|
|
|
|
Jack toolchain and updated Android Plugin for Gradle. If you haven't yet
|
|
|
|
|
installed the Android N Preview SDK, see <a href=
|
|
|
|
|
"{@docRoot}preview/setup-sdk.html">Set Up to Develop for Android N</a>.</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-03-08 18:27:27 -08:00
|
|
|
|
<p class="note">
|
|
|
|
|
<strong>Note:</strong> Using the new Java 8 language features is not a
|
2016-03-09 00:00:37 -08:00
|
|
|
|
requirement for developing apps that target the Android N platform. If you
|
|
|
|
|
don't want to write code with Java 8 language features, you can keep your
|
|
|
|
|
project's source and target compatibility values set to Java 7, but you still
|
|
|
|
|
must compile with JDK 8 to build against the Android N platform.
|
2016-03-08 18:27:27 -08:00
|
|
|
|
</p>
|
|
|
|
|
|
|
|
|
|
<h2 id="supported-features">
|
|
|
|
|
Supported Java 8 Language Features and APIs
|
|
|
|
|
</h2>
|
|
|
|
|
|
|
|
|
|
<p>
|
|
|
|
|
Android does not currently support all Java 8 language features. However, the
|
2016-03-09 00:00:37 -08:00
|
|
|
|
following features are now available when developing apps targeting the
|
|
|
|
|
Android N Preview:
|
2016-03-08 18:27:27 -08:00
|
|
|
|
</p>
|
|
|
|
|
|
|
|
|
|
<ul>
|
|
|
|
|
<li>
|
|
|
|
|
<a class="external-link" href=
|
|
|
|
|
"https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html">Default
|
|
|
|
|
and static interface methods</a>
|
|
|
|
|
</li>
|
|
|
|
|
|
|
|
|
|
<li>
|
|
|
|
|
<a class="external-link" href=
|
|
|
|
|
"https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html">
|
2016-04-19 16:30:44 -07:00
|
|
|
|
Lambda expressions</a> (also available on API level 23 and lower)
|
2016-03-08 18:27:27 -08:00
|
|
|
|
</li>
|
|
|
|
|
|
|
|
|
|
<li>
|
|
|
|
|
<a class="external-link" href=
|
|
|
|
|
"https://docs.oracle.com/javase/tutorial/java/annotations/repeating.html">Repeatable
|
|
|
|
|
annotations</a>
|
|
|
|
|
</li>
|
2016-04-12 16:52:58 -07:00
|
|
|
|
|
|
|
|
|
<li>
|
|
|
|
|
<a class="external-link" href=
|
|
|
|
|
"https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html">
|
2016-04-19 16:30:44 -07:00
|
|
|
|
Method References</a> (also available on API level 23 and lower)
|
2016-04-12 16:52:58 -07:00
|
|
|
|
</li>
|
2016-03-08 18:27:27 -08:00
|
|
|
|
</ul>
|
|
|
|
|
|
2016-04-19 16:30:44 -07:00
|
|
|
|
<p class="note">
|
|
|
|
|
<strong>Note:</strong> To test lambda expressions and method references on
|
|
|
|
|
earlier versions of Android, go to your {@code build.gradle}
|
|
|
|
|
file, and set {@code compileSdkVersion} and {@code targetSdkVersion} to 23 or
|
|
|
|
|
lower. You will still need to <a href="#configuration">enable the Jack
|
|
|
|
|
toolchain</a> to use these Java 8 features.
|
|
|
|
|
</p>
|
2016-03-08 18:27:27 -08:00
|
|
|
|
|
|
|
|
|
<p>
|
2016-03-09 00:00:37 -08:00
|
|
|
|
Additionally, the following Java 8 language feature APIs are now available:
|
2016-03-08 18:27:27 -08:00
|
|
|
|
</p>
|
|
|
|
|
|
|
|
|
|
<ul>
|
|
|
|
|
<li>Reflection and language-related APIs:
|
|
|
|
|
<ul>
|
2016-04-12 16:52:58 -07:00
|
|
|
|
<li>
|
|
|
|
|
<a class="external-link" href=
|
|
|
|
|
"https://docs.oracle.com/javase/8/docs/api/java/lang/FunctionalInterface.html">
|
|
|
|
|
{@code java.lang.FunctionalInterface}</a>
|
2016-03-08 18:27:27 -08:00
|
|
|
|
</li>
|
|
|
|
|
|
2016-04-12 16:52:58 -07:00
|
|
|
|
<li>
|
|
|
|
|
<a class="external-link" href=
|
|
|
|
|
"https://docs.oracle.com/javase/8/docs/api/java/lang/annotation/Repeatable.html">
|
|
|
|
|
{@code java.lang.annotation.Repeatable}</a>
|
2016-03-08 18:27:27 -08:00
|
|
|
|
</li>
|
|
|
|
|
|
2016-04-12 16:52:58 -07:00
|
|
|
|
<li>
|
|
|
|
|
<a class="external-link" href=
|
|
|
|
|
"https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#isDefault--">
|
|
|
|
|
{@code java.lang.reflect.Method.isDefault()}</a>
|
2016-03-08 18:27:27 -08:00
|
|
|
|
</li>
|
|
|
|
|
|
|
|
|
|
<li>and Reflection APIs associated with repeatable annotations, such as
|
2016-04-12 16:52:58 -07:00
|
|
|
|
<a class="external-link" href=
|
|
|
|
|
"https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/AnnotatedElement.html#getAnnotationsByType-java.lang.Class-">
|
|
|
|
|
{@code AnnotatedElement.getAnnotationsByType(Class)}</a>
|
2016-03-08 18:27:27 -08:00
|
|
|
|
</li>
|
|
|
|
|
</ul>
|
|
|
|
|
</li>
|
|
|
|
|
|
2016-04-12 16:52:58 -07:00
|
|
|
|
<li>Utility APIs:
|
2016-03-08 18:27:27 -08:00
|
|
|
|
<ul>
|
2016-04-12 16:52:58 -07:00
|
|
|
|
<li>
|
|
|
|
|
<a class="external-link" href=
|
|
|
|
|
"https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html">
|
|
|
|
|
{@code java.util.function}</a>
|
|
|
|
|
</li>
|
|
|
|
|
|
|
|
|
|
<li>
|
|
|
|
|
<a class="external-link" href=
|
|
|
|
|
"https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html">
|
|
|
|
|
{@code java.util.stream}</a>
|
2016-03-08 18:27:27 -08:00
|
|
|
|
</li>
|
|
|
|
|
</ul>
|
|
|
|
|
</li>
|
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
|
|
<h2 id="configuration">
|
|
|
|
|
Enabling Java 8 Features and the Jack Toolchain
|
|
|
|
|
</h2>
|
|
|
|
|
|
|
|
|
|
<p>
|
|
|
|
|
In order to use the new Java 8 language features, you need to also use the
|
|
|
|
|
new <a class="external-link" href=
|
|
|
|
|
"https://source.android.com/source/jack.html">Jack toolchain</a>. This new
|
|
|
|
|
Android toolchain compiles Java language source into Android-readable dex
|
2016-03-09 00:00:37 -08:00
|
|
|
|
bytecode, has its own {@code .jack} library format, and provides most toolchain
|
2016-03-08 18:27:27 -08:00
|
|
|
|
features as part of a single tool: repackaging, shrinking, obfuscation and
|
|
|
|
|
multidex.
|
|
|
|
|
</p>
|
|
|
|
|
|
2016-03-17 09:58:05 -07:00
|
|
|
|
<p>Here is a comparison of the two toolchains used to build Android DEX files:</p>
|
2016-03-08 18:27:27 -08:00
|
|
|
|
<ul>
|
2016-03-09 00:00:37 -08:00
|
|
|
|
<li>Legacy javac toolchain:<br>
|
|
|
|
|
<b>javac</b> ({@code .java} --> {@code .class}) --> <b>dx</b> ({@code
|
|
|
|
|
.class} --> {@code .dex})
|
2016-03-08 18:27:27 -08:00
|
|
|
|
</li>
|
|
|
|
|
|
2016-03-09 00:00:37 -08:00
|
|
|
|
<li>New Jack toolchain:<br>
|
|
|
|
|
<b>Jack</b> ({@code .java} --> {@code .jack} --> {@code .dex})
|
2016-03-08 18:27:27 -08:00
|
|
|
|
</li>
|
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
|
|
<h3>
|
|
|
|
|
Configuring Gradle
|
|
|
|
|
</h3>
|
|
|
|
|
|
|
|
|
|
<p>
|
|
|
|
|
To enable the Java 8 language features and Jack for your project, enter the
|
2016-04-19 16:30:44 -07:00
|
|
|
|
following in your module-level {@code build.gradle} file:
|
2016-03-08 18:27:27 -08:00
|
|
|
|
</p>
|
|
|
|
|
|
|
|
|
|
<pre>
|
|
|
|
|
android {
|
|
|
|
|
...
|
2016-03-09 00:00:37 -08:00
|
|
|
|
defaultConfig {
|
|
|
|
|
...
|
2016-03-08 18:27:27 -08:00
|
|
|
|
jackOptions {
|
|
|
|
|
enabled true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
compileOptions {
|
|
|
|
|
sourceCompatibility JavaVersion.VERSION_1_8
|
|
|
|
|
targetCompatibility JavaVersion.VERSION_1_8
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</pre>
|
|
|
|
|
|
2016-04-19 16:30:44 -07:00
|
|
|
|
<h3 id="known-issues">
|
2016-03-08 18:27:27 -08:00
|
|
|
|
Known Issues
|
|
|
|
|
</h3>
|
|
|
|
|
|
|
|
|
|
<p>
|
2016-04-12 16:52:58 -07:00
|
|
|
|
<a href="{@docRoot}tools/building/building-studio.html#instant-run">Instant
|
|
|
|
|
Run</a> does not currently work with Jack and will be disabled while using
|
|
|
|
|
the new toolchain.
|
2016-03-08 18:27:27 -08:00
|
|
|
|
</p>
|
|
|
|
|
|
2016-03-09 00:00:37 -08:00
|
|
|
|
<p>Because Jack does not generate intermediate class files when compiling an
|
|
|
|
|
app, tools that depend on these files do not currently work with Jack. Some
|
|
|
|
|
examples of these tools are:</p>
|
2016-03-08 18:27:27 -08:00
|
|
|
|
|
|
|
|
|
<ul>
|
|
|
|
|
<li>Lint detectors that operate on class files
|
|
|
|
|
</li>
|
|
|
|
|
|
2016-03-17 09:58:05 -07:00
|
|
|
|
<li>Tools and libraries that require the app’s class files (such as
|
|
|
|
|
instrumentation tests with JaCoCo)
|
|
|
|
|
</li>
|
2016-03-08 18:27:27 -08:00
|
|
|
|
</ul>
|
|
|
|
|
|
2016-03-09 00:00:37 -08:00
|
|
|
|
<p>If you find other problems while using Jack, <a href=
|
2016-04-19 16:30:44 -07:00
|
|
|
|
"http://tools.android.com/filing-bugs">please file a bug</a>.</p>
|