Wednesday, December 19, 2012

Obfuscating to a small self contained library .jar

In the case you would like to create a library jar file, you could:

  • Run Proguard once on the library .jar file - this can make the library self-contained and prevent package name collisions.
  • Run Proguard once on the application that uses the library file.


Step by step:

  1. Ofuscation of library jar is based on http://proguard.sourceforge.net/index.html#manual/examples.html - A typical library.
  2. To make the library .jar small, list classes to be kept explicitely instead of -keep public class * {public protected *;}
    • -keep public class my.package1.* {*;}
    • -keep public class my.package2.* {*;}
  3. To make the library smaller, remove unused classes and obfuscate the used ones, omit -dontshrink
  4. To optimize and remove dead code, omit -dontoptimize
  5. In the case of "Unknown verification type [32] in stack map frame" problems when obfuscating the app, try disabling optimizing rules of the library according to http://proguard.sourceforge.net/index.html#manual/optimizations.html. Examples that can be tried until no more errors:
    1. -optimizations !class/merging/*, !field/*, !method/*, !code/*
    2. -optimizations  !code/*, !field/*
    3. -optimizations !field/*
    4. -optimizations !field/propagation/value
  6. Finally, repackage classes to your organizations domain (www.package.my):
    1. -allowaccessmodification
    2. -repackageclasses 'my.package'
You should now have a library .jar where all or most classes are obfuscated located under my.package.*.class