Gson parsing on android with proguard

∥☆過路亽.° 提交于 2019-12-30 13:41:33

问题


I only have a parsing problem with Gson when I use proguard. In debug mode and release mode without proguard, everything work fine.

This is my json string :

{"tables":[{"name":"Profile","columns":[{"label":"id","type":"text","primary":true},{"label":"name","type":"text"},{"label":"age","type":"integer"},{"label":"human","type":"integer"},{"label":"gear","type":"Gear","custom":true,"list":true}]},{"name":"Gear","columns":[{"label":"id","type":"text","primary":true},{"label":"type","type":"text","enum":true},{"label":"name","type":"text"},{"label":"id_Profile","type":"integer","foreign_key":true}]},{"name":"Animal","columns":[{"label":"id","type":"text","primary":true},{"label":"name","type":"text"},{"label":"magic","type":"integer"},{"label":"id_Profile","type":"integer","foreign_key":true}]}]}

These are my POJOs :

public class Database {
    private List<Table> tables;

    public List<Table> getTables() {
        return tables;
    }

    public void setTables(List<Table> tables) {
        this.tables = tables;
    }
}

public class Table {
    private String name;
    private List<Column> columns;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Column> getColumns() {
        return columns;
    }

    public void setColumns(List<Column> columns) {
        this.columns = columns;
    }
 }

I read the file containing the json string and parse it this way :

public Database getDatabase(Context context) throws IOException {
    InputStream stream = context.getAssets().open(PATH);
    String schema = getJsonSchema(stream);
    Gson gson = new Gson();
    return gson.fromJson(schema, Database.class);
}

But when I try to call the method getTables() from my Database object :

for (Table table : database.getTables()) {
    //...
}

I receive this error : java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference. I have included those rules for proguard. Any idea what's the reason ?


回答1:


As darwin commented, adding the @Keep annotation to my POJOs classes fixed the issue



来源:https://stackoverflow.com/questions/40862840/gson-parsing-on-android-with-proguard

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