Android:使用Gson投射字符串(proguard) [英] Android: Cast String with Gson (proguard)

查看:160
本文介绍了Android:使用Gson投射字符串(proguard)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码将一个字符串(包含json)转换为一个Object(Category):

I use the below code to cast a string (containing json) into an 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);
}

我的分类类别:

My 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;
    }

}

当我使用密钥库导出应用程序,类别对象生成完美,应用程序工作。但是,使用proguard时,subCategories中填充了Object对象。

When I export application with the keystore, Category object is generated perfectly and application works. However, when using proguard, "subCategories" is filled with "Object" object.

日志文件:

Log file :

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

我的程序文件:

-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

我有保存我的类别和子类别类(在jp.xxx.DB.domains包中),但投射失败。

I have "kept" my Category and SubCategory classes (in the jp.xxx.DB.domains package) but cast failed.

任何想法?
谢谢

Any ideas ? Thank you

推荐答案

Json使用反射来检索getters和setters的通用签名。 ProGuard在默认情况下会丢弃这些内容,因此您应该告诉它以保留它们:

Json uses reflection to retrieve the generic signatures of the getters and setters. ProGuard discards those by default, so you should tell it to preserve them:

-keepattributes Signature

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

Note that recent versions of the Android SDK have deprecated proguard.cfg in favor of a file proguard-project.txt that is essentially empty, with the bulk of the configuration in a shared internal configuration file (all specified in project.properties).

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

Furthermore, you should not add -injars or -libraryjars options to your configuration, since the standard Ant/Eclipse/Gradle build processes automatically specify these for you.

这篇关于Android:使用Gson投射字符串(proguard)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆