blob: e4d7d946b2a5637795a87771933875c245dee28e [file] [log] [blame]
Adarsh Fernandocdf04a52016-03-08 18:27:27 -08001page.title=Using 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 <h2>
8 In this document
9 </h2>
10
11 <ol>
12 <li>
13 <a href="#supported-features">Supported Java 8 Language Features and APIs</a>
14 </li>
15
16 <li>
17 <a href="#configuration">Enabling Java 8 Features and the Jack Toolchain</a>
18 </li>
19 </ol>
20
21 <h2>
22 See also
23 </h2>
24
25 <ol>
26 <li>
27 <a class="external-link" href=
28 "https://source.android.com/source/jack.html">Jack (Java Android Compiler
29 Kit)</a>
30 </li>
31 </ol>
32 </div>
33</div>
34
35<p>
36 The N Developer Preview introduces newly supported Java 8 language features
37 that you can use when developing apps that target the Preview. To start using
38 these features, you need to download and set up Android Studio 2.1 (preview)
39 and the N Preview SDK, which include the required Jack toolchain and updated
40 Android Plugin for Gradle.
41</p>
42
43<p>
44 To learn how to set up Android Studio for the Preview, <a href=
45 "{@docRoot}preview/setup-sdk.html">read the guide</a>. If you instead want to
46 simply test your app's forward-compatibility, follow the guide to <a href=
47 "{@docRoot}preview/run-app.html">run your app on
48 the N Preview</a>.
49</p>
50
51<p>
52 This document will discuss the new language features supported in the
53 Preview, how to properly set up your project to use them, and any known
54 issues you may encounter.
55</p>
56
57<p class="note">
58 <strong>Note:</strong> Using the new Java 8 language features is not a
59 requirement for developing apps that target the N Developer Preview. After
60 following the set up instructions in the <a href=
61 "{@docRoot}preview/setup-sdk.html">Preview guide</a>, you may use
62 the Java 7 language features already supported by Android 6.0 (API level 23)
63 and below.
64</p>
65
66<h2 id="supported-features">
67 Supported Java 8 Language Features and APIs
68</h2>
69
70<p>
71 Android does not currently support all Java 8 language features. However, the
72 following features are now available when developing apps targeting the N
73 Developer Preview:
74</p>
75
76<ul>
77 <li>
78 <a class="external-link" href=
79 "https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html">Default
80 and static interface methods</a>
81 </li>
82
83 <li>
84 <a class="external-link" href=
85 "https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html">
86 Lambda expressions</a>
87 </li>
88
89 <li>
90 <a class="external-link" href=
91 "https://docs.oracle.com/javase/tutorial/java/annotations/repeating.html">Repeatable
92 annotations</a>
93 </li>
94
95 <li>
96 <a class="external-link" href=
97 "https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html">
98 Method References</a>
99 </li>
100
101 <li>
102 <a class="external-link" href=
103 "https://docs.oracle.com/javase/tutorial/java/IandI/interfaceAsType.html">Type
104 Interface</a>
105 </li>
106</ul>
107
108
109<p>
110 Additionally, the following Java 8 language feature APIs are now available
111 with the Preview:
112</p>
113
114<ul>
115 <li>Reflection and language-related APIs:
116 </li>
117
118 <li style="list-style: none; display: inline">
119 <ul>
120 <li>{@code java.lang.FunctionalInterface}
121 </li>
122
123 <li>{@code java.lang.annotation.Repeatable}
124 </li>
125
126 <li>{@code java.lang.reflect.Method.isDefault()}
127 </li>
128
129 <li>and Reflection APIs associated with repeatable annotations, such as
130 {@code AnnotatedElement.getAnnotationsByType(Class)}
131 </li>
132 </ul>
133 </li>
134 <li>Utility APIs:
135 </li>
136
137 <li style="list-style: none; display: inline">
138 <ul>
139 <li>{@code java.util.function}
140 </li>
141 </ul>
142 </li>
143</ul>
144
145<p class="note">
146 <strong>Note:</strong> The N Developer Preview bases its implementation of
147 lambda expressions on anonymous classes. This approach allows them to be
148 backwards compatible and executable on earlier versions of Android. To test
149 lambda expressions on earlier versions, remember to go to your {@code
150 build.gradle} file, and set {@code compileSdkVersion} and {@code
151 targetSdkVersion} to 23 or lower.
152</p>
153
154<h2 id="configuration">
155 Enabling Java 8 Features and the Jack Toolchain
156</h2>
157
158<p>
159 In order to use the new Java 8 language features, you need to also use the
160 new <a class="external-link" href=
161 "https://source.android.com/source/jack.html">Jack toolchain</a>. This new
162 Android toolchain compiles Java language source into Android-readable dex
163 bytecode, has its own {@code .jack} library format, and provides most tool chain
164 features as part of a single tool: repackaging, shrinking, obfuscation and
165 multidex.
166</p>
167
168<p>Here we compare the two toolchains used to build Android DEX files:</p>
169<ul>
170 <li>Legacy: javac ({@code .java} &gt; {@code .class}) &gt; dx ({@code
171 .class} &gt; {@code .dex})
172 </li>
173
174 <li>Modern: Jack ({@code .java} &gt; {@code .jack} &gt; {@code .dex})
175 </li>
176</ul>
177
178<h3>
179 Configuring Gradle
180</h3>
181
182<p>
183 To enable the Java 8 language features and Jack for your project, enter the
184 following in your module-specific {@code build.gradle} file:
185</p>
186
187<pre>
188android {
189...
190 defaultConfig {
191 ...
192 jackOptions {
193 enabled true
194 }
195 }
196 compileOptions {
197 sourceCompatibility JavaVersion.VERSION_1_8
198 targetCompatibility JavaVersion.VERSION_1_8
199 }
200}
201</pre>
202
203<h3>
204 Known Issues
205</h3>
206
207<p>
208 Instant Run, introduced in Android Studio 2.0 (Beta), does not currently work
209 with Jack and will be disabled while using the new toolchain.
210</p>
211
212<p>
213 Since Jack does not generate intermediate class files when compiling an app,
214 tools that depend on these files do not currently work with Jack. Some examples of
215 these tools are:
216</p>
217
218<ul>
219 <li>Lint detectors that operate on class files
220 </li>
221
222 <li>Tools and libraries that require the app’s class files (e.g. JaCoCo and Mockito)
223 </li>
224</ul>
225
226If you find other problems while using Jack, <a class="external-link" href=
227"http://tools.android.com/filing-bugs">please report bugs</a>.