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 Examples</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>Examples</h2> 23 24 Some typical useful configurations: 25 <ol> 26 <li><a href="#application">A typical application</a></li> 27 <li><a href="#applet">A typical applet</a></li> 28 <li><a href="#midlet">A typical midlet</a></li> 29 <li><a href="#jcapplet">A typical Java Card applet</a></li> 30 <li><a href="#xlet">A typical xlet</a></li> 31 <li><a href="#androidactivity">A simple Android activity</a></li> 32 <li><a href="#androidapplication">A complete Android application</a></li> 33 <li><a href="#library">A typical library</a></li> 34 <li><a href="#applications">All possible applications in the input jars</a></li> 35 <li><a href="#applets">All possible applets in the input jars</a></li> 36 <li><a href="#midlets">All possible midlets in the input jars</a></li> 37 <li><a href="#jcapplets">All possible Java Card applets in the input jars</a></li> 38 <li><a href="#xlets">All possible xlets in the input jars</a></li> 39 <li><a href="#servlets">All possible servlets in the input jars</a></li> 40 <li><a href="#scala">Scala applications with the Scala runtime</a></li> 41 <li><a href="#native">Processing native methods</a></li> 42 <li><a href="#callback">Processing callback methods</a></li> 43 <li><a href="#enumerations">Processing enumeration classes</a></li> 44 <li><a href="#serializable">Processing serializable classes</a></li> 45 <li><a href="#beans">Processing bean classes</a></li> 46 <li><a href="#annotations">Processing annotations</a></li> 47 <li><a href="#database">Processing database drivers</a></li> 48 <li><a href="#componentui">Processing ComponentUI classes</a></li> 49 <li><a href="#rmi">Processing RMI code</a></li> 50 <li><a href="#resourcefiles">Processing resource files</a></li> 51 <li><a href="#manifestfiles">Processing manifest files</a></li> 52 <li><a href="#stacktrace">Producing useful obfuscated stack traces</a></li> 53 <li><a href="#repackaging">Obfuscating package names</a></li> 54 <li><a href="#restructuring">Restructuring the output archives</a></li> 55 <li><a href="#filtering">Filtering the input and the output</a></li> 56 <li><a href="#multiple">Processing multiple applications at once</a></li> 57 <li><a href="#incremental">Incremental obfuscation</a></li> 58 <li><a href="#microedition">Preverifying class files for Java Micro Edition</a></li> 59 <li><a href="#upgrade">Upgrading class files to Java 6</a></li> 60 <li><a href="#deadcode">Finding dead code</a></li> 61 <li><a href="#structure">Printing out the internal structure of class files</a></li> 62 <li><a href="#annotated">Using annotations to configure ProGuard</a></li> 63 </ol> 64 65 You can find some sample configuration files in the <code>examples</code> 66 directory of the ProGuard distribution. 67 68 <h3><a name="application">A typical application</a></h3> 69 70 To shrink, optimize, and obfuscate a simple Java application, you typically 71 create a configuration file like <code>myconfig.pro</code>, which can be used 72 with 73 <pre> 74 bin/proguard @myconfig.pro 75 </pre> 76 <p> 77 The configuration file specifies the input, the output, and the entry points 78 of the application: 79 <pre> 80 -injars myapplication.jar 81 -outjars myapplication_out.jar 82 -libraryjars <java.home>/lib/rt.jar 83 -printmapping myapplication.map 84 85 -keep public class mypackage.MyMain { 86 public static void main(java.lang.String[]); 87 } 88 </pre> 89 <p> 90 Note the use of the <code><java.home></code> system property. ProGuard 91 automatically replaces it when parsing the file. 92 <p> 93 The <a href="usage.html#keep"><code>-keep</code></a> option specifies the 94 entry point of the application that has to be preserved. 95 The access modifiers <code>public</code> and <code>static</code> are not 96 really required in this case, since we know a priori that the specified class 97 and method have the proper access flags. It just looks more familiar this way. 98 <p> 99 Note that all type names are fully specified: 100 <code>mypackage.MyMain</code> and <code>java.lang.String[]</code>. 101 <p> 102 We're writing out an obfuscation mapping file with <a 103 href="usage.html#printmapping"><code>-printmapping</code></a>, for 104 de-obfuscating any stack traces later on, or for incremental obfuscation of 105 extensions. 106 <p> 107 We can further improve the results with a few additional options: 108 <pre> 109 -optimizationpasses 3 110 -overloadaggressively 111 -repackageclasses '' 112 -allowaccessmodification 113 </pre> 114 These options are not required; they just shave off some extra bytes from the 115 output jar, by performing up to 3 optimization passes, and by aggressively 116 obfuscating class members and <a href="#repackaging">package names</a>. 117 <p> 118 In general, you might need a few additional options for processing <a 119 href="#native">native methods</a>, <a href="#callback">callback methods</a>, 120 <a href="#enumerations">enumerations</a>, <a href="#serializable">serializable 121 classes</a>, <a href="#beans">bean classes</a>, <a 122 href="#annotations">annotations</a>, and <a href="#resourcefiles">resource 123 files</a>. 124 125 <h3><a name="applet">A typical applet</a></h3> 126 127 These options shrink, optimize, and obfuscate the applet 128 <code>mypackage.MyApplet</code>: 129 <pre> 130 -injars in.jar 131 -outjars out.jar 132 -libraryjars <java.home>/lib/rt.jar 133 134 -keep public class mypackage.MyApplet 135 </pre> 136 <p> 137 The typical applet methods will be preserved automatically, since 138 <code>mypackage.MyApplet</code> is an extension of the <code>Applet</code> 139 class in the library <code>rt.jar</code>. 140 <p> 141 If applicable, you should add options for processing <a href="#native">native 142 methods</a>, <a href="#callback">callback methods</a>, <a 143 href="#enumerations">enumerations</a>, <a href="#serializable">serializable 144 classes</a>, <a href="#beans">bean classes</a>, <a 145 href="#annotations">annotations</a>, and <a href="#resourcefiles">resource 146 files</a>. 147 148 <h3><a name="midlet">A typical midlet</a></h3> 149 150 These options shrink, optimize, obfuscate, and preverify the midlet 151 <code>mypackage.MyMIDlet</code>: 152 <pre> 153 -injars in.jar 154 -outjars out.jar 155 -libraryjars /usr/local/java/wtk2.5.2/lib/midpapi20.jar 156 -libraryjars /usr/local/java/wtk2.5.2/lib/cldcapi11.jar 157 -overloadaggressively 158 -repackageclasses '' 159 -allowaccessmodification 160 -microedition 161 162 -keep public class mypackage.MyMIDlet 163 </pre> 164 <p> 165 Note how we're now targeting the Java Micro Edition run-time environment of 166 <code>midpapi20.jar</code> and <code>cldcapi11.jar</code>, instead of the Java 167 Standard Edition run-time environment <code>rt.jar</code>. You can target 168 other JME environments by picking the appropriate jars. 169 <p> 170 The typical midlet methods will be preserved automatically, since 171 <code>mypackage.MyMIDlet</code> is an extension of the <code>MIDlet</code> 172 class in the library <code>midpapi20.jar</code>. 173 <p> 174 The <a href="usage.html#microedition"><code>-microedition</code></a> option 175 makes sure the class files are preverified for Java Micro Edition, producing 176 compact <code>StackMap</code> attributes. It is no longer necessary to run an 177 external preverifier. 178 <p> 179 Be careful if you do use the external <code>preverify</code> tool on a platform 180 with a case-insensitive filing system, such as Windows. Because this tool 181 unpacks your processed jars, you should then use ProGuard's <a 182 href="usage.html#dontusemixedcaseclassnames"><code>-dontusemixedcaseclassnames</code></a> 183 option. 184 <p> 185 If applicable, you should add options for processing <a href="#native">native 186 methods</a> and <a href="#resourcefiles">resource files</a>. 187 <p> 188 Note that you will still have to adapt the midlet jar size in the 189 corresponding jad file; ProGuard doesn't do that for you. 190 191 <h3><a name="jcapplet">A typical Java Card applet</a></h3> 192 193 These options shrink, optimize, and obfuscate the Java Card applet 194 <code>mypackage.MyApplet</code>: 195 <pre> 196 -injars in.jar 197 -outjars out.jar 198 -libraryjars /usr/local/java/javacard2.2.2/lib/api.jar 199 -dontwarn java.lang.Class 200 -overloadaggressively 201 -repackageclasses '' 202 -allowaccessmodification 203 204 -keep public class mypackage.MyApplet 205 </pre> 206 <p> 207 The configuration is very similar to the configuration for midlets, except that 208 it now targets the Java Card run-time environment. This environment doesn't 209 have java.lang.Class, so we're telling ProGuard not to worry about it. 210 211 <h3><a name="xlet">A typical xlet</a></h3> 212 213 These options shrink, optimize, and obfuscate the xlet 214 <code>mypackage.MyXlet</code>: 215 <pre> 216 -injars in.jar 217 -outjars out.jar 218 -libraryjars /usr/local/java/jtv1.1/javatv.jar 219 -libraryjars /usr/local/java/cdc1.1/lib/cdc.jar 220 -libraryjars /usr/local/java/cdc1.1/lib/btclasses.zip 221 -overloadaggressively 222 -repackageclasses '' 223 -allowaccessmodification 224 225 -keep public class mypackage.MyXlet 226 </pre> 227 <p> 228 The configuration is very similar to the configuration for midlets, except that 229 it now targets the CDC run-time environment with the Java TV API. 230 231 <h3><a name="androidactivity">A simple Android activity</a></h3> 232 233 These options shrink, optimize, and obfuscate the single Android 234 activity <code>mypackage.MyActivity</code>: 235 <pre> 236 -injars bin/classes 237 -outjars bin/classes-processed.jar 238 -libraryjars /usr/local/java/android-sdk/platforms/android-9/android.jar 239 240 -dontpreverify 241 -repackageclasses '' 242 -allowaccessmodification 243 -optimizations !code/simplification/arithmetic 244 245 -keep public class mypackage.MyActivity 246 </pre> 247 <p> 248 We're targeting the Android run-time and keeping the activity as an entry 249 point. 250 <p> 251 Preverification is irrelevant for the dex compiler and the Dalvik VM, so we 252 can switch it off with the 253 <a href="usage.html#dontpreverify"><code>-dontpreverify</code></a> option. 254 <p> 255 The <a href="usage.html#optimizations"><code>-optimizations</code></a> option 256 disables some arithmetic simplifications that Dalvik 1.0 and 1.5 can't handle. 257 Note that the Dalvik VM also can't 258 handle <a href="usage.html#overloadaggressively">aggressive overloading</a> 259 (of static fields). 260 <p> 261 If applicable, you should add options for processing <a href="#native">native 262 methods</a>, <a href="#callback">callback methods</a>, 263 <a href="#enumerations">enumerations</a>, 264 <a href="#annotations">annotations</a>, and 265 <a href="#resourcefiles">resource files</a>. 266 267 <h3><a name="androidapplication">A complete Android application</a></h3> 268 269 These options shrink, optimize, and obfuscate all public activities, services, 270 broadcast receivers, and content providers from the compiled classes and 271 external libraries: 272 <pre> 273 -injars bin/classes 274 -injars libs 275 -outjars bin/classes-processed.jar 276 -libraryjars /usr/local/java/android-sdk/platforms/android-9/android.jar 277 278 -dontpreverify 279 -repackageclasses '' 280 -allowaccessmodification 281 -optimizations !code/simplification/arithmetic 282 -keepattributes *Annotation* 283 284 -keep public class * extends android.app.Activity 285 -keep public class * extends android.app.Application 286 -keep public class * extends android.app.Service 287 -keep public class * extends android.content.BroadcastReceiver 288 -keep public class * extends android.content.ContentProvider 289 290 -keep public class * extends android.view.View { 291 public <init>(android.content.Context); 292 public <init>(android.content.Context, android.util.AttributeSet); 293 public <init>(android.content.Context, android.util.AttributeSet, int); 294 public void set*(...); 295 } 296 297 -keepclasseswithmembers class * { 298 public <init>(android.content.Context, android.util.AttributeSet); 299 } 300 301 -keepclasseswithmembers class * { 302 public <init>(android.content.Context, android.util.AttributeSet, int); 303 } 304 305 -keepclassmembers class * implements android.os.Parcelable { 306 static android.os.Parcelable$Creator CREATOR; 307 } 308 309 -keepclassmembers class **.R$* { 310 public static <fields>; 311 } 312 </pre> 313 <p> 314 Most importantly, we're keeping all fundamental classes that may be referenced 315 by the <code>AndroidManifest.xml</code> file of the application. If your 316 manifest file contains other classes and methods, you may have to specify 317 those as well. 318 <p> 319 We're keeping annotations, since they might be used by custom 320 <code>RemoteViews</code>. 321 <p> 322 We're keeping any custom <code>View</code> extensions and other classes with 323 typical constructors, since they might be referenced from XML layout files. 324 <p> 325 We're also keeping the required static fields in <code>Parcelable</code> 326 implementations, since they are accessed by introspection. 327 <p> 328 Finally, we're keeping the static fields of referenced inner classes of 329 auto-generated <code>R</code> classes, just in case your code is accessing 330 those fields by introspection. Note that the compiler already inlines 331 primitive fields, so ProGuard can generally remove all these classes entirely 332 anyway (because the classes are not referenced and therefore not required). 333 <p> 334 If you're using additional Google APIs, you'll have to specify 335 those as well, for instance: 336 <pre> 337 -libraryjars /usr/local/android-sdk/add-ons/google_apis-7_r01/libs/maps.jar 338 </pre> 339 <p> 340 If you're using Google's optional License Verification Library, you can 341 obfuscate its code along with your own code. You do have to preserve 342 its <code>ILicensingService</code> interface for the library to work: 343 <pre> 344 -keep public interface com.android.vending.licensing.ILicensingService 345 </pre> 346 <p> 347 If you're using the Android Compatibility library, you should add the 348 following line, to let ProGuard know it's ok that the library references some 349 classes that are not available in all versions of the API: 350 <pre> 351 -dontwarn android.support.** 352 </pre> 353 <p> 354 If applicable, you should add options for processing <a href="#native">native 355 methods</a>, <a href="#callback">callback methods</a>, 356 <a href="#enumerations">enumerations</a>, 357 and <a href="#resourcefiles">resource files</a>. You may also want to add 358 options for producing <a href="#stacktrace">useful stack traces</a>. You can 359 find a complete sample configuration in <code>examples/android.pro</code> in 360 the ProGuard distribution. 361 <p> 362 The build process of the Android SDK (version 2.3 and higher) already 363 integrates ProGuard by default. You only need to enable it (for release 364 builds) by adding <code>proguard.config=proguard.cfg</code> to the file 365 <code>build.properties</code>. In case of problems, you may want to check if 366 the automatically generated file <code>proguard.cfg</code> contains the 367 settings discussed above. The generated Ant build file already sets the input 368 and output files for you. 369 <p> 370 For more information, you can consult the official <a target="other" 371 href="http://developer.android.com/guide/developing/tools/proguard.html">Developer 372 Guide</a> in the Android SDK. 373 374 <h3><a name="library">A typical library</a></h3> 375 376 These options shrink, optimize, and obfuscate an entire library, keeping all 377 public and protected classes and class members, native method names, and 378 serialization code. The processed version of the library can then still be 379 used as such, for developing code based on its public API. 380 <pre> 381 -injars in.jar 382 -outjars out.jar 383 -libraryjars <java.home>/lib/rt.jar 384 -printmapping out.map 385 386 -keepparameternames 387 -renamesourcefileattribute SourceFile 388 -keepattributes Exceptions,InnerClasses,Signature,Deprecated, 389 SourceFile,LineNumberTable,*Annotation*,EnclosingMethod 390 391 -keep public class * { 392 public protected *; 393 } 394 395 -keepclassmembernames class * { 396 java.lang.Class class$(java.lang.String); 397 java.lang.Class class$(java.lang.String, boolean); 398 } 399 400 -keepclasseswithmembernames class * { 401 native <methods>; 402 } 403 404 -keepclassmembers enum * { 405 public static **[] values(); 406 public static ** valueOf(java.lang.String); 407 } 408 409 -keepclassmembers class * implements java.io.Serializable { 410 static final long serialVersionUID; 411 private static final java.io.ObjectStreamField[] serialPersistentFields; 412 private void writeObject(java.io.ObjectOutputStream); 413 private void readObject(java.io.ObjectInputStream); 414 java.lang.Object writeReplace(); 415 java.lang.Object readResolve(); 416 } 417 </pre> 418 <p> 419 This configuration should preserve everything we'll ever want to access in the 420 library. Only if there are any other non-public classes or methods that are 421 invoked dynamically, they should be specified using additional <a 422 href="usage.html#keep"><code>-keep</code></a> options. 423 <p> 424 The <a 425 href="usage.html#keepclassmembernames"><code>-keepclassmembernames</code></a> 426 option for the <code>class$</code> methods is not strictly necessary. These 427 methods are inserted by the <code>javac</code> compiler and the 428 <code>jikes</code> compiler respectively, in JDK 1.2 and older, to implement 429 the <code>.class</code> construct. ProGuard will automatically detect them and 430 deal with them, even when their names have been obfuscated. However, other 431 obfuscators may rely on the original method names. It may therefore be helpful 432 to preserve them, in case these other obfuscators are ever used for further 433 obfuscation of the library. 434 <p> 435 The "Exceptions" attribute has to be preserved, so the compiler knows which 436 exceptions methods may throw. 437 <p> 438 The "InnerClasses" attribute (or more precisely, its source name part) has to 439 be preserved too, for any inner classes that can be referenced from outside the 440 library. The <code>javac</code> compiler would be unable to find the inner 441 classes otherwise. 442 <p> 443 The "Signature" attribute is required to be able to access generic types when 444 compiling in JDK 5.0 and higher. 445 <p> 446 The <a href="usage.html#keepparameternames"><code>-keepparameternames</code></a> 447 option keeps the parameter names in the "LocalVariableTable" and 448 "LocalVariableTypeTable" attributes of public library methods. Some IDEs can 449 present these names to the developers who use the library. 450 <p> 451 Finally, we're keeping the "Deprecated" attribute and the attributes for 452 producing <a href="#stacktrace">useful stack traces</a>. 453 <p> 454 We've also added some options for for processing <a href="#native">native 455 methods</a>, <a href="#enumerations">enumerations</a>, <a 456 href="#serializable">serializable classes</a>, and <a 457 href="#annotations">annotations</a>, which are all discussed in their 458 respective examples. 459 460 <h3><a name="applications">All possible applications in the input jars</a></h3> 461 462 These options shrink, optimize, and obfuscate all public applications in 463 <code>in.jar</code>: 464 <pre> 465 -injars in.jar 466 -outjars out.jar 467 -libraryjars <java.home>/lib/rt.jar 468 -printseeds 469 470 -keepclasseswithmembers public class * { 471 public static void main(java.lang.String[]); 472 } 473 </pre> 474 <p> 475 Note the use of <a 476 href="usage.html#keepclasseswithmembers"><code>-keepclasseswithmembers</code></a>. 477 We don't want to preserve all classes, just all classes that have main 478 methods, and those methods. 479 <p> 480 The <a href="usage.html#printseeds"><code>-printseeds</code></a> option prints 481 out which classes exactly will be preserved, so we know for sure we're getting 482 what we want. 483 <p> 484 If applicable, you should add options for processing <a href="#native">native 485 methods</a>, <a href="#callback">callback methods</a>, <a 486 href="#enumerations">enumerations</a>, <a href="#serializable">serializable 487 classes</a>, <a href="#beans">bean classes</a>, <a 488 href="#annotations">annotations</a>, and <a href="#resourcefiles">resource 489 files</a>. 490 491 <h3><a name="applets">All possible applets in the input jars</a></h3> 492 493 These options shrink, optimize, and obfuscate all public applets in 494 <code>in.jar</code>: 495 <pre> 496 -injars in.jar 497 -outjars out.jar 498 -libraryjars <java.home>/lib/rt.jar 499 -printseeds 500 501 -keep public class * extends java.applet.Applet 502 </pre> 503 <p> 504 We're simply keeping all classes that extend the <code>Applet</code> class. 505 <p> 506 Again, the <a href="usage.html#printseeds"><code>-printseeds</code></a> option 507 prints out which applets exactly will be preserved. 508 <p> 509 If applicable, you should add options for processing <a href="#native">native 510 methods</a>, <a href="#callback">callback methods</a>, <a 511 href="#enumerations">enumerations</a>, <a href="#serializable">serializable 512 classes</a>, <a href="#beans">bean classes</a>, <a 513 href="#annotations">annotations</a>, and <a href="#resourcefiles">resource 514 files</a>. 515 516 <h3><a name="midlets">All possible midlets in the input jars</a></h3> 517 518 These options shrink, optimize, obfuscate, and preverify all public midlets in 519 <code>in.jar</code>: 520 <pre> 521 -injars in.jar 522 -outjars out.jar 523 -libraryjars /usr/local/java/wtk2.5.2/lib/midpapi20.jar 524 -libraryjars /usr/local/java/wtk2.5.2/lib/cldcapi11.jar 525 -overloadaggressively 526 -repackageclasses '' 527 -allowaccessmodification 528 -microedition 529 -printseeds 530 531 -keep public class * extends javax.microedition.midlet.MIDlet 532 </pre> 533 <p> 534 We're simply keeping all classes that extend the <code>MIDlet</code> class. 535 <p> 536 The <a href="usage.html#microedition"><code>-microedition</code></a> option 537 makes sure the class files are preverified for Java Micro Edition, producing 538 compact <code>StackMap</code> attributes. It is no longer necessary to run an 539 external preverifier. 540 <p> 541 Be careful if you do use the external <code>preverify</code> tool on a platform 542 with a case-insensitive filing system, such as Windows. Because this tool 543 unpacks your processed jars, you should then use ProGuard's <a 544 href="usage.html#dontusemixedcaseclassnames"><code>-dontusemixedcaseclassnames</code></a> 545 option. 546 <p> 547 The <a href="usage.html#printseeds"><code>-printseeds</code></a> option prints 548 out which midlets exactly will be preserved. 549 <p> 550 If applicable, you should add options for processing <a href="#native">native 551 methods</a> and <a href="#resourcefiles">resource files</a>. 552 <p> 553 Note that you will still have to adapt the midlet jar size in the 554 corresponding jad file; ProGuard doesn't do that for you. 555 556 <h3><a name="jcapplets">All possible Java Card applets in the input jars</a></h3> 557 558 These options shrink, optimize, and obfuscate all public Java Card applets in 559 <code>in.jar</code>: 560 <pre> 561 -injars in.jar 562 -outjars out.jar 563 -libraryjars /usr/local/java/javacard2.2.2/lib/api.jar 564 -dontwarn java.lang.Class 565 -overloadaggressively 566 -repackageclasses '' 567 -allowaccessmodification 568 -printseeds 569 570 -keep public class * implements javacard.framework.Applet 571 </pre> 572 <p> 573 We're simply keeping all classes that implement the <code>Applet</code> 574 interface. 575 <p> 576 The <a href="usage.html#printseeds"><code>-printseeds</code></a> option prints 577 out which applets exactly will be preserved. 578 579 <h3><a name="xlets">All possible xlets in the input jars</a></h3> 580 581 These options shrink, optimize, and obfuscate all public xlets in 582 <code>in.jar</code>: 583 <pre> 584 -injars in.jar 585 -outjars out.jar 586 -libraryjars /usr/local/java/jtv1.1/javatv.jar 587 -libraryjars /usr/local/java/cdc1.1/lib/cdc.jar 588 -libraryjars /usr/local/java/cdc1.1/lib/btclasses.zip 589 -overloadaggressively 590 -repackageclasses '' 591 -allowaccessmodification 592 -printseeds 593 594 -keep public class * implements javax.tv.xlet.Xlet 595 </pre> 596 <p> 597 We're simply keeping all classes that implement the <code>Xlet</code> interface. 598 <p> 599 The <a href="usage.html#printseeds"><code>-printseeds</code></a> option prints 600 out which xlets exactly will be preserved. 601 602 <h3><a name="servlets">All possible servlets in the input jars</a></h3> 603 604 These options shrink, optimize, and obfuscate all public servlets in 605 <code>in.jar</code>: 606 <pre> 607 -injars in.jar 608 -outjars out.jar 609 -libraryjars <java.home>/lib/rt.jar 610 -libraryjars /usr/local/java/servlet/servlet.jar 611 -printseeds 612 613 -keep public class * implements javax.servlet.Servlet 614 </pre> 615 <p> 616 Keeping all servlets is very similar to keeping all applets. The servlet API 617 is not part of the standard run-time jar, so we're specifying it as a library. 618 Don't forget to use the right path name. 619 <p> 620 We're then keeping all classes that implement the <code>Servlet</code> 621 interface. We're using the <code>implements</code> keyword because it looks 622 more familiar in this context, but it is equivalent to <code>extends</code>, 623 as far as ProGuard is concerned. 624 <p> 625 The <a href="usage.html#printseeds"><code>-printseeds</code></a> option prints 626 out which servlets exactly will be preserved. 627 <p> 628 If applicable, you should add options for processing <a href="#native">native 629 methods</a>, <a href="#callback">callback methods</a>, <a 630 href="#enumerations">enumerations</a>, <a href="#serializable">serializable 631 classes</a>, <a href="#beans">bean classes</a>, <a 632 href="#annotations">annotations</a>, and <a href="#resourcefiles">resource 633 files</a>. 634 635 <h3><a name="scala">Scala applications with the Scala runtime</a></h3> 636 637 These options shrink, optimize, and obfuscate all public Scala applications in 638 <code>in.jar</code>: 639 <pre> 640 -injars in.jar 641 -injars /usr/local/java/scala-2.9.1/lib/scala-library.jar 642 -outjars out.jar 643 -libraryjars <java.home>/lib/rt.jar 644 645 -dontwarn scala.** 646 647 -keepclasseswithmembers public class * { 648 public static void main(java.lang.String[]); 649 } 650 651 -keep class * implements org.xml.sax.EntityResolver 652 653 -keepclassmembers class * { 654 ** MODULE$; 655 } 656 657 -keepclassmembernames class scala.concurrent.forkjoin.ForkJoinPool { 658 long eventCount; 659 int workerCounts; 660 int runControl; 661 scala.concurrent.forkjoin.ForkJoinPool$WaitQueueNode syncStack; 662 scala.concurrent.forkjoin.ForkJoinPool$WaitQueueNode spareStack; 663 } 664 665 -keepclassmembernames class scala.concurrent.forkjoin.ForkJoinWorkerThread { 666 int base; 667 int sp; 668 int runState; 669 } 670 671 -keepclassmembernames class scala.concurrent.forkjoin.ForkJoinTask { 672 int status; 673 } 674 675 -keepclassmembernames class scala.concurrent.forkjoin.LinkedTransferQueue { 676 scala.concurrent.forkjoin.LinkedTransferQueue$PaddedAtomicReference head; 677 scala.concurrent.forkjoin.LinkedTransferQueue$PaddedAtomicReference tail; 678 scala.concurrent.forkjoin.LinkedTransferQueue$PaddedAtomicReference cleanMe; 679 } 680 </pre> 681 <p> 682 The configuration is essentially the same as 683 for <a href="#applications">processing applications</a>, because Scala is 684 compiled to ordinary Java bytecode. However, the example processes the Scala 685 runtime library as well. The processed jar can be an order of magnitude 686 smaller and a few times faster than the original code (for the Scala code 687 examples, for instance). 688 <p> 689 The <a href="usage.html#dontwarn"><code>-dontwarn</code></a> option tells 690 ProGuard not to complain about some artefacts in the Scala runtime, the way it 691 is compiled by the <code>scalac</code> compiler (at least in Scala 2.9.1 and 692 older). Note that this option should always be used with care. 693 <p> 694 The additional <a href="usage.html#keepoverview"><code>-keep</code></a> 695 options make sure that some classes and some fields that are accessed by means 696 of introspection are not removed or renamed. 697 <p> 698 If applicable, you should add options for processing <a href="#native">native 699 methods</a>, <a href="#callback">callback methods</a>, <a 700 href="#enumerations">enumerations</a>, <a href="#serializable">serializable 701 classes</a>, <a href="#beans">bean classes</a>, <a 702 href="#annotations">annotations</a>, and <a href="#resourcefiles">resource 703 files</a>. 704 <h3><a name="native">Processing native methods</a></h3> 705 706 If your application, applet, servlet, library, etc., contains native methods, 707 you'll want to preserve their names and their classes' names, so they can 708 still be linked to the native library. The following additional option will 709 ensure that: 710 <pre> 711 -keepclasseswithmembernames class * { 712 native <methods>; 713 } 714 </pre> 715 <p> 716 Note the use of <a 717 href="usage.html#keepclasseswithmembernames"><code>-keepclasseswithmembernames</code></a>. 718 We don't want to preserve all classes or all native methods; we just want to 719 keep the relevant names from being obfuscated. 720 <p> 721 ProGuard doesn't look at your native code, so it won't automatically preserve 722 the classes or class members that are invoked by the native code. These are 723 entry points, which you'll have to specify explicitly. <a 724 href="callback">Callback methods</a> are discussed below as a typical example. 725 726 <h3><a name="callback">Processing callback methods</a></h3> 727 728 If your application, applet, servlet, library, etc., contains callback 729 methods, which are called from external code (native code, scripts,...), 730 you'll want to preserve them, and probably their classes too. They are just 731 entry points to your code, much like, say, the main method of an application. 732 If they aren't preserved by other <code>-keep</code> options, something like 733 the following option will keep the callback class and method: 734 <pre> 735 -keep class mypackage.MyCallbackClass { 736 void myCallbackMethod(java.lang.String); 737 } 738 </pre> 739 <p> 740 This will preserve the given class and method from being removed or renamed. 741 742 <h3><a name="enumerations">Processing enumeration classes</a></h3> 743 744 If your application, applet, servlet, library, etc., contains enumeration 745 classes, you'll have to preserve some special methods. Enumerations were 746 introduced in Java 5. The java compiler translates enumerations into classes 747 with a special structure. Notably, the classes contain implementations of some 748 static methods that the run-time environment accesses by introspection (Isn't 749 that just grand? Introspection is the self-modifying code of a new 750 generation). You have to specify these explicitly, to make sure they aren't 751 removed or obfuscated: 752 <pre> 753 -keepclassmembers enum * { 754 public static **[] values(); 755 public static ** valueOf(java.lang.String); 756 } 757 </pre> 758 759 <h3><a name="serializable">Processing serializable classes</a></h3> 760 761 More complex applications, applets, servlets, libraries, etc., may contain 762 classes that are serialized. Depending on the way in which they are used, they 763 may require special attention: 764 <ul> 765 766 <li>Often, serialization is simply a means of transporting data, without 767 long-term storage. Classes that are shrunk and obfuscated should then 768 continue to function fine with the following additional options: 769 770 <pre> 771 -keepclassmembers class * implements java.io.Serializable { 772 private static final java.io.ObjectStreamField[] serialPersistentFields; 773 private void writeObject(java.io.ObjectOutputStream); 774 private void readObject(java.io.ObjectInputStream); 775 java.lang.Object writeReplace(); 776 java.lang.Object readResolve(); 777 } 778 </pre> 779 <p> 780 781 The <a 782 href="usage.html#keepclassmembers"><code>-keepclassmembers</code></a> 783 option makes sure that any serialization methods are kept. By using this 784 option instead of the basic <code>-keep</code> option, we're not 785 forcing preservation of <i>all</i> serializable classes, just preservation 786 of the listed members of classes that are actually used.</li> 787 788 <li>Sometimes, the serialized data are stored, and read back later into newer 789 versions of the serializable classes. One then has to take care the classes 790 remain compatible with their unprocessed versions and with future 791 processed versions. In such cases, the relevant classes will most likely 792 have <code>serialVersionUID</code> fields. The following options should 793 then be sufficient to ensure compatibility over time: 794 795 <pre> 796 -keepnames class * implements java.io.Serializable 797 798 -keepclassmembers class * implements java.io.Serializable { 799 static final long serialVersionUID; 800 private static final java.io.ObjectStreamField[] serialPersistentFields; 801 !static !transient <fields>; 802 private void writeObject(java.io.ObjectOutputStream); 803 private void readObject(java.io.ObjectInputStream); 804 java.lang.Object writeReplace(); 805 java.lang.Object readResolve(); 806 } 807 </pre> 808 <p> 809 810 The <code>serialVersionUID</code> and <code>serialPersistentFields</code> 811 lines makes sure those fields are preserved, if they are present. 812 The <code><fields></code> line preserves all non-static, 813 non-transient fields, with their original names. The introspection of the 814 serialization process and the de-serialization process will then find 815 consistent names.</li> 816 817 <li>Occasionally, the serialized data have to remain compatible, but the 818 classes involved lack <code>serialVersionUID</code> fields. I imagine the 819 original code will then be hard to maintain, since the serial version UID 820 is then computed from a list of features the serializable class. Changing 821 the class ever so slightly may change the computed serial version UID. The 822 list of features is specified in the section on <a 823 href="http://java.sun.com/javase/6/docs/platform/serialization/spec/class.html#4100">Stream 824 Unique Identifiers</a> of Sun's <a 825 href="http://java.sun.com/javase/6/docs/platform/serialization/spec/serialTOC.html">Java 826 Object Serialization Specification</a>. The following directives should at 827 least partially ensure compatibility with the original classes: 828 829 <pre> 830 -keepnames class * implements java.io.Serializable 831 832 -keepclassmembers class * implements java.io.Serializable { 833 static final long serialVersionUID; 834 private static final java.io.ObjectStreamField[] serialPersistentFields; 835 !static !transient <fields>; 836 !private <fields>; 837 !private <methods>; 838 private void writeObject(java.io.ObjectOutputStream); 839 private void readObject(java.io.ObjectInputStream); 840 java.lang.Object writeReplace(); 841 java.lang.Object readResolve(); 842 } 843 </pre> 844 <p> 845 846 The new options force preservation of the elements involved in the UID 847 computation. In addition, the user will have to manually specify all 848 interfaces of the serializable classes (using something like "<code>-keep 849 interface MyInterface</code>"), since these names are also used when 850 computing the UID. A fast but sub-optimal alternative would be simply 851 keeping all interfaces with "<code>-keep interface *</code>".</li> 852 853 </ul> 854 <p> 855 856 Note that the above options may preserve more classes and class members 857 than strictly necessary. For instance, a large number of classes may implement 858 the <code>Serialization</code> interface, yet only a small number may actually 859 ever be serialized. Knowing your application and tuning the configuration 860 often produces more compact results. 861 862 <h3><a name="beans">Processing bean classes</a></h3> 863 864 If your application, applet, servlet, library, etc., makes extensive use of 865 introspection on bean classes to find bean editor classes, or getter and 866 setter methods, then configuration may become painful. There's not much else 867 you can do than making sure the bean class names, or the getter and setter 868 names don't change. For instance: 869 <pre> 870 -keep public class mypackage.MyBean { 871 public void setMyProperty(int); 872 public int getMyProperty(); 873 } 874 875 -keep public class mypackage.MyBeanEditor 876 </pre> 877 <p> 878 If there are too many elements to list explicitly, wildcards in class names 879 and method signatures might be helpful. This example should encompasses all 880 possible setters and getters in classes in the package <code>mybeans</code>: 881 <pre> 882 -keep class mybeans.** { 883 void set*(***); 884 void set*(int, ***); 885 886 boolean is*(); 887 boolean is*(int); 888 889 *** get*(); 890 *** get*(int); 891 } 892 </pre> 893 <p> 894 The '<code>***</code>' wildcard matches any type (primitive or non-primitive, 895 array or non-array). The methods with the '<code>int</code>' arguments matches 896 properties that are lists. 897 898 <h3><a name="annotations">Processing annotations</a></h3> 899 900 If your application, applet, servlet, library, etc., uses annotations, you may 901 want to preserve them in the processed output. Annotations are represented by 902 attributes that have no direct effect on the execution of the code. However, 903 their values can be retrieved through introspection, allowing developers to 904 adapt the execution behavior accordingly. By default, ProGuard treats 905 annotation attributes as optional, and removes them in the obfuscation step. 906 If they are required, you'll have to specify this explicitly: 907 <pre> 908 -keepattributes *Annotation* 909 </pre> 910 <p> 911 For brevity, we're specifying a wildcarded attribute name, which will match 912 <code>RuntimeVisibleAnnotations</code>, 913 <code>RuntimeInvisibleAnnotations</code>, 914 <code>RuntimeVisibleParameterAnnotations</code>, 915 <code>RuntimeInvisibleParameterAnnotations</code>, and 916 <code>AnnotationDefault</code>. Depending on the purpose of the processed 917 code, you could refine this selection, for instance not keeping the run-time 918 invisible annotations (which are only used at compile-time). 919 <p> 920 Some code may make further use of introspection to figure out the enclosing 921 methods of anonymous inner classes. In that case, the corresponding attribute 922 has to be preserved as well: 923 <pre> 924 -keepattributes EnclosingMethod 925 </pre> 926 927 <h3><a name="database">Processing database drivers</a></h3> 928 929 Database drivers are implementations of the <code>Driver</code> interface. 930 Since they are often created dynamically, you may want to preserve any 931 implementations that you are processing as entry points: 932 <pre> 933 -keep class * implements java.sql.Driver 934 </pre> 935 <p> 936 This option also gets rid of the note that ProGuard prints out about 937 <code>(java.sql.Driver)Class.forName</code> constructs, if you are 938 instantiating a driver in your code (without necessarily implementing any 939 drivers yourself). 940 941 <h3><a name="componentui">Processing ComponentUI classes</a></h3> 942 943 Swing UI look and feels are implemented as extensions of the 944 <code>ComponentUI</code> class. For some reason, these have to contain a 945 static method <code>createUI</code>, which the Swing API invokes using 946 introspection. You should therefore always preserve the method as an entry 947 point, for instance like this: 948 <pre> 949 -keep class * extends javax.swing.plaf.ComponentUI { 950 public static javax.swing.plaf.ComponentUI createUI(javax.swing.JComponent); 951 } 952 </pre> 953 <p> 954 This option also keeps the classes themselves. 955 956 <h3><a name="rmi">Processing RMI code</a></h3> 957 958 Reportedly, the easiest way to handle RMI code is to process the code with 959 ProGuard first and then invoke the <code>rmic</code> tool. If that is not 960 possible, you may want to try something like this: 961 <pre> 962 -keepattributes Exceptions 963 964 -keep interface * extends java.rmi.Remote { 965 <methods>; 966 } 967 968 -keep class * implements java.rmi.Remote { 969 <init>(java.rmi.activation.ActivationID, java.rmi.MarshalledObject); 970 } 971 </pre> 972 <p> 973 The first <code>-keep</code> option keeps all your Remote interfaces and their 974 methods. The second one keeps all the implementations, along with their 975 particular RMI constructors, if any. 976 <p> 977 The <code>Exceptions</code> attribute has to be kept too, because the RMI 978 handling code performs introspection to check whether the method signatures 979 are compatible. 980 981 <h3><a name="resourcefiles">Processing resource files</a></h3> 982 983 If your application, applet, servlet, library, etc., contains resource files, 984 it may be necessary to adapt their names and/or their contents when the 985 application is obfuscated. The following two options can achieve this 986 automatically: 987 <pre> 988 -adaptresourcefilenames **.properties,**.gif,**.jpg 989 -adaptresourcefilecontents **.properties,META-INF/MANIFEST.MF 990 </pre> 991 <p> 992 The <a href="usage.html#adaptresourcefilenames">-adaptresourcefilenames</a> 993 option in this case renames properties files and image files in the processed 994 output, based on the obfuscated names of their corresponding class files (if 995 any). The <a 996 href="usage.html#adaptresourcefilecontents">-adaptresourcefilecontents</a> 997 option looks for class names in properties files and in the manifest file, and 998 replaces these names by the obfuscated names (if any). You'll probably want to 999 adapt the filters to suit your application. 1000 1001 <h3><a name="manifestfiles">Processing manifest files</a></h3> 1002 1003 As illustrated in the previous section, manifest files can be treated like 1004 ordinary resource files. ProGuard can adapt obfuscated class names in the 1005 files, but it won't make any other changes. If you want anything else, you 1006 should apply an external tool. For instance, if a manifest file contains 1007 signing information, you should sign the jar again after it has been 1008 processed. 1009 <p> 1010 If you're merging several input jars into a single output jar, you'll have to 1011 pick one, typically by specifying <a href="usage.html#filters">filters</a>: 1012 <pre> 1013 -injars in1.jar 1014 -injars in2.jar(!META-INF/MANIFEST.MF) 1015 -injars in3.jar(!META-INF/MANIFEST.MF) 1016 -outjars out.jar 1017 </pre> 1018 <p> 1019 The filters will let ProGuard copy the manifest file from the first jar and 1020 ignore any manifest files in the second and third input jars. Note that 1021 ProGuard will leave the order of the files in the jars unchanged; manifest 1022 files are not necessarily put first. 1023 1024 <h3><a name="stacktrace">Producing useful obfuscated stack traces</a></h3> 1025 1026 These options let obfuscated applications or libraries produce stack traces 1027 that can still be deciphered later on: 1028 <pre> 1029 -printmapping out.map 1030 1031 -renamesourcefileattribute SourceFile 1032 -keepattributes SourceFile,LineNumberTable 1033 </pre> 1034 <p> 1035 We're keeping all source file attributes, but we're replacing their values by 1036 the string "SourceFile". We could use any string. This string is already 1037 present in all class files, so it doesn't take up any extra space. If you're 1038 working with J++, you'll want to keep the "SourceDir" attribute as well. 1039 <p> 1040 We're also keeping the line number tables of all methods. 1041 <p> 1042 Whenever both of these attributes are present, the Java run-time environment 1043 will include line number information when printing out exception stack traces. 1044 <p> 1045 The information will only be useful if we can map the obfuscated names back to 1046 their original names, so we're saving the mapping to a file 1047 <code>out.map</code>. The information can then be used by the <a 1048 href="retrace/index.html">ReTrace</a> tool to restore the original stack trace. 1049 1050 <h3><a name="repackaging">Obfuscating package names</a></h3> 1051 1052 Package names can be obfuscated in various ways, with increasing levels of 1053 obfuscation and compactness. For example, consider the following classes: 1054 <pre> 1055 mycompany.myapplication.MyMain 1056 mycompany.myapplication.Foo 1057 mycompany.myapplication.Bar 1058 mycompany.myapplication.extra.FirstExtra 1059 mycompany.myapplication.extra.SecondExtra 1060 mycompany.util.FirstUtil 1061 mycompany.util.SecondUtil 1062 </pre> 1063 <p> 1064 Let's assume the class name <code>mycompany.myapplication.MyMain</code> is the 1065 main application class that is kept by the configuration. All other class names 1066 can be obfuscated. 1067 <p> 1068 By default, packages that contain classes that can't be renamed aren't renamed 1069 either, and the package hierarchy is preserved. This results in obfuscated 1070 class names like these: 1071 <pre> 1072 mycompany.myapplication.MyMain 1073 mycompany.myapplication.a 1074 mycompany.myapplication.b 1075 mycompany.myapplication.a.a 1076 mycompany.myapplication.a.b 1077 mycompany.a.a 1078 mycompany.a.b 1079 </pre> 1080 <p> 1081 The <a 1082 href="usage.html#flattenpackagehierarchy"><code>-flattenpackagehierarchy</code></a> 1083 option obfuscates the package names further, by flattening the package 1084 hierarchy of obfuscated packages: 1085 <pre> 1086 -flattenpackagehierarchy 'myobfuscated' 1087 </pre> 1088 <p> 1089 The obfuscated class names then look as follows: 1090 <pre> 1091 mycompany.myapplication.MyMain 1092 mycompany.myapplication.a 1093 mycompany.myapplication.b 1094 myobfuscated.a.a 1095 myobfuscated.a.b 1096 myobfuscated.b.a 1097 myobfuscated.b.b 1098 </pre> 1099 <p> 1100 Alternatively, the <a 1101 href="usage.html#repackageclasses"><code>-repackageclasses</code></a> option 1102 obfuscates the entire packaging, by combining obfuscated classes into a single 1103 package: 1104 <pre> 1105 -repackageclasses 'myobfuscated' 1106 </pre> 1107 The obfuscated class names then look as follows: 1108 <pre> 1109 mycompany.myapplication.MyMain 1110 mycompany.myapplication.a 1111 mycompany.myapplication.b 1112 myobfuscated.a 1113 myobfuscated.b 1114 myobfuscated.c 1115 myobfuscated.d 1116 </pre> 1117 <p> 1118 Additionally specifying the <a 1119 href="usage.html#allowaccessmodification"><code>-allowaccessmodification</code></a> 1120 option allows access permissions of classes and class members to 1121 be broadened, opening up the opportunity to repackage all obfuscated classes: 1122 <pre> 1123 -repackageclasses 'myobfuscated' 1124 -allowaccessmodification 1125 </pre> 1126 The obfuscated class names then look as follows: 1127 <pre> 1128 mycompany.myapplication.MyMain 1129 myobfuscated.a 1130 myobfuscated.b 1131 myobfuscated.c 1132 myobfuscated.d 1133 myobfuscated.e 1134 myobfuscated.f 1135 </pre> 1136 <p> 1137 The specified target package can always be the root package. For instance: 1138 <pre> 1139 -repackageclasses '' 1140 -allowaccessmodification 1141 </pre> 1142 The obfuscated class names are then the shortest possible names: 1143 <pre> 1144 mycompany.myapplication.MyMain 1145 a 1146 b 1147 c 1148 d 1149 e 1150 f 1151 </pre> 1152 <p> 1153 Note that not all levels of obfuscation of package names may be acceptable for 1154 all code. Notably, you may have to take into account that your application may 1155 contain <a href="#resourcefiles">resource files</a> that have to be adapted. 1156 1157 <h3><a name="restructuring">Restructuring the output archives</a></h3> 1158 1159 In simple applications, all output classes and resources files are merged into 1160 a single jar. For example: 1161 <pre> 1162 -injars classes 1163 -injars in1.jar 1164 -injars in2.jar 1165 -injars in3.jar 1166 -outjars out.jar 1167 </pre> 1168 <p> 1169 This configuration merges the processed versions of the files in the 1170 <code>classes</code> directory and the three jars into a single output jar 1171 <code>out.jar</code>. 1172 <p> 1173 If you want to preserve the structure of your input jars (and/or wars, ears, 1174 zips, or directories), you can specify an output directory (or a war, an ear, 1175 or a zip). For example: 1176 <pre> 1177 -injars in1.jar 1178 -injars in2.jar 1179 -injars in3.jar 1180 -outjars out 1181 </pre> 1182 <p> 1183 The input jars will then be reconstructed in the directory <code>out</code>, 1184 with their original names. 1185 <p> 1186 You can also combine archives into higher level archives. For example: 1187 <pre> 1188 -injars in1.jar 1189 -injars in2.jar 1190 -injars in3.jar 1191 -outjars out.war 1192 </pre> 1193 <p> 1194 The other way around, you can flatten the archives inside higher level 1195 archives into simple archives: 1196 <pre> 1197 -injars in.war 1198 -outjars out.jar 1199 </pre> 1200 <p> 1201 This configuration puts the processed contents of all jars inside 1202 <code>in.war</code> (plus any other contents of <code>in.war</code>) into 1203 <code>out.jar</code>. 1204 <p> 1205 If you want to combine input jars (and/or wars, ears, zips, or directories) 1206 into output jars (and/or wars, ears, zips, or directories), you can group the 1207 <a href="usage.html#injars"><code>-injars</code></a> and <a 1208 href="usage.html#outjars"><code>-outjars</code></a> options. For example: 1209 <pre> 1210 -injars base_in1.jar 1211 -injars base_in2.jar 1212 -injars base_in3.jar 1213 -outjars base_out.jar 1214 1215 -injars extra_in.jar 1216 -outjars extra_out.jar 1217 </pre> 1218 <p> 1219 This configuration puts the processed results of all <code>base_in*.jar</code> 1220 jars into <code>base_out.jar</code>, and the processed results of the 1221 <code>extra_in.jar</code> into <code>extra_out.jar</code>. Note that only the 1222 order of the options matters; the additional whitespace is just for clarity. 1223 <p> 1224 This grouping, archiving, and flattening can be arbitrarily complex. ProGuard 1225 always tries to package output archives in a sensible way, reconstructing the 1226 input entries as much as required. 1227 1228 <h3><a name="filtering">Filtering the input and the output</a></h3> 1229 1230 If you want even greater control, you can add 1231 <a href="usage.html#filters">filters</a> to the input and the output, 1232 filtering out zips, ears, wars, jars, and/or ordinary files. For example, if 1233 you want to disregard certain files from an input jar: 1234 <pre> 1235 -injars in.jar(!images/**) 1236 -outjars out.jar 1237 </pre> 1238 <p> 1239 This configuration removes any files in the <code>images</code> directory and 1240 its subdirectories. 1241 <p> 1242 Such filters can be convenient for avoiding warnings about duplicate files in 1243 the output. For example, only keeping the manifest file from a first input jar: 1244 <pre> 1245 -injars in1.jar 1246 -injars in2.jar(!META-INF/MANIFEST.MF) 1247 -injars in3.jar(!META-INF/MANIFEST.MF) 1248 -outjars out.jar 1249 </pre> 1250 <p> 1251 Another useful application is speeding up the processing by ProGuard, by 1252 disregarding a large number of irrelevant classes in the runtime library jar: 1253 <pre> 1254 -libraryjars <java.home>/lib/rt.jar(java/**,javax/**) 1255 </pre> 1256 <p> 1257 The filter makes ProGuard disregard <code>com.sun.**</code> classes, for 1258 instance , which don't affect the processing of ordinary applications. 1259 <p> 1260 It is also possible to filter the jars (and/or wars, ears, zips) themselves, 1261 based on their names. For example: 1262 <pre> 1263 -injars in(**/acme_*.jar;) 1264 -outjars out.jar 1265 </pre> 1266 <p> 1267 Note the semi-colon in the filter; the filter in front of it applies to jar 1268 names. In this case, only <code>acme_*.jar</code> jars are read from the 1269 directory <code>in</code> and its subdirectories. Filters for war names, ear 1270 names, and zip names can be prefixed with additional semi-colons. All types of 1271 filters can be combined. They are orthogonal. 1272 <p> 1273 On the other hand, you can also filter the output, in order to control what 1274 content goes where. For example: 1275 <pre> 1276 -injars in.jar 1277 -outjars code_out.jar(**.class) 1278 -outjars resources_out.jar 1279 </pre> 1280 <p> 1281 This configuration splits the processed output, sending <code>**.class</code> 1282 files to <code>code_out.jar</code>, and all remaining files to 1283 <code>resources_out.jar</code>. 1284 <p> 1285 Again, the filtering can be arbitrarily complex, especially when combined with 1286 grouping input and output. 1287 1288 <h3><a name="multiple">Processing multiple applications at once</a></h3> 1289 1290 You can process several dependent or independent applications (or applets, 1291 midlets,...) in one go, in order to save time and effort. ProGuard's input and 1292 output handling offers various ways to keep the output nicely structured. 1293 <p> 1294 The easiest way is to specify your input jars (and/or wars, ears, zips, and 1295 directories) and a single output directory. ProGuard will then reconstruct the 1296 input in this directory, using the original jar names. For example, showing 1297 just the input and output options: 1298 <pre> 1299 -injars application1.jar 1300 -injars application2.jar 1301 -injars application3.jar 1302 -outjars processed_applications 1303 </pre> 1304 <p> 1305 After processing, the directory <code>processed_applications</code> will 1306 contain processed versions of application jars, with their original names. 1307 1308 <h3><a name="incremental">Incremental obfuscation</a></h3> 1309 1310 After having <a href="#application">processed an application</a>, e.g. 1311 ProGuard itself, you can still incrementally add other pieces of code that 1312 depend on it, e.g. the ProGuard GUI: 1313 <pre> 1314 -injars proguardgui.jar 1315 -outjars proguardgui_out.jar 1316 -injars proguard.jar 1317 -outjars proguard_out.jar 1318 -libraryjars <java.home>/lib/rt.jar 1319 -applymapping proguard.map 1320 1321 -keep public class proguard.gui.ProGuardGUI { 1322 public static void main(java.lang.String[]); 1323 } 1324 </pre> 1325 <p> 1326 We're reading both unprocessed jars as input. Their processed contents will go 1327 to the respective output jars. The <a 1328 href="usage.html#applymapping"><code>-applymapping</code></a> option then 1329 makes sure the ProGuard part of the code gets the previously produced 1330 obfuscation mapping. The final application will consist of the obfuscated 1331 ProGuard jar and the additional obfuscated GUI jar. 1332 <p> 1333 The added code in this example is straightforward; it doesn't affect the 1334 original code. The <code>proguard_out.jar</code> will be identical to the one 1335 produced in the initial processing step. If you foresee adding more complex 1336 extensions to your code, you should specify the options <a 1337 href="usage.html#useuniqueclassmembernames"><code>-useuniqueclassmembernames</code></a>, 1338 <a href="usage.html#dontshrink"><code>-dontshrink</code></a>, and <a 1339 href="usage.html#dontoptimize"><code>-dontoptimize</code></a> <i>in the 1340 original processing step</i>. These options ensure that the obfuscated base 1341 jar will always remain usable without changes. You can then specify the base 1342 jar as a library jar: 1343 <pre> 1344 -injars proguardgui.jar 1345 -outjars proguardgui_out.jar 1346 -libraryjars proguard.jar 1347 -libraryjars <java.home>/lib/rt.jar 1348 -applymapping proguard.map 1349 1350 -keep public class proguard.gui.ProGuardGUI { 1351 public static void main(java.lang.String[]); 1352 } 1353 </pre> 1354 1355 <h3><a name="microedition">Preverifying class files for Java Micro Edition</a></h3> 1356 1357 Even if you're not interested in shrinking, optimizing, and obfuscating your 1358 midlets, as shown in the <a href="#midlets">midlets example</a>, you can still 1359 use ProGuard to preverify the class files for Java Micro Edition. ProGuard 1360 produces slightly more compact results than the traditional external 1361 preverifier. 1362 <pre> 1363 -injars in.jar 1364 -outjars out.jar 1365 -libraryjars /usr/local/java/wtk2.5.2/lib/midpapi20.jar 1366 -libraryjars /usr/local/java/wtk2.5.2/lib/cldcapi11.jar 1367 1368 -dontshrink 1369 -dontoptimize 1370 -dontobfuscate 1371 1372 -microedition 1373 </pre> 1374 <p> 1375 We're not processing the input, just making sure the class files are 1376 preverified by targeting them at Java Micro Edition with the <a 1377 href="usage.html#microedition"><code>-microedition</code></a> option. Note 1378 that we don't need any <code>-keep</code> options to specify entry points; all 1379 class files are simply preverified. 1380 1381 <h3><a name="upgrade">Upgrading class files to Java 6</a></h3> 1382 1383 The following options upgrade class files to Java 6, by updating their 1384 internal version numbers and preverifying them. The class files can then be 1385 loaded more efficiently by the Java 6 Virtual Machine. 1386 <pre> 1387 -injars in.jar 1388 -outjars out.jar 1389 -libraryjars <java.home>/lib/rt.jar 1390 1391 -dontshrink 1392 -dontoptimize 1393 -dontobfuscate 1394 1395 -target 1.6 1396 </pre> 1397 <p> 1398 We're not processing the input, just retargeting the class files with the <a 1399 href="usage.html#target"><code>-target</code></a> option. They will 1400 automatically be preverified for Java 6 as a result. Note that we don't need 1401 any <code>-keep</code> options to specify entry points; all class files are 1402 simply updated and preverified. 1403 1404 <h3><a name="deadcode">Finding dead code</a></h3> 1405 1406 These options list unused classes, fields, and methods in the application 1407 <code>mypackage.MyApplication</code>: 1408 <pre> 1409 -injars in.jar 1410 -libraryjars <java.home>/lib/rt.jar 1411 1412 -dontoptimize 1413 -dontobfuscate 1414 -dontpreverify 1415 -printusage 1416 1417 -keep public class mypackage.MyApplication { 1418 public static void main(java.lang.String[]); 1419 } 1420 </pre> 1421 <p> 1422 We're not specifying an output jar, just printing out some results. We're 1423 saving some processing time by skipping the other processing steps. 1424 <p> 1425 The java compiler inlines primitive constants and String constants 1426 (<code>static final</code> fields). ProGuard would therefore list such fields 1427 as not being used in the class files that it analyzes, even if they <i>are</i> 1428 used in the source files. We can add a <a 1429 href="usage.html#keepclassmembers"><code>-keepclassmembers</code></a> option 1430 that keeps those fields a priori, in order to avoid having them listed: 1431 <pre> 1432 -keepclassmembers class * { 1433 static final % *; 1434 static final java.lang.String *; 1435 } 1436 </pre> 1437 1438 <h3><a name="structure">Printing out the internal structure of class files</a></h3> 1439 1440 These options print out the internal structure of all class files in the input 1441 jar: 1442 <pre> 1443 -injars in.jar 1444 1445 -dontshrink 1446 -dontoptimize 1447 -dontobfuscate 1448 -dontpreverify 1449 1450 -dump 1451 </pre> 1452 <p> 1453 Note how we don't need to specify the Java run-time jar, because we're not 1454 processing the input jar at all. 1455 1456 <h3><a name="annotated">Using annotations to configure ProGuard</a></h3> 1457 1458 The traditional ProGuard configuration allows to keep a clean separation 1459 between the code and the configuration for shrinking, optimization, and 1460 obfuscation. However, it is also possible to define specific annotations, 1461 and then annotate the code to configure the processing. 1462 <p> 1463 You can find a set of such predefined annotations in the directory 1464 <code>examples/annotations/lib</code> in the ProGuard distribution. 1465 The annotation classes are defined in <code>annotations.jar</code>. The 1466 corresponding ProGuard configuration (or meta-configuration, if you prefer) 1467 is specified in <code>annotations.pro</code>. With these files, you can start 1468 annotating your code. For instance, a java source file 1469 <code>Application.java</code> can be annotated as follows: 1470 <pre> 1471 @KeepApplication 1472 public class Application { 1473 .... 1474 } 1475 </pre> 1476 <p> 1477 The ProGuard configuration file for the application can then be simplified by 1478 leveraging off these annotations: 1479 <pre> 1480 -injars in.jar 1481 -outjars out.jar 1482 -libraryjars <java.home>/lib/rt.jar 1483 1484 -include lib/annotations.pro 1485 </pre> 1486 <p> 1487 The annotations are effectively replacing the application-dependent 1488 <code>-keep</code> options. You may still wish to add traditional 1489 <code>-keep</code> options for processing <a href="#native">native 1490 methods</a>, <a href="#enumerations">enumerations</a>, <a 1491 href="#serializable">serializable classes</a>, and <a 1492 href="#annotations">annotations</a>. 1493 <p> 1494 The directory <code>examples/annotations</code> contains more examples that 1495 illustrate some of the possibilities. 1496 1497 <hr /> 1498 <noscript><div><a target="_top" href="../index.html" class="button">Show menu</a></div></noscript> 1499 <address> 1500 Copyright © 2002-2011 1501 <a target="other" href="http://www.lafortune.eu/">Eric Lafortune</a>. 1502 </address> 1503 </body> 1504 </html> 1505