【问题标题】:Android runtime error related to Proguard与 Proguard 相关的 Android 运行时错误
【发布时间】:2016-03-05 13:58:29
【问题描述】:

更新

我解决了这个问题。问题是 Proguard 重命名了我的大部分模型类,所以我仔细地包含了规则,以便它保留这些类并且没有修改或重命名任何东西。

以下 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(一个用于网络的 android 库)相关的错误。我正在制作一个 GsonRequest 并基本上将 json 响应从服务器映射到我的 pojos。正如我所说,如果未启用 Proguard,这一切都可以正常工作。

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

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

错误是 nullpointerexception,因为我有一个 pojo,它有一个返回数组列表的函数。我正在用凌空响应映射这个 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)

【问题讨论】:

    标签: android gson proguard android-volley


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-21
      • 2015-04-02
      • 2017-11-08
      • 1970-01-01
      • 1970-01-01
      • 2017-10-02
      • 1970-01-01
      相关资源
      最近更新 更多