summaryrefslogtreecommitdiff
path: root/docs/html/guide/platform/j8-jack.jd
blob: 9b19426200e63be3045c9f324fc7bb9915389d3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
page.title=Use Java 8 Language Features
page.keywords="android N", "Java 8", "Jack"
@jd:body

<div id="qv-wrapper">
  <div id="qv">
    <h2 id="Contents">
      In this document:
    </h2>

    <ol>
      <li>
        <a href="#supported-features">Supported Java 8 Language Features and
        APIs</a>
      </li>

      <li>
        <a href="#configuration">Enable Java 8 Features and the Jack
        Toolchain</a>
        <ul>
          <li>
            <a href="#configure-gradle">Configure Gradle</a>
          </li>

          <li>
            <a href="#known-issues">Known issues</a>
          </li>
        </ul>
      </li>
    </ol>
  </div>
</div>

<p>
  Android supports all Java 7 language features and a subset of Java 8 language
  features that vary by platform version. This page describes the new language
  features you can use, how to properly configure your project to use them, and
  any known issues you may encounter.
</p>

<p class="note">
  <strong>Note:</strong> When developing apps for Android, using Java 8
  language features is optional. You can keep your project's source and target
  compatibility values set to Java 7, but you still need to compile using JDK
  8.
</p>

<p>
  Support for Java 8 language features requires a new compiler called <a href=
  "https://source.android.com/source/jack.html">Jack</a>. Jack is supported
  only on Android Studio 2.1 and higher. So if you want to use Java 8 language
  features, you need to use Android Studio 2.1 to build your app.
</p>

<p>
  If you already have Android Studio installed, make sure you update to the
  latest version by clicking <strong>Help &gt; Check for Update</strong> (on
  Mac, <strong>Android Studio &gt; Check for Updates</strong>). If you don't
  already have the IDE installed on your workstation, <a href=
  "{@docRoot}studio/">download Android Studio here</a>.
</p>

<h2 id="supported-features">
  Supported Java 8 Language Features and APIs
</h2>

<p>
  Android does not support all Java 8 language features. However, the
  following features are available when developing apps targeting
  Android 7.0 (API level 24):
</p>

<ul>
  <li>
    <a class="external-link" href=
    "https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html">Default
    and static interface methods</a>
  </li>

  <li>
    <a class="external-link" href=
    "https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html">
    Lambda expressions</a> (also available on API level 23 and lower)
  </li>

  <li>
    <a class="external-link" href=
    "https://docs.oracle.com/javase/tutorial/java/annotations/repeating.html">Repeatable
    annotations</a>
  </li>

  <li>
    <a class="external-link" href=
    "https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html">
    Method References</a> (also available on API level 23 and lower)
  </li>

  <li>
    <a class="external-link" href=
    "https://docs.oracle.com/javase/tutorial/java/annotations/type_annotations.html">
    Type Annotations</a> (also available on API level 23 and lower)
  </li>
</ul>

<p class="note">
  <strong>Note:</strong> Type annotation information is only available at
  compile time, and not during runtime.
</p>

<p>
  To test lambda expressions, method references, and type annotations on
  earlier versions of Android, go to your {@code build.gradle} file, and set
  {@code compileSdkVersion} and {@code targetSdkVersion} to 23 or lower. You
  will still need to <a href="#configuration">enable the Jack toolchain</a> to
  use these Java 8 features.
</p>

<p>
  Additionally, the following Java 8 language APIs are also available:
</p>

<ul>
  <li>Reflection and language-related APIs:
    <ul>
      <li>
        <a class="external-link" href=
        "https://docs.oracle.com/javase/8/docs/api/java/lang/FunctionalInterface.html">
        {@code java.lang.FunctionalInterface}</a>
      </li>

      <li>
        <a class="external-link" href=
        "https://docs.oracle.com/javase/8/docs/api/java/lang/annotation/Repeatable.html">
        {@code java.lang.annotation.Repeatable}</a>
      </li>

      <li>
        <a class="external-link" href=
        "https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#isDefault--">
        {@code java.lang.reflect.Method.isDefault()}</a>
      </li>

      <li>and Reflection APIs associated with repeatable annotations, such as
        <a class="external-link" href=
        "https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/AnnotatedElement.html#getAnnotationsByType-java.lang.Class-">
        {@code AnnotatedElement.getAnnotationsByType(Class)}</a>
      </li>
    </ul>
  </li>

  <li>Utility APIs:
    <ul>
      <li>
        <a class="external-link" href=
        "https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html">
        {@code java.util.function}</a>
      </li>

      <li>
        <a class="external-link" href=
        "https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html">
        {@code java.util.stream}</a>
      </li>
    </ul>
  </li>
</ul>

<h2 id="configuration">
  Enable Java 8 Features and the Jack Toolchain
</h2>

<p>
  In order to use the new Java 8 language features, you need to also use the
  <a href="https://source.android.com/source/jack.html">Jack toolchain</a>.
  This new Android toolchain compiles Java language sources into
  Android-readable DEX bytecode, has its own {@code .jack} library format, and
  provides most toolchain features as part of a single tool: repackaging,
  shrinking, obfuscation and multidex.
</p>

<p>Here is a comparison of the two toolchains used to build Android DEX files:</p>
<ul>
  <li>Legacy javac toolchain:<br>
  <b>javac</b> ({@code .java} → {@code .class}) → <b>dx</b> ({@code
  .class} → {@code .dex})
  </li>

  <li>New Jack toolchain:<br>
  <b>Jack</b> ({@code .java} → {@code .jack} → {@code .dex})
  </li>
</ul>

<h3 id="configure-gradle">
  Configure Gradle
</h3>

<p>
  To enable Java 8 language features and Jack for your project, enter the
  following in your module-level {@code build.gradle} file:
</p>

<pre>
android {
  ...
  defaultConfig {
    ...
    jackOptions {
      enabled true
    }
  }
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}
</pre>

<h3 id="known-issues">
  Known issues
</h3>

<p>
  <a href="{@docRoot}tools/building/building-studio.html#instant-run">Instant
  Run</a> does not currently work with Jack and will be disabled while using
  the new toolchain.
</p>

<p>Because Jack does not generate intermediate class files when compiling an
app, tools that depend on these files do not currently work with Jack. Some
examples of these tools are:</p>

<ul>
  <li>Lint detectors that operate on class files
  </li>

  <li>Tools and libraries that require the app’s class files (such as
  instrumentation tests with JaCoCo)
  </li>
</ul>

<p>If you find other issues while using Jack, <a href=
"http://tools.android.com/filing-bugs">please file a bug</a>.</p>