与Proguard相关的Android运行时错误

时间:2016-03-05 13:58:30

标签: android gson proguard android-volley

更新

我解决了这个问题。问题是Proguard重命名了我的大多数模型类,因此我仔细地包含了规则,以便保留这些类并且不修改或重命名任何内容。

以下准则规则有效。

-dontwarn com.squareup.okhttp.**
-dontwarn com.android.volley.**

-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {
**[] $VALUES;
public *;
}

##---------------Begin: proguard configuration for Gson  ----------
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature

# For using GSON @Expose annotation
-keepattributes *Annotation*

# Gson specific classes
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.stream.** { *; }

# Application classes that will be serialized/deserialized over Gson
-keep class com.*.*.pojos.** { *; }
-keep class com.*.*.pojos.check.** { *; }
-keep class com.*.*.pojos.rocket.** { *; }
-keep class com.*.*.pojos.rocketDetail.** { *; }
-keep class com.*.*.pojos.weather.** { *; }

##---------------End: proguard configuration for Gson  ----------

-keep class com.squareup.okhttp.** { *; }

-keeppackagenames org.jsoup.nodes

# Hide warnings about references to newer platforms in the library
-dontwarn android.support.v7.**
# don't process support library 
-keep class android.support.v7.** { *; }
-keep interface android.support.v7.** { *; }

如果未启用Proguard,我的应用程序可以正常工作。但是如果我启用proguard我会收到与Volley(一个用于网络的安卓库)相关的错误。我正在制作GsonRequest并基本上将json响应从服务器映射到我的pojos。正如我所说,如果没有启用Proguard,一切正常。

我很确定错误与我的模型类的响应映射有关,在这种情况下,如果启用了proguard,则不会发生这种情况。

我尝试了许多保存pojos,Gson库和Volley本身的proguard配置。我正在使用Gson 2.4库。但它们似乎都不起作用。我一次又一次地得到同样的错误。

错误是nullpointerexception,因为我有一个pojo,它有一个返回arraylist的函数。我正在使用排球响应来映射这个pojo。由于没有映射,因此arrylist不会使用值进行初始化。

我该怎么做才能解决这个问题?我找不到任何解决方案,我想用proguard混淆我的代码。我应该使用Proguard吗?

pro.cfg

##---------------Begin: proguard configuration common for all Android apps ----------

-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontskipnonpubliclibraryclassmembers
-dontpreverify
-verbose
-dump class_files.txt
-printseeds seeds.txt
-printusage unused.txt
-printmapping mapping.txt
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

-allowaccessmodification
-keepattributes *Annotation*
-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable
-repackageclasses ''

-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
-dontnote com.android.vending.licensing.ILicensingService

# Explicitly preserve all serialization members. The Serializable interface
# is only a marker interface, so it wouldn't save them.
-keepclassmembers class * implements java.io.Serializable {
    static final long serialVersionUID;
    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();
}

# Preserve all native method names and the names of their classes.
-keepclasseswithmembernames class * {
    native <methods>;
}

-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

# Preserve static fields of inner classes of R classes that might be accessed
# through introspection.
-keepclassmembers class **.R$* {
  public static <fields>;
}

# Preserve the special static methods that are required in all enumeration classes.
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep public class * {
    public protected *;
}

-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}
##---------------End: proguard configuration common for all Android apps ----------

##---------------Begin: proguard configuration for Gson  ----------
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature

# For using GSON @Expose annotation
-keepattributes *Annotation*

# Gson specific classes
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.stream.** { *; }

# Application classes that will be serialized/deserialized over Gson
-keep class com.blah.blah.pojos.** { *; }

##---------------End: proguard configuration for Gson  ----------

-dontwarn com.squareup.okhttp.**

-dontwarn com.android.volley.**

-keep class com.android.volley.** { *; }

调试日志

FATAL EXCEPTION: main
                                                                     Process: com.balh.balh, PID: 722
                                                                     java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
                                                                         at com.balh.balh.network.services.e.a(Unknown Source)
                                                                         at com.balh.balh.network.services.e.a(Unknown Source)
                                                                         at com.balh.balh.network.a.b(Unknown Source)
                                                                         at com.android.volley.i.run(Unknown Source)
                                                                         at android.os.Handler.handleCallback(Handler.java:739)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                         at android.os.Looper.loop(Looper.java:135)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5287)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at java.lang.reflect.Method.invoke(Method.java:372)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

1 个答案:

答案 0 :(得分:0)

也许你错过了okhttp的proguard规则: - 尝试添加

-keep class com.squareup.okhttp.** { *; }