我遇到了非常具体的错误,该错误在混淆后出现。我使用 gson 获取用于更新程序的游戏服务器数据。
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;
}
}
提供的数据是 mods 列表的数组,表示为 ArrayList:
@SerializedName("mods")
private List<Mod> mods = new ArrayList<Mod>();
没有 proguard 混淆一切正常,但我得到一个收集错误:
这是它在 eclipse 中的样子:
抛出错误的方法:
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;
}
我不知道如何修复混淆错误。你能帮忙吗?
最佳答案
有两种解决方案:
1) 您可以将@SerializedName
名称添加到您的Mod
类和其他json 响应相关类。
2) 如果 Java 模型位于与您的 json 响应相关的特定包中,您可能会说 proguard 不会混淆它。正如我从您的错误日志中看到的那样;你的 Mod
类被混淆了,它的名字变成了 a。所以;您可以将如下内容添加到 proguard.cfg 文件中:
编辑:
# 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.** { *; }
关于java - 混淆后gson的Proguard错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21800369/