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