Home | History | Annotate | Download | only in manual
      1 <!doctype html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
      2 <html>
      3 <head>
      4 <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
      5 <meta http-equiv="content-style-type" content="text/css">
      6 <link rel="stylesheet" type="text/css" href="style.css">
      7 <title>ProGuard Introduction</title>
      8 <script type="text/javascript" language="JavaScript">
      9 <!--
     10 if (window.self==window.top)
     11   window.top.location.replace("../index.html#"+window.location.pathname+window.location.hash);
     12 else {
     13   var hash="#"+window.location.pathname.replace(window.top.location.pathname.replace("index.html", ""), "");
     14   if (window.top.location.hash!=hash)
     15     window.top.location.hash=hash;
     16 }
     17 //-->
     18 </script>
     19 </head>
     20 <body>
     21 
     22 <h2>Introduction</h2>
     23 
     24 <b>ProGuard</b> is a Java class file shrinker, optimizer, obfuscator, and
     25 preverifier. The shrinking step detects and removes unused classes, fields,
     26 methods, and attributes. The optimization step analyzes and optimizes the
     27 bytecode of the methods. The obfuscation step renames the remaining classes,
     28 fields, and methods using short meaningless names. These first steps make the
     29 code base smaller, more efficient, and harder to reverse-engineer. The final
     30 preverification step adds preverification information to the classes, which is
     31 required for Java Micro Edition and for Java 6 and higher.
     32 <p>
     33 Each of these steps is optional. For instance, ProGuard can also be used to
     34 just list dead code in an application, or to preverify class files for
     35 efficient use in Java 6.
     36 <p>
     37 
     38 <table class="diagram" align="center">
     39 
     40 <tr>
     41 <td rowspan="4" class="lightblock">Input jars</td>
     42 <td colspan="8" class="transparentblock"></td>
     43 </tr>
     44 
     45 <tr>
     46 <td rowspan="2" class="transparentblock"></td>
     47 <td rowspan="3" class="lightblock">Shrunk code</td>
     48 <td colspan="6" class="transparentblock"></td>
     49 </tr>
     50 
     51 <tr>
     52 <td             class="transparentblock"></td>
     53 <td rowspan="2" class="lightblock">Optim. code</td>
     54 <td colspan="3" class="transparentblock"></td>
     55 <td rowspan="2" class="lightblock">Output jars</td>
     56 </tr>
     57 
     58 <tr>
     59 <td             class="transparentblock">- shrink &rarr;</td>
     60 <td             class="transparentblock">- optimize &rarr;</td>
     61 <td             class="transparentblock">- obfuscate &rarr;</td>
     62 <td             class="lightblock">Obfusc. code</td>
     63 <td             class="transparentblock">- preverify &rarr;</td>
     64 </tr>
     65 
     66 <tr>
     67 <td             class="darkblock">Library jars</td>
     68 <td colspan="7" class="transparentblock">------------------------------- (unchanged) -------------------------------&rarr;</td>
     69 <td             class="darkblock">Library jars</td>
     70 </tr>
     71 
     72 </table>
     73 <p>
     74 
     75 ProGuard first reads the <b>input jars</b> (or wars, ears, zips, or
     76 directories). It then subsequently shrinks, optimizes, obfuscates, and
     77 preverifies them. You can optionally let ProGuard perform multiple
     78 optimization passes. ProGuard writes the processed results to one or
     79 more <b>output jars</b> (or wars, ears, zips, or directories). The input may
     80 contain resource files, whose names and contents can optionally be updated to
     81 reflect the obfuscated class names.
     82 <p>
     83 ProGuard requires the <b>library jars</b> (or wars, ears, zips, or
     84 directories) of the input jars to be specified. These are essentially the
     85 libraries that you would need for compiling the code. ProGuard uses them to
     86 reconstruct the class dependencies that are necessary for proper processing.
     87 The library jars themselves always remain unchanged. You should still put them
     88 in the class path of your final application.
     89 
     90 <h3>Entry points</h3>
     91 
     92 In order to determine which code has to be preserved and which code can be
     93 discarded or obfuscated, you have to specify one or more <i>entry points</i> to
     94 your code. These entry points are typically classes with main methods, applets,
     95 midlets, activities, etc.
     96 <ul>
     97 <li>In the <b>shrinking step</b>, ProGuard starts from these seeds and
     98     recursively determines which classes and class members are used. All other
     99     classes and class members are discarded.</li>
    100 
    101 <li>In the <b>optimization step</b>, ProGuard further optimizes the code.
    102     Among other optimizations, classes and methods that are not entry points
    103     can be made private, static, or final, unused parameters can be removed,
    104     and some methods may be inlined.</li>
    105 
    106 <li>In the <b>obfuscation step</b>, ProGuard renames classes and class members
    107     that are not entry points. In this entire process, keeping the entry
    108     points ensures that they can still be accessed by their original names.</li>
    109 
    110 <li>The <b>preverification step</b> is the only step that doesn't have to know
    111     the entry points.</li>
    112 </ul>
    113 <p>
    114 The <a href="usage.html">Usage section</a> of this manual describes the
    115 necessary <a href="usage.html#keepoptions"><code>-keep</code> options</a> and
    116 the <a href="examples.html">Examples section</a> provides plenty of examples.
    117 
    118 <h3>Reflection</h3>
    119 
    120 Reflection and introspection present particular problems for any automatic
    121 processing of code. In ProGuard, classes or class members in your code that
    122 are created or invoked dynamically (that is, by name) have to be specified as
    123 entry points too. For example, <code>Class.forName()</code> constructs may
    124 refer to any class at run-time. It is generally impossible to compute which
    125 classes have to be preserved (with their original names), since the class
    126 names might be read from a configuration file, for instance. You therefore
    127 have to specify them in your ProGuard configuration, with the same
    128 simple <code>-keep</code> options.
    129 <p>
    130 However, ProGuard will already detect and handle the following cases for you:
    131 
    132 <ul>
    133 <li><code>Class.forName("SomeClass")</code></li>
    134 <li><code>SomeClass.class</code></li>
    135 <li><code>SomeClass.class.getField("someField")</code></li>
    136 <li><code>SomeClass.class.getDeclaredField("someField")</code></li>
    137 <li><code>SomeClass.class.getMethod("someMethod", new Class[] {})</code></li>
    138 <li><code>SomeClass.class.getMethod("someMethod", new Class[] { A.class })</code></li>
    139 <li><code>SomeClass.class.getMethod("someMethod", new Class[] { A.class, B.class })</code></li>
    140 <li><code>SomeClass.class.getDeclaredMethod("someMethod", new Class[] {})</code></li>
    141 <li><code>SomeClass.class.getDeclaredMethod("someMethod", new Class[] { A.class })</code></li>
    142 <li><code>SomeClass.class.getDeclaredMethod("someMethod", new Class[] { A.class, B.class })</code></li>
    143 <li><code>AtomicIntegerFieldUpdater.newUpdater(SomeClass.class, "someField")</code></li>
    144 <li><code>AtomicLongFieldUpdater.newUpdater(SomeClass.class, "someField")</code></li>
    145 <li><code>AtomicReferenceFieldUpdater.newUpdater(SomeClass.class, SomeType.class, "someField")</code></li>
    146 </ul>
    147 
    148 The names of the classes and class members may of course be different, but the
    149 constructs should be literally the same for ProGuard to recognize them. The
    150 referenced classes and class members are preserved in the shrinking phase, and
    151 the string arguments are properly updated in the obfuscation phase.
    152 <p>
    153 Furthermore, ProGuard will offer some suggestions if keeping some classes or
    154 class members appears necessary. For example, ProGuard will note constructs
    155 like "<code>(SomeClass)Class.forName(variable).newInstance()</code>". These
    156 might be an indication that the class or interface <code>SomeClass</code>
    157 and/or its implementations may need to be preserved. You can then adapt your
    158 configuration accordingly.
    159 <p>
    160 For proper results, you should at least be somewhat familiar with the code
    161 that you are processing. Obfuscating code that performs a lot of reflection
    162 may require trial and error, especially without the necessary information
    163 about the internals of the code.
    164 
    165 <hr />
    166 <noscript><div><a target="_top" href="../index.html" class="button">Show menu</a></div></noscript>
    167 <address>
    168 Copyright &copy; 2002-2013
    169 <a target="other" href="http://www.lafortune.eu/">Eric Lafortune</a>.
    170 </address>
    171 </body>
    172 </html>
    173