我遇到了proguard和Gson庫的問題。我的代碼是Gson庫和Proguard
AdServerResult result = (AdServerResult) new Gson().fromJson(json,
AdServerResult.class);
(我創建一個使用GSON圖書館新AdServerResult
對象)
我AdServerResult
類:
public class AdServerResult {
public ArrayList<AdServerObject> shorts ;
public ArrayList<AdServerObject> longs ;
public ArrayList<AdServerObject> getShorts() {
return shorts;
}
public void setShorts(ArrayList<AdServerObject> shorts) {
this.shorts = shorts;
}
public ArrayList<AdServerObject> getLongs() {
return longs;
}
public void setLongs(ArrayList<AdServerObject> longs) {
this.longs = longs;
}
//for debug
@Override
public String toString(){
StringBuilder sb = new StringBuilder() ;
if (shorts!=null && shorts.size()>0){
for (AdServerObject s: shorts){
sb.append("url: "+s.getPicture_url());
}
}
if (longs!=null && longs.size()>0){
for (AdServerObject s: longs){
sb.append("url: "+s.getPicture_url());
}
}
return sb.toString();
}
}
我AdServerObject
(存在於AdServerResult
類):
public class AdServerObject{
public String appli_id;
public String picture_url;
public String getAppli_id() {
return appli_id;
}
public void setAppli_id(String appli_id) {
this.appli_id = appli_id;
}
public String getPicture_url() {
return picture_url;
}
public void setPicture_url(String picture_url) {
this.picture_url = picture_url;
}
}
而我的「JSON」字符串如下:
{
"shorts": [
{ "appli_id": "282", "picture_url": "xxxx"},
{ "appli_id": "275", "picture_url": "xxx" }
],
"longs": [
{"appli_id": "MyAppli3", "picture_url": "xxxxx"},
{"appli_id": "MyAppli4", "picture_url": "xxxxx" }
]
}
此代碼的工作,但使用ProGuard導出時,該AdServerResult
實例null
。
我proguard的文件(僅JSON部分):
# for gson (GSON @Expose annotation)
-keep public class com.google.gson.Gson
-keepattributes *Annotation*
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.stream.** { *; }
-keep class com.google.gson.examples.android.model.** { *; }
...
-keepattributes Signature
-keepattributes *Annotation*
...
-keep public class jp.xxx.ad_server.AdServerResult
-keep public class jp.xxx.ad_server.AdServerObject
-keepclassmembers class jp.xxx.ad_server.AdServerObject
-keepclassmembers class jp.xxx.ad_server.AdServerResult
我已經使用這個Gson().fromJson
很多次,但我從來沒有遇到過這樣的問題....我這個問題掙扎了12個小時,我的大腦即將爆炸。
- 編輯
我已經更新的ProGuard(4.11),但問題仍然存在。
http://stackoverflow.com/questions/6533676/proguard-and-gson-on-android-classcastexception?rq=1頂部的答案可能會有所幫助 –
謝謝你的回答。我的proguard文件類似於https://code.google.com/p/google-gson/source/browse/trunk/examples/android-proguard-example/proguard.cfg的「Gson的proguard配置」 – johann