Proguard obfuscation is breaking simplexml

后端 未结 7 782
耶瑟儿~
耶瑟儿~ 2020-12-14 18:01

I am using simplexml in my android project, and everything works fine until I obfuscate the code. Then, errors start pouring in.

Part of the XML is as follows:

相关标签:
7条回答
  • 2020-12-14 18:05

    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.

    0 讨论(0)
  • 2020-12-14 18:17

    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.* *;
    }
    
    0 讨论(0)
  • 2020-12-14 18:18

    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 ;)

    0 讨论(0)
  • 2020-12-14 18:21

    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.

    0 讨论(0)
  • 2020-12-14 18:21

    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.* *;
    }
    
    0 讨论(0)
  • 2020-12-14 18:25

    Use the official one from the project Subversion repository.

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

    0 讨论(0)
提交回复
热议问题