Proguard issue while using GSON

匿名 (未验证) 提交于 2019-12-03 02:06:01

问题:

I am using Gson in my application and for that, i am using some classes with name as same as the one in using Json. My application works well, But while writing proguard, application crashes, I guess some of the classes are shrinking. my error is :

java.lang.ClassCastException: com.google.gson.internal.StringMap cannot be cast to com.sample.package.GsonClass

回答1:

You need to add these lines to your proguard so that gson class is kept while signing your app..

##---------------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   # 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.google.gson.examples.android.model.** { *; }  ##---------------End: proguard configuration for Gson  ---------- 


回答2:

Another reason is: before use

List list = gson.fromJson(jsonString, type); 

you should construct the correct typeToken, like this:

Type type = new TypeToken>(){}.getType(); List list = gson.fromJson(jsonString,type) 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!