Java 模糊处理后gson的Proguard错误
我有一个非常特殊的错误,在混淆之后出现。我使用gson获取用于更新程序的游戏服务器数据Java 模糊处理后gson的Proguard错误,java,gson,proguard,Java,Gson,Proguard,我有一个非常特殊的错误,在混淆之后出现。我使用gson获取用于更新程序的游戏服务器数据 public static List<Server> getServers() { try { URL url = new URL("http://api.ensemplix.ru/v2/server/game/"); HttpURLConnection conn = (HttpURLConnection) url.openConnection();
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;
}
}
publicstaticlist getServers(){
试一试{
URL=新URL(“http://api.ensemplix.ru/v2/server/game/");
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
InputStream in=conn.getInputStream();
BufferedReader br=新的BufferedReader(新的InputStreamReader(在,
“UTF-8”);
返回Arrays.asList(gson.fromJson(br,Server[].class));
}捕获(IOE异常){
logger.warn(“获取服务器失败”,e);
返回null;
}
}
在提供的数据中,mods列表的数组表示为ArrayList:
@SerializedName("mods")
private List<Mod> mods = new ArrayList<Mod>();
@SerializedName(“mods”)
private List mods=new ArrayList();
在没有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;
}
公共静态列表getUpdateList(服务器类型服务器类型){
List updateList=new ArrayList();
System.out.println(serverType.getMods().toString());
对于(Mod:serverType.getMods()){
布尔安装=false;
布尔更新=false;
布尔存在=假;
用于(LocalMod LocalMod:localMods){
if(serverType==localMod.getServerType()){
if(mod.getName().equalsIgnoreCase(localMod.getName())){
存在=真;
如果(!mod.getVersion().equalsIgnoreCase(
localMod.getVersion()){
logger.info(“删除mod”+localMod.getName()
+“version”+localMod.getVersion()
+更新为“+mod.getVersion()
+“+serverType.getName())中的”;
如果(!localMod.getFile().delete()){
抛出新的IllegalArgumentException(
“删除模块失败”
+localMod.getName()
+ " "
+localMod.getFile()
.getAbsolutePath());
}
localMods.remove(localMod);
打破
}
}
}
}
如果(!存在){
install=true;
}
如果(安装| |更新){
如果(安装){
logger.info(“安装mod”+mod.getName()+“版本”
+mod.getVersion()+”表示“+serverType.getName());
}
updateList.add(mod);
}
}
返回更新列表;
}
我不知道如何修复混淆错误。你能帮忙吗 有两种解决方案: 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.** { *; }
解决方案:
@SerializedName("mods")
private Mod[] mods;
public List<Mod> getMods() {
return Arrays.asList(mods);
}
@SerializedName(“mods”)
私有模块[]模块;
公共列表getMods(){
返回数组.asList(mods);
}
每个属性都有@SerializedName,在.cfg中,我防止mod类混淆。如果您应用我的第二个建议,您将不会收到任何错误,或者错误消息必须更改。你试过了吗?用-keep类ru.ensemplix.*{*}我也有同样的问题我编辑了我的答案,这是一个谷歌的例子,我目前正在使用类似的例子。嗯,我看到你的解决方案是我的第一个建议。