Proguard obfuscation is breaking simplexml

谁说胖子不能爱 提交于 2019-11-28 21:18:40
Konstantin Pribluda

You already figured out that keeping annotation is a good idea. You may also try to add type parameter to @ElementList annotation - apparently there is a problem with generic type erasure and simplexml needs additional hint about type of elements in the list

you may also play around with -keepattributes Signature, *Annotation*:

The "Signature" attribute is required to be able to access generic types when compiling in JDK 5.0 and higher.

The problems when you use the SimpleXML library and obfuscate the code are the followings:

  1. You have to keep the "Annotations" and "Signatures" of your entities

    @Attribute(name = "retcode", required = true) private String _retcode;

  2. You have to keep the SimpleXML Library

  3. You have to prevent certain blocks of code be remove, for example, if the constructor of an entity is not used, proguard will remove it, but that method can be internally used by Simple XML Library

The proguard.cfg file may to be something like this:

# The following line may be different
-libraryjars <java.home>/lib/rt.jar(java/**,javax/**)

-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
# (3)Not remove unused code
-dontshrink

-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService
# (2)Simple XML
-keep public class org.simpleframework.**{ *; } 
-keep class org.simpleframework.xml.**{ *; } 
-keep class org.simpleframework.xml.core.**{ *; } 
-keep class org.simpleframework.xml.util.**{ *; }
# (1)Annotations and signatures
-keepattributes *Annotation*
-keepattributes Signature

-keepclasseswithmembernames class * {
    native <methods>;
}

-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.app.Activity {
   public void *(android.view.View);
}

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}

I use it in my own project and it works ;)

Use the official one from the project Subversion repository.

https://simple.svn.sourceforge.net/svnroot/simple/trunk/download/stream/proguard.pro

Right click your project in eclipse. Go to android -> run Lint.

Lint has the ability to check for proguard misconfigurations and may pick up, and explain your error.

Cristan

I kept getting the following errors:

can't find referenced class javax.xml.stream.XMLEventReader

can't find referenced class javax.xml.stream.events.XMLEvent

This is because these are part of the Java runtime (rt.jar) but not part of the Android runtime (android.jar), so ProGuard warns that something might be broken. This isn't actually a problem, so we can do the following:

-dontwarn javax.xml.stream.events.**

Source

Combined with the answer of zmicer, I get the following

-dontwarn javax.xml.stream.events.**

-keep public class org.simpleframework.** { *; }
-keep class org.simpleframework.xml.** { *; }
-keep class org.simpleframework.xml.core.** { *; }
-keep class org.simpleframework.xml.util.** { *; }

-keepattributes ElementList, Root

-keepclassmembers class * {
    @org.simpleframework.xml.* *;
}

Try adding this to your proguard file:

-keep public class org.simpleframework.** { *; }
-keep class org.simpleframework.xml.** { *; }
-keep class org.simpleframework.xml.core.** { *; }
-keep class org.simpleframework.xml.util.** { *; }

-keepattributes ElementList, Root

-keepclassmembers class * {
    @org.simpleframework.xml.* *;
}

This fixed it for me.

This exact addition to the proguard file worked for me:

-dontwarn javax.xml.stream.**

-keep public class org.simpleframework.** { *; }
-keep class org.simpleframework.xml.** { *; }
-keep class org.simpleframework.xml.core.** { *; }
-keep class org.simpleframework.xml.util.** { *; }

-keepattributes ElementList, Root

-keepclassmembers class * {
    @org.simpleframework.xml.* *;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!