0

我使用以下代码将字符串(包含 json)转换为对象(类别):

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 包中),但转换失败。

有任何想法吗 ?谢谢

4

1 回答 1

4

Json 使用反射来检索 getter 和 setter 的通用签名。ProGuard 默认会丢弃这些,所以你应该告诉它保留它们:

-keepattributes Signature

请注意,Android SDK 的最新版本已弃用proguard.cfg,取而代之的是一个proguard-project.txt基本上为空的文件,其中大部分配置位于共享的内部配置文件中(全部在 中指定project.properties)。

此外,您不应在配置中添加-injars-libraryjars选项,因为标准的 Ant/Eclipse/Gradle 构建过程会自动为您指定这些。

于 2013-09-13T23:57:39.397 回答