我使用下面的代码将一个字符串(包含json)转换为Object(Category):
String jsonInfo = "{'id':'GAME','name':'game','subCategories':[{'subId':'ALL','subName':'All games'},{'subId':'SOCIAL','subName':'Social game'}]}";
Category xx = getCategory(jsonInfo);
//method for casting string into Category
public Category getCategory (String json){
Category myObject ;
myObject = new Gson().fromJson(json, Category.class);
}
我的分类:
public class Category extends MasterDomain {
public String id;
public String name;
public List<SubCategory> subCategories;
public Category() {
}
public Category(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<SubCategory> getSubCategories() {
return subCategories;
}
public void setSubCategories(List<SubCategory> subCategories) {
subCategories = subCategories;
}
}
当我使用密钥库导出应用程序时,Category对象生成完美且应用程序正常工作。但是,使用proguard时,“subCategories”会填充“Object”对象。
日志文件:
09-13 14:46:42.313: W/System.err(31631): java.lang.ClassCastException: java.lang.Object cannot be cast to jp.xxx.DB.domains.SubCategory
我的proguard文件:
-optimizationpasses 2
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-dump class_files.txt
-printseeds seeds.txt
-printusage unused.txt
-printmapping mapping.txt
# The -optimizations option disables some arithmetic simplifications that Dalvik 1.0 and 1.5 can't handle.
-optimizations !code/simplification/arithmetic
# Basic elements
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class com.google.gson.Gson
# for views
-keep public class * extends View {
public <init>(android.content.Context);
public <init>(android.content.Context, android.util.AttributeSet);
public <init>(android.content.Context, android.util.AttributeSet, int);
public void set*(...);
}
# Enumerations
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
# for gson (GSON @Expose annotation)
-keepattributes *Annotation*
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.stream.** { *; }
-keep class com.google.gson.examples.android.model.** { *; }
-dontwarn org.w3c.dom.bootstrap.**
-dontwarn org.joda.**
# external libraries
-libraryjars libs/android-support-v4.jar
-libraryjars libs/gson-1.7.1.jar
-libraryjars libs/jp.maru.mrd-1.1.1.jar
-libraryjars libs/libGoogleAnalyticsV2.jar
# for google analytics & campaign tool
-keep class com.google.android.gms.analytics.**
-keep class com.google.analytics.tracking.**
-dontwarn com.google.android.gms.analytics.**
-dontwarn com.google.analytics.tracking.**
# for parcebable objects
-keep class jp.xxx.DB.domains.** {
*;
}
-keepattributes SourceFile, LineNumberTable
我已经“保留”了我的Category和SubCategory类(在jp.xxx.DB.domains包中),但是施法失败了。
有什么想法吗? 谢谢
答案 0 :(得分:4)
Json使用反射来检索getter和setter的通用签名。 ProGuard默认会丢弃它们,因此您应该告诉它保留它们:
-keepattributes Signature
请注意,Android SDK的最新版本已弃用proguard.cfg
,而支持基本为空的文件proguard-project.txt
,其中大部分配置位于共享内部配置文件中(均在{ {1}})。
此外,您不应在配置中添加project.properties
或-injars
选项,因为标准的Ant / Eclipse / Gradle构建过程会自动为您指定这些选项。