Android 将res/raw中的XML文件与Proguard一起使用时出现NullPointerException

Android 将res/raw中的XML文件与Proguard一起使用时出现NullPointerException,android,proguard,simple-framework,Android,Proguard,Simple Framework,我在我的应用程序中使用了一个,通过使用Proguard.cfg中的以下行,似乎已成功地将其从Proguard模糊处理中排除,这要归功于: 我可以签名并导出apk,但当我尝试运行我的应用程序时,当尝试访问res/raw目录(R.raw.home\u screen\u菜单)中的XML文件时,它会因NullPointerException崩溃 我将R.java排除在外,原因如下: -keepclassmembers class **.R$* { public static <fields

我在我的应用程序中使用了一个,通过使用Proguard.cfg中的以下行,似乎已成功地将其从Proguard模糊处理中排除,这要归功于:

我可以签名并导出apk,但当我尝试运行我的应用程序时,当尝试访问res/raw目录(R.raw.home\u screen\u菜单)中的XML文件时,它会因NullPointerException崩溃

我将R.java排除在外,原因如下:

-keepclassmembers class **.R$* {
    public static <fields>;
}
以下是my 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>;
}
#使用5个优化步骤
-优化过程5
#在不区分大小写的归档系统(如Windows)中不预先验证时。此工具将解压缩已处理的JAR(如果使用windows,则应使用):
-dontusemixedcaseclassnames
#指定不忽略非公共库类。从版本4.5开始,这是默认设置
-DontskipnonPublicLibraryClass
#预验证与dex编译器和Dalvik VM无关,因此我们可以使用-dontpreverify选项将其关闭。
-不要翻转
#指定在处理过程中写出更多信息。如果程序以异常终止,此选项将打印出整个堆栈跟踪,而不仅仅是异常消息。
-冗长的
#-optimizations选项禁用Dalvik 1.0和1.5无法处理的一些算术简化。请注意,Dalvik VM也不能处理(静态字段的)主动重载。
#了解或更改此支票http://proguard.sourceforge.net/index.html#/manual/optimizations.html
-优化!代码/简化/算术,!字段/*,!类/合并/*
-转储../bin/class_files.txt
-printseeds../bin/seeds.txt
-printusage../bin/unused.txt
-printmapping../bin/mapping.txt
-dontskipnonpubliclibraryclassmembers
#忽略roboguice.activity.RoboAccountAuthenticationActivity的警告
-dontwarn roboguice.activity.RoboAccountAuthenticationActivity
-keep public class*扩展android.app.Activity
-keep public class*扩展android.app.Application
-keep public class*扩展了android.app.Service
-keep public class*扩展了android.content.BroadcastReceiver
-keep public class*扩展了android.content.ContentProvider
-keep public class*扩展了android.app.backup.BackupAgentHelper
-keep public class*扩展了android.preference.preference
-保持公共类com.android.vending.licensing.ILicensingService
-保持类com.google.inject.Binder
#保留注释
-keepattributes*注释*
-保留署名
#要删除调试日志,请执行以下操作:
-assumenosideeffects类android.util.Log{
公共静态***d(…);
公共静态***v(…);
公共静态***w(…);
}
#机器人向导
-keepclassmembers类*{
@com.google.inject.inject(…);
}
#SimpleXML
-保持公共类org.simpleframework.*{*;}
-保持类org.simpleframework.xml.*{*;}
-保持类org.simpleframework.xml.core.*{*;}
-保持类org.simpleframework.xml.util.*{*;}
-dontwarn javax.xml.stream**
-dontwarn javax.xml.namespace**
-保持公共类机器人导航**
#无法保留所有@Observes方法,因此请使用On*事件约定来标识事件处理程序
-keepclassmembers类*{
无效*(**在*事件发生时);
}
-keep public class*扩展了android.view.view{
public(android.content.Context);
public(android.content.Context、android.util.AttributeSet);
public(android.content.Context,android.util.AttributeSet,int);
公共无效集*(…);
}
-keepclassswithmembers类*{
本地人;
}
-keepclassswithmembers类*{
public(android.content.Context、android.util.AttributeSet);
}
-keepclassswithmembers类*{
public(android.content.Context,android.util.AttributeSet,int);
}
-keepclassmembers枚举*{
公共静态**[]值();
公共静态**valueOf(java.lang.String);
}
-keep class*实现android.os.Parcelable{
公开静态最终android.os.Parcelable$Creator*;
}
-keepclassmembers类*实现android.os.Parcelable{
静态android.os.Parcelable$Creator;
}
-keepclassmembers类**.R$*{
公共静态;
}

原来原始文件是单独存在的,没有问题。问题是我的类用于序列化,并带有简单XML序列化程序的注释。我在我的应用程序中添加了日志语句,并在有问题的行中发现此错误:“属性“id”在第2行的com.xxx.ListViewItems类中没有匹配项”。这使我意识到私有“id”变量正在被模糊化,并且无法被简单的XML序列化程序识别。以下是ListViewItems.java中的代码:

@Attribute(required=false)
private String id;  // This was being obfuscated and thus unrecognizable!
以下是我添加到Proguard中的内容,以完全避免混淆类(可能有更好的方法):


我也有同样的问题。多亏了这个问题和答案,我终于解决了它。我在这里留下了最终的配置:

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配置中)


希望对某人有所帮助

顺便说一句,问题的proguard配置中显示的
-keepattributes签名
-keepattributes*注释*
在这里很重要;如果没有这些,proguard仍然会丢弃用于成员的注释,以及用于简单XML“构造函数注入”的构造函数参数注释(如果您正在使用它们的话)。谢谢@MattGibson,您的注释保存了meThanks!!这个答案帮助我解决了我的问题!:)
@Attribute(required=false)
private String id;  // This was being obfuscated and thus unrecognizable!
# Ignore our XML Serialization classes
-keep public class com.xxx.ListViewItems {
  public protected private *;
}
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'
    }
}
-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 *;
}