smain@google.com | a329ffd | 2016-03-09 00:00:37 -0800 | [diff] [blame^] | 1 | page.title=Java 8 Language Features |
Adarsh Fernando | cdf04a5 | 2016-03-08 18:27:27 -0800 | [diff] [blame] | 2 | page.keywords="android N", "Java 8", "Jack" |
| 3 | @jd:body |
| 4 | |
| 5 | <div id="qv-wrapper"> |
| 6 | <div id="qv"> |
Adarsh Fernando | cdf04a5 | 2016-03-08 18:27:27 -0800 | [diff] [blame] | 7 | <ol> |
| 8 | <li> |
| 9 | <a href="#supported-features">Supported Java 8 Language Features and APIs</a> |
| 10 | </li> |
Adarsh Fernando | cdf04a5 | 2016-03-08 18:27:27 -0800 | [diff] [blame] | 11 | <li> |
| 12 | <a href="#configuration">Enabling Java 8 Features and the Jack Toolchain</a> |
| 13 | </li> |
| 14 | </ol> |
Adarsh Fernando | cdf04a5 | 2016-03-08 18:27:27 -0800 | [diff] [blame] | 15 | </div> |
| 16 | </div> |
| 17 | |
smain@google.com | 6789404 | 2016-03-08 21:14:47 -0800 | [diff] [blame] | 18 | <p>Android N introduces support for Java 8 language features |
smain@google.com | a329ffd | 2016-03-09 00:00:37 -0800 | [diff] [blame^] | 19 | 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 Fernando | cdf04a5 | 2016-03-08 18:27:27 -0800 | [diff] [blame] | 21 | Preview, how to properly set up your project to use them, and any known |
| 22 | issues you may encounter. |
| 23 | </p> |
| 24 | |
smain@google.com | a329ffd | 2016-03-09 00:00:37 -0800 | [diff] [blame^] | 25 | <p>To start using these features, you need to download and set up Android |
| 26 | Studio 2.1 (preview) and the Android N Preview SDK, which includes the required |
| 27 | Jack toolchain and updated Android Plugin for Gradle. If you haven't yet |
| 28 | installed 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 Fernando | cdf04a5 | 2016-03-08 18:27:27 -0800 | [diff] [blame] | 33 | <p class="note"> |
| 34 | <strong>Note:</strong> Using the new Java 8 language features is not a |
smain@google.com | a329ffd | 2016-03-09 00:00:37 -0800 | [diff] [blame^] | 35 | 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 Fernando | cdf04a5 | 2016-03-08 18:27:27 -0800 | [diff] [blame] | 39 | </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.com | a329ffd | 2016-03-09 00:00:37 -0800 | [diff] [blame^] | 47 | following features are now available when developing apps targeting the |
| 48 | Android N Preview: |
Adarsh Fernando | cdf04a5 | 2016-03-08 18:27:27 -0800 | [diff] [blame] | 49 | </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 Fernando | cdf04a5 | 2016-03-08 18:27:27 -0800 | [diff] [blame] | 69 | </ul> |
| 70 | |
| 71 | |
| 72 | <p> |
smain@google.com | a329ffd | 2016-03-09 00:00:37 -0800 | [diff] [blame^] | 73 | Additionally, the following Java 8 language feature APIs are now available: |
Adarsh Fernando | cdf04a5 | 2016-03-08 18:27:27 -0800 | [diff] [blame] | 74 | </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.com | 6789404 | 2016-03-08 21:14:47 -0800 | [diff] [blame] | 108 | <strong>Note:</strong> The Android N bases its implementation of |
Adarsh Fernando | cdf04a5 | 2016-03-08 18:27:27 -0800 | [diff] [blame] | 109 | 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.com | a329ffd | 2016-03-09 00:00:37 -0800 | [diff] [blame^] | 125 | bytecode, has its own {@code .jack} library format, and provides most toolchain |
Adarsh Fernando | cdf04a5 | 2016-03-08 18:27:27 -0800 | [diff] [blame] | 126 | features as part of a single tool: repackaging, shrinking, obfuscation and |
| 127 | multidex. |
| 128 | </p> |
| 129 | |
smain@google.com | a329ffd | 2016-03-09 00:00:37 -0800 | [diff] [blame^] | 130 | <p>Here is a comparison the two toolchains used to build Android DEX files:</p> |
Adarsh Fernando | cdf04a5 | 2016-03-08 18:27:27 -0800 | [diff] [blame] | 131 | <ul> |
smain@google.com | a329ffd | 2016-03-09 00:00:37 -0800 | [diff] [blame^] | 132 | <li>Legacy javac toolchain:<br> |
| 133 | <b>javac</b> ({@code .java} --> {@code .class}) --> <b>dx</b> ({@code |
| 134 | .class} --> {@code .dex}) |
Adarsh Fernando | cdf04a5 | 2016-03-08 18:27:27 -0800 | [diff] [blame] | 135 | </li> |
| 136 | |
smain@google.com | a329ffd | 2016-03-09 00:00:37 -0800 | [diff] [blame^] | 137 | <li>New Jack toolchain:<br> |
| 138 | <b>Jack</b> ({@code .java} --> {@code .jack} --> {@code .dex}) |
Adarsh Fernando | cdf04a5 | 2016-03-08 18:27:27 -0800 | [diff] [blame] | 139 | </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> |
| 152 | android { |
Adarsh Fernando | cdf04a5 | 2016-03-08 18:27:27 -0800 | [diff] [blame] | 153 | ... |
smain@google.com | a329ffd | 2016-03-09 00:00:37 -0800 | [diff] [blame^] | 154 | defaultConfig { |
| 155 | ... |
Adarsh Fernando | cdf04a5 | 2016-03-08 18:27:27 -0800 | [diff] [blame] | 156 | 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.com | a329ffd | 2016-03-09 00:00:37 -0800 | [diff] [blame^] | 176 | <p>Because Jack does not generate intermediate class files when compiling an |
| 177 | app, tools that depend on these files do not currently work with Jack. Some |
| 178 | examples of these tools are:</p> |
Adarsh Fernando | cdf04a5 | 2016-03-08 18:27:27 -0800 | [diff] [blame] | 179 | |
| 180 | <ul> |
| 181 | <li>Lint detectors that operate on class files |
| 182 | </li> |
| 183 | |
smain@google.com | a329ffd | 2016-03-09 00:00:37 -0800 | [diff] [blame^] | 184 | <li>Tools and libraries that require the app’s class files (such as JaCoCo |
| 185 | and Mockito)</li> |
Adarsh Fernando | cdf04a5 | 2016-03-08 18:27:27 -0800 | [diff] [blame] | 186 | </ul> |
| 187 | |
smain@google.com | a329ffd | 2016-03-09 00:00:37 -0800 | [diff] [blame^] | 188 | <p>If you find other problems while using Jack, <a href= |
| 189 | "http://tools.android.com/filing-bugs">please report bugs</a>.</p> |