2012-08-28 26 views
19

Tôi đang sử dụng simplexml trong dự án Android của mình và mọi thứ đều hoạt động tốt cho đến khi tôi làm xáo trộn mã. Sau đó, các lỗi bắt đầu đổ vàoProguard obfuscation đang vi phạm simplexml

Một phần của XML như sau:.

<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" > 
... 

Tôi có hai lớp: 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; 
    } 
} 

và 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; 
    } 
} 

Bây giờ, khi tôi làm xáo trộn mã, nếu tôi bỏ qua "-keepattributes Chú thích "Tôi nhận được một PersistenceException: Constructor không phù hợp cho lớp.

Nếu tôi bao gồm nó, tôi nhận được thông báo "Không thể xác định loại chung cho tham số 1 của hàm tạo" ngoại lệ, tất cả đều trong thời gian chạy.
Như bạn có thể thấy, các tên có đó, và tôi đã cố gắng giữ toàn bộ lớp giữ chúng, tất cả đều không có kết quả.

Tôi làm cách nào để định cấu hình Proguard hoạt động với simplexml?

EDIT: file proguard.cfg của tôi là như sau: (đó là một chút nhồi với tất cả những điều tôi đã cố gắng, nhưng đây là phiên bản hiện tại)

-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(); 
} 
+1

đăng proguard.cfg –

Trả lời

2

Nhấp chuột phải dự án của bạn trong eclipse . Đi tới android -> chạy Lint.

Lint có khả năng kiểm tra các cấu hình sai của proguard và có thể nhận và giải thích lỗi của bạn.

+0

của tôi und a Proguard cảnh báo nhưng loại bỏ nó đã không giúp đỡ với vấn đề của tôi. – Janusz

6

Bạn đã nhận ra rằng việc giữ chú thích là một ý tưởng hay. Bạn cũng có thể thử thêm tham số kiểu vào chú thích @ElementList - có vẻ như có vấn đề với xóa kiểu chung và đơn giản cần thêm gợi ý về loại phần tử trong danh sách

bạn cũng có thể chơi chung với chữ ký -keepattributes, * Chú thích *:

Các "Chữ ký" thuộc tính là cần thiết để có thể truy cập vào chung loại khi biên dịch trong JDK 5.0 trở lên.

+0

Lúc đầu tôi đấu tranh để hiểu những gì bạn có nghĩa là, nhưng bây giờ tôi hoàn toàn hiểu và vấn đề của tôi đã được cố định bởi câu trả lời này. –

+0

Hoạt động thực sự. Vào cuối khai báo của tôi trông như thế này: '@ElementList (inline = true, type = Myclass.class)' –

4

Những vấn đề khi bạn sử dụng thư viện SimpleXML và làm mờ các mã được các nội dung sau:

  1. Bạn cần phải giữ "Chú thích" và "chữ ký" của tổ chức của bạn

    @Attribute (name = "retcode", required = true) chuỗi riêng _retcode;

  2. Bạn cần phải giữ SimpleXML Thư viện

  3. Bạn cần phải ngăn chặn các khối nhất định mã được loại bỏ, ví dụ, nếu các nhà xây dựng của một thực thể không được sử dụng, Proguard sẽ loại bỏ nó, nhưng phương pháp đó có thể là trong nội bộ được sử dụng bởi Thư viện Simple XML

File proguard.cfg có thể trở thành một cái gì đó như thế này:

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

tôi sử dụng nó trong dự án của riêng tôi và nó hoạt động;)

1

Hãy thử này thêm vào tập tin Proguard của bạn:

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

này cố định nó cho tôi.

2

Tôi liên tục nhận được các lỗi sau đây:

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

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

Đây là bởi vì đây là một phần của thời gian chạy Java (rt.jar) nhưng không nằm trong thời gian chạy Android (android.jar), do đó ProGuard cảnh báo rằng một cái gì đó có thể bị hỏng. Đây không phải là thực sự là một vấn đề, vì vậy chúng ta có thể làm như sau:

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

Source

Kết hợp với câu trả lời của zmicer, tôi nhận được sau

-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

99% này, nhưng một chút khác biệt cho 'dontwarn' và 'keepattributes'. -dontwarn javax.xml.stream. ** -keepattributes ElementList, Root, * Chú thích * –

0

này Ngoài ra chính xác đến tệp bảo vệ đã hoạt động cho tôi:

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