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<!-- 10if (window.self==window.top) 11 window.top.location.replace("../index.html#"+window.location.pathname+window.location.hash); 12else { 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 25preverifier. The shrinking step detects and removes unused classes, fields, 26methods, and attributes. The optimization step analyzes and optimizes the 27bytecode of the methods. The obfuscation step renames the remaining classes, 28fields, and methods using short meaningless names. These first steps make the 29code base smaller, more efficient, and harder to reverse-engineer. The final 30preverification step adds preverification information to the classes, which is 31required for Java Micro Edition and for Java 6 and higher. 32<p> 33Each of these steps is optional. For instance, ProGuard can also be used to 34just list dead code in an application, or to preverify class files for 35efficient 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 →</td> 60<td class="transparentblock">- optimize →</td> 61<td class="transparentblock">- obfuscate →</td> 62<td class="lightblock">Obfusc. code</td> 63<td class="transparentblock">- preverify →</td> 64</tr> 65 66<tr> 67<td class="darkblock">Library jars</td> 68<td colspan="7" class="transparentblock">------------------------------- (unchanged) -------------------------------→</td> 69<td class="darkblock">Library jars</td> 70</tr> 71 72</table> 73<p> 74 75ProGuard first reads the <b>input jars</b> (or wars, ears, zips, or 76directories). It then subsequently shrinks, optimizes, obfuscates, and 77preverifies them. You can optionally let ProGuard perform multiple 78optimization passes. ProGuard writes the processed results to one or 79more <b>output jars</b> (or wars, ears, zips, or directories). The input may 80contain resource files, whose names and contents can optionally be updated to 81reflect the obfuscated class names. 82<p> 83ProGuard requires the <b>library jars</b> (or wars, ears, zips, or 84directories) of the input jars to be specified. These are essentially the 85libraries that you would need for compiling the code. ProGuard uses them to 86reconstruct the class dependencies that are necessary for proper processing. 87The library jars themselves always remain unchanged. You should still put them 88in the class path of your final application. 89 90<h3>Entry points</h3> 91 92In order to determine which code has to be preserved and which code can be 93discarded or obfuscated, you have to specify one or more <i>entry points</i> to 94your code. These entry points are typically classes with main methods, applets, 95midlets, 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> 114The <a href="usage.html">Usage section</a> of this manual describes the 115necessary <a href="usage.html#keepoptions"><code>-keep</code> options</a> and 116the <a href="examples.html">Examples section</a> provides plenty of examples. 117 118<h3>Reflection</h3> 119 120Reflection and introspection present particular problems for any automatic 121processing of code. In ProGuard, classes or class members in your code that 122are created or invoked dynamically (that is, by name) have to be specified as 123entry points too. For example, <code>Class.forName()</code> constructs may 124refer to any class at run-time. It is generally impossible to compute which 125classes have to be preserved (with their original names), since the class 126names might be read from a configuration file, for instance. You therefore 127have to specify them in your ProGuard configuration, with the same 128simple <code>-keep</code> options. 129<p> 130However, 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 148The names of the classes and class members may of course be different, but the 149constructs should be literally the same for ProGuard to recognize them. The 150referenced classes and class members are preserved in the shrinking phase, and 151the string arguments are properly updated in the obfuscation phase. 152<p> 153Furthermore, ProGuard will offer some suggestions if keeping some classes or 154class members appears necessary. For example, ProGuard will note constructs 155like "<code>(SomeClass)Class.forName(variable).newInstance()</code>". These 156might be an indication that the class or interface <code>SomeClass</code> 157and/or its implementations may need to be preserved. You can then adapt your 158configuration accordingly. 159<p> 160For proper results, you should at least be somewhat familiar with the code 161that you are processing. Obfuscating code that performs a lot of reflection 162may require trial and error, especially without the necessary information 163about 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> 168Copyright © 2002-2013 169<a target="other" href="http://www.lafortune.eu/">Eric Lafortune</a>. 170</address> 171</body> 172</html> 173