Android: Cast String with Gson (proguard)

2019-08-28 17:43发布

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

}

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

My proguard file:

-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

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

Any ideas ? Thank you

1条回答
该账号已被封号
2楼-- · 2019-08-28 18:36

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

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).

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.

查看更多
登录 后发表回答