Proguard混淆正在打破simplexml

我在我的android项目中使用simplexml,一切正常,直到我混淆代码。 然后,错误开始涌入。

部分XML如下所示:

<categories success="true"> <category id="102" caption="Magazin" parent="0" num_mags="114" > <category id="15" caption="Kunst" parent="102" num_mags="13" > <category id="17" caption="Design" parent="15" num_mags="10" ></category> <category id="18" caption="Haute+Couture" parent="15" num_mags="2" > ... 

我有两个类:CategoryItemList:

 @Root(name = "categories") public class CategoryItemList { private final List<CategoryItem> mCategoryItems; /** * Create a new category items list. * * @param categoryItems the list of category items */ public CategoryItemList(@ElementList(name = "category", inline = true) final List<CategoryItem> categoryItems) { mCategoryItems = categoryItems; } @ElementList(name = "category", inline = true) public List<CategoryItem> getCategoryItems() { return mCategoryItems; } } 

和CategoryItem:

 @Root(name = "category") public class CategoryItem { private final int mId; private final String mCaption; private final int mParent; private final int mNumberOfMagazines; private final ArrayList<CategoryItem> mSubCategoryItems; /** * Creating a new category item. * * @param id the category id * @param caption the name of category * @param parent the parent category * @param numMags the number of magazines from that category */ public CategoryItem(@Attribute(name = "id") final int id, @Attribute(name = "caption") final String caption, @Attribute(name = "parent") final int parent, @Attribute(name = "num_mags") final int numMags, @ElementList(name = "category", inline = true, required = false) final ArrayList<CategoryItem> subCategoryItems) { mId = id; mCaption = caption; mParent = parent; mNumberOfMagazines = numMags; mSubCategoryItems = subCategoryItems; } @Attribute(name = "id") public int getId() { return mId; } @Attribute(name = "caption") public String getCaption() { String categoryName = null; try { categoryName = URLDecoder.decode(mCaption, "UTF-8"); } catch (final UnsupportedEncodingException e) { e.printStackTrace(); } return categoryName; } @Attribute(name = "parent") public int getParentId() { return mParent; } @Attribute(name = "num_mags") public int getNumbersOfMagazines() { return mNumberOfMagazines; } @ElementList(name = "category", inline = true, required = false) public ArrayList<CategoryItem> getSubCategory() { return mSubCategoryItems; } } 

现在,当我混淆代码时,如果我省略“-keepattributes Annotation ”,则得到一个PersistenceException:Constructor与类不匹配。

如果我包含它,我会得到一个“无法确定通用types的构造函数的参数1”exception,所有这些在运行时。
正如你所看到的那样,名字就在那里,我试图让整个class级都抱着他们,一切都无济于事。

我如何configurationProguard使用simplexml?

编辑:我的proguard.cfg文件如下:(这是一个塞满了我试过的所有东西,但这是当前版本)

 -dontusemixedcaseclassnames -dontskipnonpubliclibraryclasses -dontpreverify -verbose -printseeds -dontoptimize -keepattributes *Annotation* -keepattributes EnclosingMethod -libraryjars <java.home>/lib/rt.jar (javax/xml/stream/** ) -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 -keepclasseswithmembers 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 enum * { public static **[] values(); public static ** valueOf(java.lang.String); } -keep class * implements android.os.Parcelable { public static final android.os.Parcelable$Creator *; } -dontwarn android.support.**,de.greenrobot.**,org.simpleframework.xml.** -keep class com.crittercism.**{ *; } -keepclassmembernames class com.crittercism.**{ *; } -keepclasseswithmembers class com.crittercism.**{ *; } -keep class org.simpleframework.**{ *; } -keepclassmembernames class org.simpleframework.**{ *; } -keepclasseswithmembers class org.simpleframework.**{ *; } -keep class crittercism.android.** -keepclassmembers public class com.crittercism.*{ *;} -keep public class database.** { public static <fields>; } -keep class android.support.** -keepclasseswithmembers class android.support.** { *;} -keep class org.simpleframeork.** -keepclasseswithmembers class org.simpleframeork.** { *;} -keep class javax.** -keepclasseswithmembers class javax.** { *;} -keep class com.test.category.** -keepclassmembernames class com.test.category.** { *; } -keepclasseswithmembers class com.test.category.** { *;} -keep class com.test.download.** -keepclassmembernames class com.test.download.** { *; } -keepclasseswithmembers class com.test.download.** { *;} -keep class org.simpleframework.**{ *; } -keep class org.simpleframework.xml.**{ *; } -keep class org.simpleframework.xml.core.**{ *; } -keep class org.simpleframework.xml.util.**{ *; } -keep class org.simpleframework.xml.stream.**{ *; } -keepclassmembers class * implements java.io.Serializable { private static final java.io.ObjectStreamField[] serialPersistentFields; private void writeObject(java.io.ObjectOutputStream); private void readObject(java.io.ObjectInputStream); java.lang.Object writeReplace(); java.lang.Object readResolve(); } 

Solutions Collecting From Web of "Proguard混淆正在打破simplexml"

你已经知道保持注释是一个好主意。 您也可以尝试将types参数添加到@ElementList注释中 – 显然,genericstypes擦除存在问题,Simplexml需要关于列表中元素types的附加提示

你也可以玩-keepattributes Signature,* Annotation *:

在“JDK 5.0”和更高版本中编译时,“Signature”属性需要能够访问genericstypes。

使用SimpleXML库和混淆代码时的问题如下:

  1. 您必须保留实体的“注释”和“签名”

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

  2. 你必须保持SimpleXML库

  3. 例如,如果不使用实体的构造函数,proguard将删除它,但是该方法可以由Simple XML Library内部使用

proguard.cfg文件可能是这样的:

 # 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 *; } 

我用它在我自己的项目中,它的工作原理;)

在eclipse中右键点击你的项目。 转到android – >运行Lint。

林特有能力检查proguard错误configuration,并可能拿起,并解释你的错误。

我不断收到以下错误:

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

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

这是因为这些是Java运行时( rt.jar )的一部分,但不是Android运行时( android.jar )的一部分,所以ProGuard警告说可能会有某些东西被破坏。 这实际上不是一个问题,所以我们可以做到以下几点:

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

资源

结合zmicer的答案,我得到以下

 -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.* *; } 

尝试添加到您的proguard文件:

 -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.* *; } 

这为我修好了。

这个确切的proguard文件除了为我工作:

 -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.* *; }