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