2012-04-16 75 views
3

我用我的应用程序一个simple XML serializer,似乎何时已经成功地从Proguard的混淆通过使用proguard.cfg以下行排除它,感谢这个SO questionNullPointerException异常使用资源的XML文件/原使用ProGuard

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

我能够签署和导出apk,但是当我尝试运行我的应用程序时,它尝试访问res/raw目录(R.raw.home_screen_menu)中的XML文件时发生NullPointerException异常。

我排除R.java这个:

-keepclassmembers class **.R$* { 
    public static <fields>; 
} 

我一直是这样的配置文件,整天玩耍,但没有任何运气。有没有人遇到过这个问题或类似的东西?我用这样的了“adaptresource”选项尝试最新的东西如下所示:

-adaptresourcefilenames **.xml 
-adaptresourcefilecontents **.xml 

仅供参考,这里是我proguard.cfg的内容:

#Use 5 step of optimization 
-optimizationpasses 5 

#When not preverifing in a case-insensitive filing system, such as Windows. This tool will unpack your processed jars,(if using windows you should then use): 
-dontusemixedcaseclassnames 

#Specifies not to ignore non-public library classes. As of version 4.5, this is the default setting 
-dontskipnonpubliclibraryclasses 

#Preverification is irrelevant for the dex compiler and the Dalvik VM, so we can switch it off with the -dontpreverify option. 
-dontpreverify 

#Specifies to write out some more information during processing. If the program terminates with an exception, this option will print out the entire stack trace, instead of just the exception message. 
-verbose 

#The -optimizations option disables some arithmetic simplifications that Dalvik 1.0 and 1.5 can't handle. Note that the Dalvik VM also can't handle aggressive overloading (of static fields). 
#To understand or change this check http://proguard.sourceforge.net/index.html#/manual/optimizations.html 
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/* 

-dump ../bin/class_files.txt 
-printseeds ../bin/seeds.txt 
-printusage ../bin/unused.txt 
-printmapping ../bin/mapping.txt 

-dontskipnonpubliclibraryclassmembers 

#Ignore warnings for roboguice.activity.RoboAccountAuthenticatorActivity 
-dontwarn roboguice.activity.RoboAccountAuthenticatorActivity 

-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 
-keep class com.google.inject.Binder 

# Keep annotations 
-keepattributes *Annotation* 
-keepattributes Signature 

#To remove debug logs: 
-assumenosideeffects class android.util.Log { 
    public static *** d(...); 
    public static *** v(...); 
    public static *** w(...); 
} 

# Roboguice 
-keepclassmembers class * { 
    @com.google.inject.Inject <init>(...); 
} 

# SimpleXML 
-keep public class org.simpleframework.**{ *; } 
-keep class org.simpleframework.xml.**{ *; } 
-keep class org.simpleframework.xml.core.**{ *; } 
-keep class org.simpleframework.xml.util.**{ *; } 
-dontwarn javax.xml.stream.** 
-dontwarn javax.xml.namespace.** 

-keep public class roboguice.** 

# There's no way to keep all @Observes methods, so use the On*Event convention to identify event handlers 
-keepclassmembers class * { 
    void *(**On*Event); 
} 

-keep public class * extends android.view.View { 
    public <init>(android.content.Context); 
    public <init>(android.content.Context, android.util.AttributeSet); 
    public <init>(android.content.Context, android.util.AttributeSet, int); 
    public void set*(...); 
} 

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

-keepclassmembers class * implements android.os.Parcelable { 
    static android.os.Parcelable$Creator CREATOR; 
} 

-keepclassmembers class **.R$* { 
    public static <fields>; 
} 

回答

4

原来的原料文件被遗留下来,好吧。问题在于我的类使用Simple XML序列化器的批注进行序列化。我将log语句添加到我的应用程序,并在违规行中发现此错误:“属性'id'在第2行的com.xxx.ListViewItems类中没有匹配”。这让我意识到私有的“id”变量被混淆,Simple XML序列化程序无法识别。下面是来自ListViewItems.java代码:

@Attribute(required=false) 
private String id; // This was being obfuscated and thus unrecognizable! 

这是我加什么的ProGuard完全跳过混淆一个类(有可能是一个更好的方法):

# Ignore our XML Serialization classes 
-keep public class com.xxx.ListViewItems { 
    public protected private *; 
} 
+3

顺便提一下,在曲线中显示的'-keepattributes Signature'和'-keepattributes * Annotation *' estion的proguard配置在这里很重要;如果没有这些,proguard仍然会抛弃成员的注释,以及简单XML的“构造函数注入”的构造函数参数注释(如果使用的话)。 – 2012-05-12 21:03:25

+1

谢谢@MattGibson,你的评论救了我 – Bruce 2014-04-06 16:48:37

+0

谢谢!这个答案帮助我解决我的问题! :) – sabadow 2015-02-17 09:26:00

6

我有完全一样的问题。最后,我解决这个问题要感谢这个问题和答案。我离开这里的最终配置:

的build.gradle:

buildTypes { 
    release { 
     minifyEnabled true 
     shrinkResources true 
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
    } 
} 

dependencies { 
    compile('org.simpleframework:simple-xml:2.7.1') { 
     exclude module: 'stax' 
     exclude module: 'stax-api' 
     exclude module: 'xpp3' 
    } 
} 

proguard-rules.pro(除了默认的ProGuard配置文件)

-dontwarn javax.xml.** 

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

-keepattributes Signature 
-keepattributes *Annotation* 

# Ignore our XML Serialization classes 
-keep public class your.annotated.pojo.models.*{ 
    public protected private *; 
} 

希望它可以帮助给别人