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