1page.title=Java 8 Language Features
2page.keywords="android N", "Java 8", "Jack"
3@jd:body
4
5<div id="qv-wrapper">
6  <div id="qv">
7    <ol>
8      <li>
9        <a href="#supported-features">Supported Java 8 Language Features and APIs</a>
10      </li>
11      <li>
12        <a href="#configuration">Enabling Java 8 Features and the Jack Toolchain</a>
13      </li>
14    </ol>
15  </div>
16</div>
17
18<p>Android N introduces support for Java 8 language features
19  that you can use when developing apps that target Android N.
20  This page describes the new language features supported in the Android N
21  Preview, how to properly set up your project to use them, and any known
22  issues you may encounter.
23</p>
24
25<p>To start using these features, you need to download and set up Android
26Studio 2.1 and the Android N Preview SDK, which includes the required
27Jack toolchain and updated Android Plugin for Gradle. If you haven't yet
28installed the Android N Preview SDK, see <a href=
29"{@docRoot}preview/setup-sdk.html">Set Up to Develop for Android N</a>.</p>
30
31
32
33<p class="note">
34  <strong>Note:</strong> Using the new Java 8 language features is not a
35  requirement for developing apps that target the Android N platform. If you
36  don't want to write code with Java 8 language features, you can keep your
37  project's source and target compatibility values set to Java 7, but you still
38  must compile with JDK 8 to build against the Android N platform.
39</p>
40
41<h2 id="supported-features">
42  Supported Java 8 Language Features and APIs
43</h2>
44
45<p>
46  Android does not currently support all Java 8 language features. However, the
47  following features are now available when developing apps targeting the
48  Android N Preview:
49</p>
50
51<ul>
52  <li>
53    <a class="external-link" href=
54    "https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html">Default
55    and static interface methods</a>
56  </li>
57
58  <li>
59    <a class="external-link" href=
60    "https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html">
61    Lambda expressions</a> (also available on API level 23 and lower)
62  </li>
63
64  <li>
65    <a class="external-link" href=
66    "https://docs.oracle.com/javase/tutorial/java/annotations/repeating.html">Repeatable
67    annotations</a>
68  </li>
69
70  <li>
71    <a class="external-link" href=
72    "https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html">
73    Method References</a> (also available on API level 23 and lower)
74  </li>
75
76  <li>
77    <a class="external-link" href=
78    "https://docs.oracle.com/javase/tutorial/java/annotations/type_annotations.html">
79    Type Annotations</a> (also available on API level 23 and lower)
80  </li>
81</ul>
82
83<p class="note">
84  <strong>Note:</strong> Type annotation information is only available at
85  compile time, and not during runtime.
86</p>
87
88<p>
89  To test lambda expressions, method references, and type annotations on
90  earlier versions of Android, go to your {@code build.gradle} file, and set
91  {@code compileSdkVersion} and {@code targetSdkVersion} to 23 or lower. You
92  will still need to <a href="#configuration">enable the Jack toolchain</a> to
93  use these Java 8 features.
94</p>
95
96<p>
97  Additionally, the following Java 8 language feature APIs are now available:
98</p>
99
100<ul>
101  <li>Reflection and language-related APIs:
102    <ul>
103      <li>
104        <a class="external-link" href=
105        "https://docs.oracle.com/javase/8/docs/api/java/lang/FunctionalInterface.html">
106        {@code java.lang.FunctionalInterface}</a>
107      </li>
108
109      <li>
110        <a class="external-link" href=
111        "https://docs.oracle.com/javase/8/docs/api/java/lang/annotation/Repeatable.html">
112        {@code java.lang.annotation.Repeatable}</a>
113      </li>
114
115      <li>
116        <a class="external-link" href=
117        "https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#isDefault--">
118        {@code java.lang.reflect.Method.isDefault()}</a>
119      </li>
120
121      <li>and Reflection APIs associated with repeatable annotations, such as
122        <a class="external-link" href=
123        "https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/AnnotatedElement.html#getAnnotationsByType-java.lang.Class-">
124        {@code AnnotatedElement.getAnnotationsByType(Class)}</a>
125      </li>
126    </ul>
127  </li>
128
129  <li>Utility APIs:
130    <ul>
131      <li>
132        <a class="external-link" href=
133        "https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html">
134        {@code java.util.function}</a>
135      </li>
136
137      <li>
138        <a class="external-link" href=
139        "https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html">
140        {@code java.util.stream}</a>
141      </li>
142    </ul>
143  </li>
144</ul>
145
146<h2 id="configuration">
147  Enabling Java 8 Features and the Jack Toolchain
148</h2>
149
150<p>
151  In order to use the new Java 8 language features, you need to also use the
152  new <a class="external-link" href=
153  "https://source.android.com/source/jack.html">Jack toolchain</a>. This new
154  Android toolchain compiles Java language source into Android-readable dex
155  bytecode, has its own {@code .jack} library format, and provides most toolchain
156  features as part of a single tool: repackaging, shrinking, obfuscation and
157  multidex.
158</p>
159
160<p>Here is a comparison of the two toolchains used to build Android DEX files:</p>
161<ul>
162  <li>Legacy javac toolchain:<br>
163  <b>javac</b> ({@code .java} --&gt; {@code .class}) --&gt; <b>dx</b> ({@code
164  .class} --&gt; {@code .dex})
165  </li>
166
167  <li>New Jack toolchain:<br>
168  <b>Jack</b> ({@code .java} --&gt; {@code .jack} --&gt; {@code .dex})
169  </li>
170</ul>
171
172<h3>
173  Configuring Gradle
174</h3>
175
176<p>
177  To enable the Java 8 language features and Jack for your project, enter the
178  following in your module-level {@code build.gradle} file:
179</p>
180
181<pre>
182android {
183  ...
184  defaultConfig {
185    ...
186    jackOptions {
187      enabled true
188    }
189  }
190  compileOptions {
191    sourceCompatibility JavaVersion.VERSION_1_8
192    targetCompatibility JavaVersion.VERSION_1_8
193  }
194}
195</pre>
196
197<h3 id="known-issues">
198  Known Issues
199</h3>
200
201<p>
202  <a href="{@docRoot}tools/building/building-studio.html#instant-run">Instant
203  Run</a> does not currently work with Jack and will be disabled while using
204  the new toolchain.
205</p>
206
207<p>Because Jack does not generate intermediate class files when compiling an
208app, tools that depend on these files do not currently work with Jack. Some
209examples of these tools are:</p>
210
211<ul>
212  <li>Lint detectors that operate on class files
213  </li>
214
215  <li>Tools and libraries that require the app’s class files (such as
216  instrumentation tests with JaCoCo)
217  </li>
218</ul>
219
220<p>If you find other problems while using Jack, <a href=
221"http://tools.android.com/filing-bugs">please file a bug</a>.</p>