android - 使用 Proguard 在 res/raw 中使用 XML 文件时出现 NullPointerException

标签 android proguard simple-framework

我正在使用 simple XML serializer在我的应用程序中,似乎已经通过在 proguard.cfg 中使用以下行成功地将其从 Proguard 混淆中排除,这要归功于 SO question :

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

最佳答案

我也有同样的问题。最后我解决了这个问题和答案。我把最终配置留在这里:

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(添加到默认混淆器配置)

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

希望对大家有帮助

关于android - 使用 Proguard 在 res/raw 中使用 XML 文件时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10182076/

相关文章:

java - android 尝试乘法和显示 double 时出现错误

java - 如何使用 ImageView 而不是菜单项制作底部导航栏?

java - 简单框架的映射 Mime 类型属性

android - 如何在Android的一次查询中获取流派和每种流派的歌曲数?

android - ImageSwitcher 在前 2 个图像切换后没有动画

java - Proguard 混淆和通用方法

Android - Proguard 重复 zip 条目错误

android - Android Studio 中混淆文件夹结构、包

java - org.simpleframework.xml.core.PersistenceException : Constructor not matched for class A

java - 如何更改 servlet 中路径的主机和端口?