1 page.title=Use 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 id="Contents"> 8 In this document: 9 </h2> 10 11 <ol> 12 <li> 13 <a href="#supported-features">Supported Java 8 Language Features and 14 APIs</a> 15 </li> 16 17 <li> 18 <a href="#configuration">Enable Java 8 Features and the Jack 19 Toolchain</a> 20 <ul> 21 <li> 22 <a href="#configure-gradle">Configure Gradle</a> 23 </li> 24 25 <li> 26 <a href="#known-issues">Known issues</a> 27 </li> 28 </ul> 29 </li> 30 </ol> 31 </div> 32 </div> 33 34 <p> 35 Android supports all Java 7 language features and a subset of Java 8 language 36 features that vary by platform version. This page describes the new language 37 features you can use, how to properly configure your project to use them, and 38 any known issues you may encounter. 39 </p> 40 41 <p class="note"> 42 <strong>Note:</strong> When developing apps for Android, using Java 8 43 language features is optional. You can keep your project's source and target 44 compatibility values set to Java 7, but you still need to compile using JDK 45 8. 46 </p> 47 48 <p> 49 Support for Java 8 language features requires a new compiler called <a href= 50 "https://source.android.com/source/jack.html">Jack</a>. Jack is supported 51 only on Android Studio 2.1 and higher. So if you want to use Java 8 language 52 features, you need to use Android Studio 2.1 to build your app. 53 </p> 54 55 <p> 56 If you already have Android Studio installed, make sure you update to the 57 latest version by clicking <strong>Help > Check for Update</strong> (on 58 Mac, <strong>Android Studio > Check for Updates</strong>). If you don't 59 already have the IDE installed on your workstation, <a href= 60 "{@docRoot}studio/">download Android Studio here</a>. 61 </p> 62 63 <h2 id="supported-features"> 64 Supported Java 8 Language Features and APIs 65 </h2> 66 67 <p> 68 Android does not support all Java 8 language features. However, the 69 following features are available when developing apps targeting 70 Android 7.0 (API level 24): 71 </p> 72 73 <ul> 74 <li> 75 <a class="external-link" href= 76 "https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html">Default 77 and static interface methods</a> 78 </li> 79 80 <li> 81 <a class="external-link" href= 82 "https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html"> 83 Lambda expressions</a> (also available on API level 23 and lower) 84 </li> 85 86 <li> 87 <a class="external-link" href= 88 "https://docs.oracle.com/javase/tutorial/java/annotations/repeating.html">Repeatable 89 annotations</a> 90 </li> 91 92 <li> 93 <a class="external-link" href= 94 "https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html"> 95 Method References</a> (also available on API level 23 and lower) 96 </li> 97 98 <li> 99 <a class="external-link" href= 100 "https://docs.oracle.com/javase/tutorial/java/annotations/type_annotations.html"> 101 Type Annotations</a> (also available on API level 23 and lower) 102 </li> 103 </ul> 104 105 <p class="note"> 106 <strong>Note:</strong> Type annotation information is only available at 107 compile time, and not during runtime. 108 </p> 109 110 <p> 111 To test lambda expressions, method references, and type annotations on 112 earlier versions of Android, go to your {@code build.gradle} file, and set 113 {@code compileSdkVersion} and {@code targetSdkVersion} to 23 or lower. You 114 will still need to <a href="#configuration">enable the Jack toolchain</a> to 115 use these Java 8 features. 116 </p> 117 118 <p> 119 Additionally, the following Java 8 language APIs are also available: 120 </p> 121 122 <ul> 123 <li>Reflection and language-related APIs: 124 <ul> 125 <li> 126 <a class="external-link" href= 127 "https://docs.oracle.com/javase/8/docs/api/java/lang/FunctionalInterface.html"> 128 {@code java.lang.FunctionalInterface}</a> 129 </li> 130 131 <li> 132 <a class="external-link" href= 133 "https://docs.oracle.com/javase/8/docs/api/java/lang/annotation/Repeatable.html"> 134 {@code java.lang.annotation.Repeatable}</a> 135 </li> 136 137 <li> 138 <a class="external-link" href= 139 "https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#isDefault--"> 140 {@code java.lang.reflect.Method.isDefault()}</a> 141 </li> 142 143 <li>and Reflection APIs associated with repeatable annotations, such as 144 <a class="external-link" href= 145 "https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/AnnotatedElement.html#getAnnotationsByType-java.lang.Class-"> 146 {@code AnnotatedElement.getAnnotationsByType(Class)}</a> 147 </li> 148 </ul> 149 </li> 150 151 <li>Utility APIs: 152 <ul> 153 <li> 154 <a class="external-link" href= 155 "https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html"> 156 {@code java.util.function}</a> 157 </li> 158 159 <li> 160 <a class="external-link" href= 161 "https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html"> 162 {@code java.util.stream}</a> 163 </li> 164 </ul> 165 </li> 166 </ul> 167 168 <h2 id="configuration"> 169 Enable Java 8 Features and the Jack Toolchain 170 </h2> 171 172 <p> 173 In order to use the new Java 8 language features, you need to also use the 174 <a href="https://source.android.com/source/jack.html">Jack toolchain</a>. 175 This new Android toolchain compiles Java language sources into 176 Android-readable DEX bytecode, has its own {@code .jack} library format, and 177 provides most toolchain features as part of a single tool: repackaging, 178 shrinking, obfuscation and multidex. 179 </p> 180 181 <p>Here is a comparison of the two toolchains used to build Android DEX files:</p> 182 <ul> 183 <li>Legacy javac toolchain:<br> 184 <b>javac</b> ({@code .java} {@code .class}) <b>dx</b> ({@code 185 .class} {@code .dex}) 186 </li> 187 188 <li>New Jack toolchain:<br> 189 <b>Jack</b> ({@code .java} {@code .jack} {@code .dex}) 190 </li> 191 </ul> 192 193 <h3 id="configure-gradle"> 194 Configure Gradle 195 </h3> 196 197 <p> 198 To enable Java 8 language features and Jack for your project, enter the 199 following in your module-level {@code build.gradle} file: 200 </p> 201 202 <pre> 203 android { 204 ... 205 defaultConfig { 206 ... 207 jackOptions { 208 enabled true 209 } 210 } 211 compileOptions { 212 sourceCompatibility JavaVersion.VERSION_1_8 213 targetCompatibility JavaVersion.VERSION_1_8 214 } 215 } 216 </pre> 217 218 <h3 id="known-issues"> 219 Known issues 220 </h3> 221 222 <p> 223 <a href="{@docRoot}tools/building/building-studio.html#instant-run">Instant 224 Run</a> does not currently work with Jack and will be disabled while using 225 the new toolchain. 226 </p> 227 228 <p>Because Jack does not generate intermediate class files when compiling an 229 app, tools that depend on these files do not currently work with Jack. Some 230 examples of these tools are:</p> 231 232 <ul> 233 <li>Lint detectors that operate on class files 234 </li> 235 236 <li>Tools and libraries that require the apps class files (such as 237 instrumentation tests with JaCoCo) 238 </li> 239 </ul> 240 241 <p>If you find other issues while using Jack, <a href= 242 "http://tools.android.com/filing-bugs">please file a bug</a>.</p>