8

我在我的应用程序中使用 Gson,为此,我使用了一些名称与使用 Json 相同的类。我的应用程序运行良好,但是在编写 proguard 时,应用程序崩溃了,我猜有些类正在缩小。我的错误是:

java.lang.ClassCastException:com.google.gson.internal.StringMap 无法转换为 com.sample.package.GsonClass

4

2 回答 2

22

您需要将这些行添加到您的 proguard 中,以便在签署您的应用程序时保留 gson 类。

##---------------Begin: proguard configuration for Gson  ----------
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature


# Gson specific classes
-keep class sun.misc.Unsafe { *; }
#-keep class com.google.gson.stream.** { *; }

# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { *; }

##---------------End: proguard configuration for Gson  ----------
于 2013-05-15T12:20:50.520 回答
3

另一个原因是:使用前

List<T> list = gson.fromJson(jsonString, type);

您应该构造正确的 typeToken,如下所示:

Type type = new TypeToken<List<T>>(){}.getType();
List<T> list = gson.fromJson(jsonString,type)
于 2015-07-29T06:35:30.047 回答