Proguard obfuscation is breaking simplexml

后端 未结 7 822
耶瑟儿~
耶瑟儿~ 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: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 /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 ;
    }
    
    -keepclasseswithmembers class * {
        public (android.content.Context, android.util.AttributeSet);
    }
    
    -keepclasseswithmembers class * {
        public (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 ;)

提交回复
热议问题