Pregunta

I have got an issue with proguard and the Gson library. My code is

AdServerResult result = (AdServerResult) new Gson().fromJson(json, 
      AdServerResult.class);

(I create a new AdServerResult object using the Gson library)

My AdServerResult class :

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();
    }

}

My AdServerObject (present in the AdServerResult class) :

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;
    }       
}

And my "json" string is as below :

{ 
  "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" }
  ]

}

This code works but when exporting with proguard, the AdServerResult instance is null.

My proguard file (only the json part) :

# 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

I have used this Gson().fromJson many times but I never encountered a such problem.... I am struggling with this issue for 12 hours and my brain is about to explode.

-- EDIT

I have updated proguard(4.11) but problem remains.

¿Fue útil?

Solución

You want to keep the fields of the classes that you are serializing, with their original names:

-keepclassmembers class jp.xxx.ad_server.AdServerResult$AdServerObject { <fields>; }
-keepclassmembers class jp.xxx.ad_server.AdServerResult { <fields>; }

ProGuard may otherwise rename or even remove them.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top