Tuesday, December 31, 2013

ProGuard and JAXB

I encountered strange issue today when obfuscating Java code using ProGuard. Problem was that my code used JAXB (Java 7 + annotations and generics) and after obfuscation I was constantly getting exception

java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.ElementNSImpl cannot be cast to
I tried many different options:
<option>-keep public class my.jaxb.classes.** { public protected private *; }</option>

<option>-keepattributes *Annotation*</option>
but nothing helped (btw, you do have to use those two options from above, but they are not enough on their own, and it took me almost an hour to figure out what was happening).

Only after I added
 <option>-keepattributes Signature</option>
everything started behaving correctly.

Jaxb requires generics to be available to perform xml parsing and without this option ProGuard was not retaining that information after obfuscation. That was causing the exception above.

Hopefully this will save you some time.

ProGuard is a great tool, the only thing I found strange is that there is no official Maven plugin for it and also latest ProGuard releases can not be found in Maven central.