Adarsh Fernando | cdf04a5 | 2016-03-08 18:27:27 -0800 | [diff] [blame] | 1 | page.title=Using Java 8 Language Features |
| 2 | page.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.com | 6789404 | 2016-03-08 21:14:47 -0800 | [diff] [blame^] | 35 | <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 Fernando | cdf04a5 | 2016-03-08 18:27:27 -0800 | [diff] [blame] | 37 | these features, you need to download and set up Android Studio 2.1 (preview) |
smain@google.com | 6789404 | 2016-03-08 21:14:47 -0800 | [diff] [blame^] | 38 | 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 |
| 43 | N</a>.</p> |
| 44 | |
Adarsh Fernando | cdf04a5 | 2016-03-08 18:27:27 -0800 | [diff] [blame] | 45 | |
| 46 | <p> |
smain@google.com | 6789404 | 2016-03-08 21:14:47 -0800 | [diff] [blame^] | 47 | This document describes the new language features supported in the Android N |
Adarsh Fernando | cdf04a5 | 2016-03-08 18:27:27 -0800 | [diff] [blame] | 48 | 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.com | 6789404 | 2016-03-08 21:14:47 -0800 | [diff] [blame^] | 54 | requirement for developing apps that target the Android N Preview. After |
Adarsh Fernando | cdf04a5 | 2016-03-08 18:27:27 -0800 | [diff] [blame] | 55 | 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> |
| 89 | |
| 90 | <li> |
| 91 | <a class="external-link" href= |
| 92 | "https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html"> |
| 93 | Method References</a> |
| 94 | </li> |
| 95 | |
| 96 | <li> |
| 97 | <a class="external-link" href= |
| 98 | "https://docs.oracle.com/javase/tutorial/java/IandI/interfaceAsType.html">Type |
| 99 | Interface</a> |
| 100 | </li> |
| 101 | </ul> |
| 102 | |
| 103 | |
| 104 | <p> |
| 105 | Additionally, the following Java 8 language feature APIs are now available |
| 106 | with the Preview: |
| 107 | </p> |
| 108 | |
| 109 | <ul> |
| 110 | <li>Reflection and language-related APIs: |
| 111 | </li> |
| 112 | |
| 113 | <li style="list-style: none; display: inline"> |
| 114 | <ul> |
| 115 | <li>{@code java.lang.FunctionalInterface} |
| 116 | </li> |
| 117 | |
| 118 | <li>{@code java.lang.annotation.Repeatable} |
| 119 | </li> |
| 120 | |
| 121 | <li>{@code java.lang.reflect.Method.isDefault()} |
| 122 | </li> |
| 123 | |
| 124 | <li>and Reflection APIs associated with repeatable annotations, such as |
| 125 | {@code AnnotatedElement.getAnnotationsByType(Class)} |
| 126 | </li> |
| 127 | </ul> |
| 128 | </li> |
| 129 | <li>Utility APIs: |
| 130 | </li> |
| 131 | |
| 132 | <li style="list-style: none; display: inline"> |
| 133 | <ul> |
| 134 | <li>{@code java.util.function} |
| 135 | </li> |
| 136 | </ul> |
| 137 | </li> |
| 138 | </ul> |
| 139 | |
| 140 | <p class="note"> |
smain@google.com | 6789404 | 2016-03-08 21:14:47 -0800 | [diff] [blame^] | 141 | <strong>Note:</strong> The Android N bases its implementation of |
Adarsh Fernando | cdf04a5 | 2016-03-08 18:27:27 -0800 | [diff] [blame] | 142 | lambda expressions on anonymous classes. This approach allows them to be |
| 143 | backwards compatible and executable on earlier versions of Android. To test |
| 144 | lambda expressions on earlier versions, remember to go to your {@code |
| 145 | build.gradle} file, and set {@code compileSdkVersion} and {@code |
| 146 | targetSdkVersion} to 23 or lower. |
| 147 | </p> |
| 148 | |
| 149 | <h2 id="configuration"> |
| 150 | Enabling Java 8 Features and the Jack Toolchain |
| 151 | </h2> |
| 152 | |
| 153 | <p> |
| 154 | In order to use the new Java 8 language features, you need to also use the |
| 155 | new <a class="external-link" href= |
| 156 | "https://source.android.com/source/jack.html">Jack toolchain</a>. This new |
| 157 | Android toolchain compiles Java language source into Android-readable dex |
| 158 | bytecode, has its own {@code .jack} library format, and provides most tool chain |
| 159 | features as part of a single tool: repackaging, shrinking, obfuscation and |
| 160 | multidex. |
| 161 | </p> |
| 162 | |
| 163 | <p>Here we compare the two toolchains used to build Android DEX files:</p> |
| 164 | <ul> |
| 165 | <li>Legacy: javac ({@code .java} > {@code .class}) > dx ({@code |
| 166 | .class} > {@code .dex}) |
| 167 | </li> |
| 168 | |
| 169 | <li>Modern: Jack ({@code .java} > {@code .jack} > {@code .dex}) |
| 170 | </li> |
| 171 | </ul> |
| 172 | |
| 173 | <h3> |
| 174 | Configuring Gradle |
| 175 | </h3> |
| 176 | |
| 177 | <p> |
| 178 | To enable the Java 8 language features and Jack for your project, enter the |
| 179 | following in your module-specific {@code build.gradle} file: |
| 180 | </p> |
| 181 | |
| 182 | <pre> |
| 183 | android { |
| 184 | ... |
| 185 | defaultConfig { |
| 186 | ... |
| 187 | jackOptions { |
| 188 | enabled true |
| 189 | } |
| 190 | } |
| 191 | compileOptions { |
| 192 | sourceCompatibility JavaVersion.VERSION_1_8 |
| 193 | targetCompatibility JavaVersion.VERSION_1_8 |
| 194 | } |
| 195 | } |
| 196 | </pre> |
| 197 | |
| 198 | <h3> |
| 199 | Known Issues |
| 200 | </h3> |
| 201 | |
| 202 | <p> |
| 203 | Instant Run, introduced in Android Studio 2.0 (Beta), does not currently work |
| 204 | with Jack and will be disabled while using the new toolchain. |
| 205 | </p> |
| 206 | |
| 207 | <p> |
| 208 | Since Jack does not generate intermediate class files when compiling an app, |
| 209 | tools that depend on these files do not currently work with Jack. Some examples of |
| 210 | these tools are: |
| 211 | </p> |
| 212 | |
| 213 | <ul> |
| 214 | <li>Lint detectors that operate on class files |
| 215 | </li> |
| 216 | |
| 217 | <li>Tools and libraries that require the app’s class files (e.g. JaCoCo and Mockito) |
| 218 | </li> |
| 219 | </ul> |
| 220 | |
| 221 | If you find other problems while using Jack, <a class="external-link" href= |
| 222 | "http://tools.android.com/filing-bugs">please report bugs</a>. |