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