Using ProGuard with Android

flawyte picture flawyte · Jun 28, 2012 · Viewed 19.2k times · Source

I have problems dealing with ProGuard and Android.

I searched on the web for hours and found multiple ways to obsfuscate an Android application. For now I'm trying one that looks to be the easiest, so :

  • I created a config.cfg file in the root directory of my project
  • I added proguard.config=config.cfg in my project.properties
  • The I used the Eclipse export wizard to export & sign the .apk file

I got a message saying Proguard returned with error code 1. See console and in the console:

Proguard returned with error code 1. See console
Note: there were 3847 duplicate class definitions.
Warning: com.fasterxml.jackson.databind.ext.DOMSerializer: can't find referenced class org.w3c.dom.bootstrap.DOMImplementationRegistry
Warning: com.fasterxml.jackson.databind.ext.DOMSerializer: can't find referenced class org.w3c.dom.bootstrap.DOMImplementationRegistry
Warning: com.fasterxml.jackson.databind.ext.DOMSerializer: can't find referenced class org.w3c.dom.bootstrap.DOMImplementationRegistry
Warning: com.fasterxml.jackson.databind.ext.DOMSerializer: can't find referenced class org.w3c.dom.bootstrap.DOMImplementationRegistry
      You should check if you need to specify additional program jars.
Warning: there were 4 unresolved references to classes or interfaces.
         You may need to specify additional library jars (using '-libraryjars').
Error: Please correct the above warnings first.

Even if I use the config provided by the official Proguard website, or the default one I have errors.
Here is my custom config (myProject/config.cfg):

-injars      bin/classes
-injars      libs
-libraryjars "C:\Program Files\Android\android-sdk\platforms\android-13\android.jar"

-dontskipnonpubliclibraryclasses
-optimizationpasses 5
-printmapping map.txt
-flattenpackagehierarchy
-dontpreverify
-repackageclasses ''
-allowaccessmodification
-optimizations !code/simplification/arithmetic
-keepattributes *Annotation*

-keep public class * extends android.app.Application
-keep public class * extends android.app.Activity
-keep public class * extends android.app.PreferenceActivity
-keep public class * extends android.view.View
-keep public class * extends android.widget.BaseAdapter
-keep public class * implements android.view.View.OnTouchListener

-keep public class * extends android.view.View {
    public <init>(android.content.Context);
    public <init>(android.content.Context, android.util.AttributeSet);
    public <init>(android.content.Context, android.util.AttributeSet, int);
    public void set*(...);
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers class * extends android.content.Context {
   public void *(android.view.View);
   public void *(android.view.MenuItem);
}

-keepclassmembers class * implements android.os.Parcelable {
    static android.os.Parcelable$Creator CREATOR;
}

-keepclassmembers class **.R$* {
    public static <fields>;
}

I can't get it to work... any idea would be greatly appreciated !

NOTE : I use the Jackson JSON librairies that are stored in the libs folder of my project

Answer

Eric Lafortune picture Eric Lafortune · Jun 28, 2012

1) ProGuard manual > Troubleshooting > Note: duplicate definition of program/library class

The Android Ant/Eclipse builds already specify -injars/-libraryjars for you. If you specify them again in your configuration, ProGuard notes that they are duplicated. So don't specify -injars/-libraryjars.

2) ProGuard manual > Troubleshooting > Warning: can't find referenced class

org.w3c.dom.bootstrap.DOMImplementationRegistry is not present in the input code, yet com.fasterxml.jackson.databind.ext.DOMSerializer is using it. If your application works anyway, you can let ProGuard accept it with:

-dontwarn org.w3c.dom.bootstrap.DOMImplementationRegistry