user3169562
user3169562

Reputation:

Proguard error with gson after obfuscation

I got very specific bug that appers after obfuscation. I use gson to get game server data that used for updater.

public static List<Server> getServers() {
    try {
        URL url = new URL("http://api.ensemplix.ru/v2/server/game/");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        InputStream in = conn.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(in,
                "UTF-8"));
        return Arrays.asList(gson.fromJson(br, Server[].class));
    } catch (IOException e) {
        logger.warn("Failed get servers", e);
        return null;
    }
}

In provided data is array of mods list that are represented as ArrayList:

@SerializedName("mods")
private List<Mod> mods = new ArrayList<Mod>();

With no proguard obfuscation all works fine, but with i get a collection error:

http://cs608821.vk.me/v608821289/1815/xFtFEbtVYME.jpg This how it's looks in eclipse: http://cs608821.vk.me/v608821221/2541/-ZXvF0yKZJY.jpg

Method which throw error:

    public static List<Mod> getUpdateList(ServerType serverType) {
    List<Mod> updateList = new ArrayList<Mod>();

    System.out.println(serverType.getMods().toString());

    for (Mod mod : serverType.getMods()) {
        boolean install = false;
        boolean update = false;
        boolean exists = false;

        for (LocalMod localMod : localMods) {
            if (serverType == localMod.getServerType()) {
                if (mod.getName().equalsIgnoreCase(localMod.getName())) {
                    exists = true;

                    if (!mod.getVersion().equalsIgnoreCase(
                            localMod.getVersion())) {
                        logger.info("Removing mod " + localMod.getName()
                                + " version " + localMod.getVersion()
                                + " to update to " + mod.getVersion()
                                + " in " + serverType.getName());

                        if (!localMod.getFile().delete()) {
                            throw new IllegalArgumentException(
                                    "Failed delete mod "
                                            + localMod.getName()
                                            + " "
                                            + localMod.getFile()
                                                    .getAbsolutePath());
                        }

                        localMods.remove(localMod);
                        break;
                    }
                }
            }
        }

        if (!exists) {
            install = true;
        }

        if (install || update) {
            if (install) {
                logger.info("Installing mod " + mod.getName() + " version "
                        + mod.getVersion() + " for " + serverType.getName());
            }

            updateList.add(mod);
        }
    }

    return updateList;
}

I dont have any idea how fix obfuscation error. Can you help please?

Upvotes: 2

Views: 3030

Answers (2)

user3169562
user3169562

Reputation:

Solution:

@SerializedName("mods")
private Mod[] mods;

public List<Mod> getMods() {
    return Arrays.asList(mods);
}

Upvotes: 0

Devrim
Devrim

Reputation: 15533

There are 2 solutions for this:

1) You may add @SerializedName name to your Mod class and other json response related classes.

2) You may say proguard to not obfuscate a Java model if it is in a specific package which is related with your json response. As I see from your error log; your Mod class is obfuscated and its name became a. So; you can add something like below to the proguard.cfg file:

Edit:

# Gson uses generic type information stored in a class file when working with fields.
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature
# Gson specific classes
-keep class sun.misc.Unsafe { *; }

# Application classes that will be serialized/deserialized over Gson
-keep class ru.ensemplix.** { *; }

See link

Upvotes: 1

Related Questions