应用Proguard之后的ClassCast异常

时间:2018-07-10 10:59:42

标签: android android-studio android-proguard

我正在尝试发布启用了proguard的应用程序。没有proguard,该应用程序可以正常运行。但是,使用proguard,应用程序将在下面的代码中因ClassCastException崩溃。

Uri.Builder builder = Uri.parse(Constant.GET_HOME_BANNER_DATA).buildUpon();

        GsonRequest gsonRequest = new GsonRequest<HomeBannerDataList>(Request.Method.GET, builder.toString(),
                HomeBannerDataList.class, hashHeader, null, new Response.Listener<HomeBannerDataList>() {
            @Override
            public void onResponse(HomeBannerDataList response) {
                if (response == null || response.size() <= 0) {
                    return;
                }
                if (response.get(0).savingscheme != null) {
                    if (response.get(0).savingscheme.getData() != null && response.get(0).savingscheme.getData().size() > 0) {
                        if (!TextUtils.isEmpty(response.get(0).savingscheme.getData().get(0).getDescription())) {
                            Utility.setSavingSchemeText(response.get(0).savingscheme.getData().get(0).getDescription());
                        }
                        if (!TextUtils.isEmpty(response.get(0).savingscheme.getData().get(0).getMediumimage())) {
                            Utility.setSavingSchemeBanner(Constant.PRODUCT_IMAGE_URL + response.get(0).savingscheme.getData().get(0).getMediumimage());
                        }
                    }
                }

例外情况如下。

Fatal Exception: java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.g----ls.o----a.beans.HomeBannerData
       at com.grtjewels.oriana.fragment.homefragment.HomeFragmentImpPresenter$1.onResponse(Unknown Source)
       at com.gr----ls.o---na.webservice.GsonRequest.deliverResponse(Unknown Source)
       at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(Unknown Source)
       at android.os.Handler.handleCallback(Handler.java:751)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:154)
       at android.app.ActivityThread.main(ActivityThread.java:6123)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)

下面是proguard rules.pro文件。

# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in D:\AndroidSDK\adt-bundle-windows-x86_64-20140702\sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {

#}
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
#   public *;
#}
-dontskipnonpubliclibraryclasses
-dontskipnonpubliclibraryclassmembers

-dontwarn com.google.**
#-keep class com.google.** { *;}


#-keep class com.facebook.** { *; }
#-keepattributes Signature

-keep public class * implements com.bumptech.glide.module.GlideModule

-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.stream.** { *; }


#-keep class com.android.volley.** {*;}
#-keep class com.android.volley.toolbox.** {*;}
#-keep class com.android.volley.Response$* { *; }
#-keep class com.android.volley.Request$* { *; }
#-keep class com.android.volley.RequestQueue$* { *; }
##-keep class com.android.volley.toolbox.HurlStack$* { *; }
##-keep class com.android.volley.toolbox.ImageLoader$* { *; }

-keepclassmembers class ** {
  @com.some.package.server.JsonDeserializerWithOptions$FieldRequired public *;
}
#-keep @interface com.some.package.server.JsonDeserializerWithOptions$FieldRequired
#-keep class com.some.package.server.JsonDeserializerWithOptions
#-keepclassmembers,allowshrinking,allowobfuscation class com.android.volley.NetworkDispatcher {
#    void processRequest();
#}
#-keepclassmembers,allowshrinking,allowobfuscation class com.android.volley.CacheDispatcher {
#    void processRequest();
#}
# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
-keepclasseswithmembernames class * {
    native <methods>;
}
# keep setters in Views so that animations can still work.
# see http://proguard.sourceforge.net/manual/examples.html#beans
-keepclassmembers public class * extends android.view.View {
   void set*(***);
   *** get*();
}
# We want to keep methods in Activity that could be used in the XML attribute onClick
-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}
# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

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

-keepclassmembers class * implements java.io.Serializable {
    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();

}
-keep class com.g----ls.o----a.beans.** { *; }

# The support library contains references to newer platform versions.
# Don't warn about those in case this app is linking against an older
# platform version.  We know about them, and they are safe.
-dontwarn android.support.**
-dontwarn okio.**
-dontwarn org.codehaus.**
-keep public class org.jsoup.** {
public *;
}

我知道proguard文件存在问题。有人可以帮我解决这个问题吗?谢谢。

1 个答案:

答案 0 :(得分:0)

如果不正确,则必须保留将由Gson反序列化的每个类。在这种情况下,您的 HomeBannerData

看看Gson Sample Project中的示例规则,尤其是第14行:

##---------------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
-dontwarn sun.misc.**
#-keep class com.google.gson.stream.** { *; }

# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { *; }

# Prevent proguard from stripping interface information from TypeAdapterFactory,
# JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer

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