blob: 1f24530f0a37999aeac51818cca161746c60fbec [file] [log] [blame]
smain@google.coma329ffd2016-03-09 00:00:37 -08001page.title=Java 8 Language Features
Adarsh Fernandocdf04a52016-03-08 18:27:27 -08002page.keywords="android N", "Java 8", "Jack"
3@jd:body
4
5<div id="qv-wrapper">
6 <div id="qv">
Adarsh Fernandocdf04a52016-03-08 18:27:27 -08007 <ol>
8 <li>
9 <a href="#supported-features">Supported Java 8 Language Features and APIs</a>
10 </li>
Adarsh Fernandocdf04a52016-03-08 18:27:27 -080011 <li>
12 <a href="#configuration">Enabling Java 8 Features and the Jack Toolchain</a>
13 </li>
14 </ol>
Adarsh Fernandocdf04a52016-03-08 18:27:27 -080015 </div>
16</div>
17
smain@google.com67894042016-03-08 21:14:47 -080018<p>Android N introduces support for Java 8 language features
smain@google.coma329ffd2016-03-09 00:00:37 -080019 that you can use when developing apps that target Android N.
20 This page describes the new language features supported in the Android N
Adarsh Fernandocdf04a52016-03-08 18:27:27 -080021 Preview, how to properly set up your project to use them, and any known
22 issues you may encounter.
23</p>
24
smain@google.coma329ffd2016-03-09 00:00:37 -080025<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
Adarsh Fernandocdf04a52016-03-08 18:27:27 -080033<p class="note">
34 <strong>Note:</strong> Using the new Java 8 language features is not a
smain@google.coma329ffd2016-03-09 00:00:37 -080035 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.
Adarsh Fernandocdf04a52016-03-08 18:27:27 -080039</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
smain@google.coma329ffd2016-03-09 00:00:37 -080047 following features are now available when developing apps targeting the
48 Android N Preview:
Adarsh Fernandocdf04a52016-03-08 18:27:27 -080049</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>
Adarsh Fernandocdf04a52016-03-08 18:27:27 -080069</ul>
70
71
72<p>
smain@google.coma329ffd2016-03-09 00:00:37 -080073 Additionally, the following Java 8 language feature APIs are now available:
Adarsh Fernandocdf04a52016-03-08 18:27:27 -080074</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">
smain@google.com67894042016-03-08 21:14:47 -0800108 <strong>Note:</strong> The Android N bases its implementation of
Adarsh Fernandocdf04a52016-03-08 18:27:27 -0800109 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
smain@google.coma329ffd2016-03-09 00:00:37 -0800125 bytecode, has its own {@code .jack} library format, and provides most toolchain
Adarsh Fernandocdf04a52016-03-08 18:27:27 -0800126 features as part of a single tool: repackaging, shrinking, obfuscation and
127 multidex.
128</p>
129
smain@google.coma329ffd2016-03-09 00:00:37 -0800130<p>Here is a comparison the two toolchains used to build Android DEX files:</p>
Adarsh Fernandocdf04a52016-03-08 18:27:27 -0800131<ul>
smain@google.coma329ffd2016-03-09 00:00:37 -0800132 <li>Legacy javac toolchain:<br>
133 <b>javac</b> ({@code .java} --&gt; {@code .class}) --&gt; <b>dx</b> ({@code
134 .class} --&gt; {@code .dex})
Adarsh Fernandocdf04a52016-03-08 18:27:27 -0800135 </li>
136
smain@google.coma329ffd2016-03-09 00:00:37 -0800137 <li>New Jack toolchain:<br>
138 <b>Jack</b> ({@code .java} --&gt; {@code .jack} --&gt; {@code .dex})
Adarsh Fernandocdf04a52016-03-08 18:27:27 -0800139 </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 {
Adarsh Fernandocdf04a52016-03-08 18:27:27 -0800153 ...
smain@google.coma329ffd2016-03-09 00:00:37 -0800154 defaultConfig {
155 ...
Adarsh Fernandocdf04a52016-03-08 18:27:27 -0800156 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
smain@google.coma329ffd2016-03-09 00:00:37 -0800176<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>
Adarsh Fernandocdf04a52016-03-08 18:27:27 -0800179
180<ul>
181 <li>Lint detectors that operate on class files
182 </li>
183
smain@google.coma329ffd2016-03-09 00:00:37 -0800184 <li>Tools and libraries that require the app’s class files (such as JaCoCo
185 and Mockito)</li>
Adarsh Fernandocdf04a52016-03-08 18:27:27 -0800186</ul>
187
smain@google.coma329ffd2016-03-09 00:00:37 -0800188<p>If you find other problems while using Jack, <a href=
189"http://tools.android.com/filing-bugs">please report bugs</a>.</p>