1page.title=Jack (Java Android Compiler Kit) 2@jd:body 3 4<!-- 5 Copyright 2015 The Android Open Source Project 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18--> 19 20<div id="qv-wrapper"> 21 <div id="qv"> 22 <h2>In this document</h2> 23 <ol id="auto-toc"> 24 </ol> 25 </div> 26</div> 27 28<h2 id=the_jack_toolchain>The Jack toolchain</h2> 29 30<p>Jack (Java Android Compiler Kit) is a new Android toolchain that compiles Java 31source into Android dex bytecode. It replaces the previous Android toolchain, 32which consists of multiple tools, such as javac, ProGuard, jarjar, and dx.</p> 33 34<p>The Jack toolchain provides the following advantages:</p> 35 36<ul> 37 <li> <strong>Completely open source</strong><br> 38Available in AOSP; partners are welcome to contribute. 39 <li> <strong>Speeds compilation time</strong><br> 40 41Jack has specific supports to reduce compilation time: pre-dexing, incremental 42compilation and a Jack compilation server. 43 <li> <strong>Handles shrinking, obfuscation, repackaging and multidex</strong><br> 44Using a separate package such as ProGuard is no longer necessary. 45</ul> 46 47<p class="note">Note that beginning in Android 7.0 (N), Jack supports code coverage with JaCoCo. 48See <a href="https://android.googlesource.com/platform/prebuilts/sdk/+/master/tools/README-jack-code-coverage.md"> 49Code Coverage with JaCoCo</a> and <a href="https://developer.android.com/preview/j8-jack.html"> 50Java 8 Language Features</a> for details.</p> 51 52<img src="{@docRoot}images/jack-overview.png" height="75%" width="75%" alt="Jack overview" /> 53<p class="img-caption"><strong>Figure 1. </strong>Jack (Java Android Compiler Kit)</p> 54 55<h2 id=the_jack_library_format>The .jack library format</h2> 56 57<p>Jack has its own .jack file format, which contains the pre-compiled dex code 58for the library, allowing for faster compilation (pre-dex).</p> 59 60<img src="{@docRoot}images/jack-library-file.png" height="75%" width="75%" alt="Jack library file contents" /> 61<p class="img-caption"><strong>Figure 2. </strong>Jack library file contents</p> 62 63<h2 id=jack_intermediate_library_linker_jill>Jack Intermediate Library Linker (Jill)</h2> 64 65<p>The Jill tool translates the existing .jar libraries into the new library 66format, as shown below.</p> 67 68<img src="{@docRoot}images/jill.png" alt="Importing existing .jar libraries using Jill" /> 69<p class="img-caption"><strong>Figure 3. </strong>Workflow to import an existing .jar library</p> 70 71<h2 id=using_jack_in_your_android_build>Using Jack in your Android build</h2> 72 73<div class="note">For instructions on using Jack in Android 7.0 (N) and later, see the <a 74href="https://android.googlesource.com/platform/prebuilts/sdk/+/master/tools/README-jack-server.md">Jack 75server documentation</a>. For Android 6.0 (M), use the instructions in this section.</div> 76 77<p>You don’t have to do anything differently to use Jack — just use your 78standard makefile commands to compile the tree or your project. Jack is the 79default Android build toolchain for M.</p> 80 81<p>The first time Jack is used, it launches a local Jack compilation server on 82your computer:</p> 83 84<ul> 85 <li> This server brings an intrinsic speedup, because it avoids launching a new host 86JRE JVM, loading Jack code, initializing Jack and warming up the JIT at each 87compilation. It also provides very good compilation times during small 88compilations (e.g. in incremental mode). 89 <li> The server is also a short-term solution to control the number of parallel Jack 90compilations, and so to avoid overloading your computer (memory or disk issue), 91because it limits the number of parallel compilations. 92</ul> 93 94<p>The Jack server shuts itself down after an idle time without any compilation. 95It uses two TCP ports on the localhost interface, and so is not available 96externally. All these parameters (number of parallel compilations, timeout, 97ports number, etc) can be modified by editing the<code> $HOME/.jack</code> file.</p> 98 99<h3 id=$home_jack_file>$HOME/.jack file</h3> 100 101<p>The <code>$HOME/.jack</code> file contains settings for Jack server variables, in a full bash syntax. </p> 102 103<p>Here are the available settings, with their definitions and default values:</p> 104 105<ul> 106 <li> <strong><code>SERVER=true</strong> </code>Enable the server feature of Jack. 107 <li> <strong><code>SERVER_PORT_SERVICE=8072</code> 108</strong>Set the TCP port number of the server for compilation purposes. 109 <li> <strong><code>SERVER_PORT_ADMIN=8073</code></strong> 110Set the TCP port number of the server for admin purposes. 111 <li> <strong><code>SERVER_COUNT=1</code></strong> 112Unused at present. 113 <li> <strong><code>SERVER_NB_COMPILE=4</code></strong> 114Maximum number of parallel compilations allowed. 115 <li> <strong><code>SERVER_TIMEOUT=60</code></strong> 116Number of idle seconds the server has to wait without any compilation before 117shutting itself down. 118 <li> <strong><code>SERVER_LOG=${SERVER_LOG:=$SERVER_DIR/jack-$SERVER_PORT_SERVICE.log}</code></strong> 119File where server logs are written. By default, this variable can be 120overloaded by an environment variable. 121 <li> <strong><code>JACK_VM_COMMAND=${JACK_VM_COMMAND:=java}</code></strong> 122The default command used to launch a JVM on the host. By default, this 123variable can be overloaded by environment variable. 124</ul> 125 126<h3 id=jack_troubleshooting>Jack troubleshooting</h3> 127 128<p><strong>If your computer becomes unresponsive during compilation or if you experience 129Jack compilations failing on “Out of memory error”</strong></p> 130 131<p>You can improve the situation by reducing the number of Jack simultaneous 132compilations by editing your<code> $HOME/.jack</code> and changing<code> SERVER_NB_COMPILE</code> to a lower value.</p> 133 134<p><strong>If your compilations are failing on “Cannot launch background server”</strong></p> 135 136<p>The most likely cause is TCP ports are already used on your computer. Try to 137change it by editing your <code>$HOME/.jack </code>(<code>SERVER_PORT_SERVICE</code> and <code>SERVER_PORT_ADMIN</code> variables).</p> 138 139<p>If it doesn’t solve the problem, please report and attach your compilation log 140and the Jack server log (see ‘Finding the Jack log’ below to know where to find 141the server log file). To unblock the situation, disable jack compilation server 142by editing your <code>$HOME/.jack</code> and changing <code>SERVER</code> to false. Unfortunately this will significantly slow down your compilation and 143may force you to launch <code>make -j</code> with load control (option "<code>-l</code>" of <code>make</code>). </p> 144 145<p><strong>If your compilation gets stuck without any progress</strong></p> 146 147<p>Please report this and give us the following additional information (where 148possible):</p> 149 150<ul> 151 <li> The command line at which you are stuck. 152 <li> The output of this command line. 153 <li> The result of executing <code>jack-admin server-stat</code>. 154 <li> The <code>$HOME/.jack</code> file. 155 <li> The content of the server log with the server state dumped. To get this — 156 <ul> 157 <li> Find the Jack background server process by running <code>jack-admin list-server</code>. 158 <li> Send a <code>kill -3</code> command to this server to dump its state into the log file. 159 <li> To locate the server log file, see ‘Finding the Jack log’ below. 160 </ul> 161 <li> The result of executing <code>ls -lR $TMPDIR/jack-$USER.</code> 162 <li> The result of running <code>ps j -U $USER.</code> 163</ul> 164 165<p>You should be able to unblock yourself by killing the Jack background server 166(use <code>jack-admin kill-server</code>), and then by removing its temporary directories contained in <code>jack-$USER</code> of your temporary directory (<code>/tmp</code> or <code>$TMPDIR</code>).</p> 167 168<p><strong>If you have any other issues </strong></p> 169 170<p>To report bugs or request features, please use our public issue tracker, 171available at <a href="http://b.android.com">http://b.android.com</a>, with the <a href="https://code.google.com/p/android/issues/entry?template=Jack%20bug%20report">Jack tool bug report</a> or <a href="https://code.google.com/p/android/issues/entry?template=Jack%20feature%20request">Jack tool feature request</a> templates. Please attach the Jack log to the bug report. </p> 172<table> 173 <tr> 174 <td><strong>Finding the Jack log</strong> 175<ul> 176 <li> If you ran a make command with a dist target, the Jack log is located at <code>$ANDROID_BUILD_TOP/out/dist/logs/jack-server.log</code> 177 <li> Otherwise you can find it in by running <code>jack-admin server-log</code> 178</ul> 179</td> 180 </tr> 181</table> 182 183<p>In case of reproducible Jack failures, you can get a more detailed log by 184setting one variable, as follows:</p> 185 186<pre class=prettyprint> 187$ export ANDROID_JACK_EXTRA_ARGS= "--verbose debug --sanity-checks on -D 188sched.runner=single-threaded" 189</pre> 190 191<p>Then use your standard makefile commands to compile the tree or your project 192and attach its standard output and error.</p> 193 194<p>To remove detailed build logs use:</p> 195 196<pre class=prettyprint> 197$ unset ANDROID_JACK_EXTRA_ARGS 198</pre> 199 200<h3 id=jack_limitations>Jack limitations</h3> 201 202<ul> 203 <li> The Jack server is mono-user by default, so can be only used by one user on a 204computer. If it is not the case, please, choose different port numbers for each 205user and adjust SERVER_NB_COMPILE accordingly. You can also disable the Jack 206server by setting SERVER=false in your $HOME/.jack. 207 <li> CTS compilation is slow due to current vm-tests-tf integration. 208 <li> Bytecode manipulation tools, like JaCoCo, are not supported. 209</ul> 210 211<h2 id=using_jack_features>Using Jack features</h2> 212 213<p>Jack supports Java programming language 1.7 and integrates additional features 214described below.</p> 215 216<h3 id=predexing>Predexing </h3> 217 218<p>When generating a JACK library file, the .dex of the library is generated and 219stored inside the .jack library file as a pre-dex. When compiling, JACK reuses 220the pre-dex from each library.</p> 221 222<p>All libraries are pre-dexed.</p> 223 224<img src="{@docRoot}images/pre-dex.png" height="75%" width="75%" alt="Jack libraries with pre-dex" /> 225<p class="img-caption"><strong>Figure 4. </strong>Jack libraries with pre-dex</p> 226 227<h4 id=limitations>Limitations</h4> 228 229 230<p>Currently, JACK does not reuse the library pre-dex if 231shrinking/obfuscation/repackaging is used in the compilation.</p> 232 233<h3 id=incremental_compilation>Incremental compilation</h3> 234 235 236<p>Incremental compilation means that only components that were touched since the 237last compilation, and their dependencies, are recompiled. Incremental 238compilation can be significantly faster than a full compilation when changes 239are limited to only a limited set of components.</p> 240 241<h4 id=limitations>Limitations</h4> 242 243 244<p>Incremental compilation is deactivated when shrinking, obfuscation, repackaging 245or multi-dex legacy is enabled.</p> 246 247<h4 id=enabling_incremental_builds>Enabling incremental builds</h4> 248 249 250<p>Currently incremental compilation is not enabled by default. To enable 251incremental builds, add the following line to the Android.mk file of the 252project that you want to build incrementally:</p> 253 254<pre class=prettyprint> 255LOCAL_JACK_ENABLED := incremental 256</pre> 257 258<p class="note"><strong>Note:</strong> The first time that you build your project with Jack if some dependencies 259are not built, use <code>mma</code> to build them, and after that you can use the standard build command.</p> 260 261<h3 id=shrinking_and_obfuscation>Shrinking and Obfuscation</h3> 262 263<p>JACK has shrinking and obfuscation support and uses proguard configuration 264files to enable shrinking and obfuscation features. Here are the supported and 265ignored options:</p> 266 267<h4 id=supported_common_options>Supported common options</h4> 268 269 270<p>Common options include the following:</p> 271 272<ul> 273 <li> <code>@</code> 274 <li> <code>-include</code> 275 <li> <code>-basedirectory</code> 276 <li> <code>-injars</code> 277 <li> <code>-outjars // only 1 output jar supported</code> 278 <li> <code>-libraryjars</code> 279 <li> <code>-keep</code> 280 <li> <code>-keepclassmembers</code> 281 <li> <code>-keepclasseswithmembers</code> 282 <li> <code>-keepnames</code> 283 <li> <code>-keepclassmembernames</code> 284 <li> <code>-keepclasseswithmembernames</code> 285 <li> <code>-printseeds</code> 286</ul> 287 288<h4 id=supported_shrinking_options>Supported shrinking options</h4> 289 290 291<p>Shrinking options include the following:</p> 292 293<ul> 294 <li> <code>-dontshrink</code> 295</ul> 296 297<h4 id=supported_obfuscation_options>Supported obfuscation options</h4> 298 299 300<p>Obfuscation options include the following:</p> 301 302<ul> 303 <li> <code>-dontobfuscate</code> 304 <li> <code>-printmapping</code> 305 <li> <code>-applymapping</code> 306 <li> <code>-obfuscationdictionary</code> 307 <li> <code>-classobfuscationdictionary</code> 308 <li> <code>-packageobfuscationdictionary</code> 309 <li> <code>-useuniqueclassmembernames</code> 310 <li> <code>-dontusemixedcaseclassnames</code> 311 <li> <code>-keeppackagenames</code> 312 <li> <code>-flattenpackagehierarchy</code> 313 <li> <code>-repackageclasses</code> 314 <li> <code>-keepattributes</code> 315 <li> <code>-adaptclassstrings</code> 316</ul> 317 318<h4 id=ignored_options>Ignored options</h4> 319 320 321<p>Ignored options include the following:</p> 322 323<ul> 324 <li> <code>-dontoptimize // Jack does not optimize</code> 325 <li> <code>-dontpreverify // Jack does not preverify</code> 326 <li> <code>-skipnonpubliclibraryclasses</code> 327 <li> <code>-dontskipnonpubliclibraryclasses</code> 328 <li> <code>-dontskipnonpubliclibraryclassmembers</code> 329 <li> <code>-keepdirectories</code> 330 <li> <code>-target</code> 331 <li> <code>-forceprocessing</code> 332 <li> <code>-printusage</code> 333 <li> <code>-whyareyoukeeping</code> 334 <li> <code>-optimizations</code> 335 <li> <code>-optimizationpasses</code> 336 <li> <code>-assumenosideeffects</code> 337 <li> <code>-allowaccessmodification</code> 338 <li> <code>-mergeinterfacesaggressively</code> 339 <li> <code>-overloadaggressively</code> 340 <li> <code>-microedition</code> 341 <li> <code>-verbose</code> 342 <li> <code>-dontnote</code> 343 <li> <code>-dontwarn</code> 344 <li> <code>-ignorewarnings</code> 345 <li> <code>-printconfiguration</code> 346 <li> <code>-dump</code> 347</ul> 348 349<p class="note"><strong>Note:</strong> Other options will generate an error.</p> 350 351<h3 id=repackaging>Repackaging</h3> 352 353<p>JACK uses jarjar configuration files to do the repackaging.</p> 354 355<p class="note"><strong>Note:</strong> JACK is compatible with "rule" rule types, but is not compatible with "zap" or 356"keep" rule types. If you need "zap" or "keep" rule types please file a feature 357request with a description of how you use the feature in your app.</p> 358 359<h3 id=multidex_support>Multidex support</h3> 360 361 362<p>Since dex files are limited to 65K methods, apps with over 65K methods must be 363split into multiple dex files. (See <a href="http://developer.android.com/tools/building/multidex.html">‘Building Apps with Over 65K Methods’</a> for more information about multidex.)</p> 364 365<p>Jack offers native and legacy multidex support. </p> 366 367