blob: 78252f08e8db4e7c5a76fc9bd400e3b85ed99486 [file] [log] [blame]
Trevor Johns682c24e2016-04-12 10:13:47 -07001page.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 (preview) 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>
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</ul>
70
71
72<p>
73 Additionally, the following Java 8 language feature APIs are now available:
74</p>
75
76<ul>
77 <li>Reflection and language-related APIs:
78 </li>
79
80 <li style="list-style: none; display: inline">
81 <ul>
82 <li>{@code java.lang.FunctionalInterface}
83 </li>
84
85 <li>{@code java.lang.annotation.Repeatable}
86 </li>
87
88 <li>{@code java.lang.reflect.Method.isDefault()}
89 </li>
90
91 <li>and Reflection APIs associated with repeatable annotations, such as
92 {@code AnnotatedElement.getAnnotationsByType(Class)}
93 </li>
94 </ul>
95 </li>
96 <li>Utility APIs:
97 </li>
98
99 <li style="list-style: none; display: inline">
100 <ul>
101 <li>{@code java.util.function}
102 </li>
103 </ul>
104 </li>
105</ul>
106
107<p class="note">
108 <strong>Note:</strong> The Android N bases its implementation of
109 lambda expressions on anonymous classes. This approach allows them to be
110 backwards compatible and executable on earlier versions of Android. To test
111 lambda expressions on earlier versions, remember to go to your {@code
112 build.gradle} file, and set {@code compileSdkVersion} and {@code
113 targetSdkVersion} to 23 or lower.
114</p>
115
116<h2 id="configuration">
117 Enabling Java 8 Features and the Jack Toolchain
118</h2>
119
120<p>
121 In order to use the new Java 8 language features, you need to also use the
122 new <a class="external-link" href=
123 "https://source.android.com/source/jack.html">Jack toolchain</a>. This new
124 Android toolchain compiles Java language source into Android-readable dex
125 bytecode, has its own {@code .jack} library format, and provides most toolchain
126 features as part of a single tool: repackaging, shrinking, obfuscation and
127 multidex.
128</p>
129
130<p>Here is a comparison of the two toolchains used to build Android DEX files:</p>
131<ul>
132 <li>Legacy javac toolchain:<br>
133 <b>javac</b> ({@code .java} --&gt; {@code .class}) --&gt; <b>dx</b> ({@code
134 .class} --&gt; {@code .dex})
135 </li>
136
137 <li>New Jack toolchain:<br>
138 <b>Jack</b> ({@code .java} --&gt; {@code .jack} --&gt; {@code .dex})
139 </li>
140</ul>
141
142<h3>
143 Configuring Gradle
144</h3>
145
146<p>
147 To enable the Java 8 language features and Jack for your project, enter the
148 following in your module-specific {@code build.gradle} file:
149</p>
150
151<pre>
152android {
153 ...
154 defaultConfig {
155 ...
156 jackOptions {
157 enabled true
158 }
159 }
160 compileOptions {
161 sourceCompatibility JavaVersion.VERSION_1_8
162 targetCompatibility JavaVersion.VERSION_1_8
163 }
164}
165</pre>
166
167<h3>
168 Known Issues
169</h3>
170
171<p>
172 Instant Run, introduced in Android Studio 2.0 (Beta), does not currently work
173 with Jack and will be disabled while using the new toolchain.
174</p>
175
176<p>Because Jack does not generate intermediate class files when compiling an
177app, tools that depend on these files do not currently work with Jack. Some
178examples of these tools are:</p>
179
180<ul>
181 <li>Lint detectors that operate on class files
182 </li>
183
184 <li>Tools and libraries that require the app’s class files (such as
185 instrumentation tests with JaCoCo)
186 </li>
187</ul>
188
189<p>
190 Calls to an interface's default method on a <a class="external-link" href=
191 "https://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Proxy.html"><code>
192 Proxy</code></a> instance fail with an exception.
193</p>
194
195<p>If you find other problems while using Jack, <a href=
196"http://tools.android.com/filing-bugs">please report bugs</a>.</p>