Obfuscate Dagger Code with ProGuard

Since upgraded to Dagger 1.1.0, the original used obfuscator yGuard was not happy anymore due to “$$” in the Dagger generated adapter class names.

Switching to ProGuard (4.10) can fix the problem. Here is the ProGuard configuration that works for the Coffee example code.

-injars C:\Users\liuzy163\Downloads\dagger-master\dagger-master\examples\simple\target\classes
-outjars C:\Users\liuzy163\Downloads\dagger-master\dagger-master\examples\simple\target\classes_obfuscated

-libraryjars C:\Users\liuzy163\Downloads\javax.inject-1.jar
-libraryjars C:\Users\liuzy163\Downloads\dagger-1.1.0.jar
-libraryjars C:\Program Files\Java\jre7\lib\rt.jar
-libraryjars C:\Program Files\Java\jre7\lib\jsse.jar

-keepattributes *Annotation*

-keepclassmembers,allowobfuscation class * {
@javax.inject.* *;
@dagger.* *;

-keep class **$$ModuleAdapter
-keep class **$$InjectAdapter
-keep class **$$StaticInjection
-keep class coffee.CoffeeApp{
public static void main(java.lang.String[]);
-keepnames !abstract class coffee.*
-keepnames class dagger.Lazy

-printmapping C:\mapping.txt
-printseeds C:\seeds.txt

For real projects, more classes or members may need to be kept. You should pay attention to the following items:

  • classes/methods uses reflection
  • runtime annotation
  • servlets/RPC classes, i.e. GWT remote service classes
  • interfaces returned by Dagger providers
  • abstract classes inherited by injectable classes
  • enum values() and valueOf() methods

These classes can be added to the proguard configuration or annotated, depend on your code maintenance strategy and project build ways.

This entry was posted in Java, Programming, Tool and Debug and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s