当我在GSON上运行Proguard时应用程序崩溃(使用枚举)

2022-09-04 01:58:45

我正在我的Android应用程序中使用GSON来解析数据。我已经在我的模型对象中添加了一个枚举。添加枚举后,我的应用开始崩溃。当我使runProguard假时,一切都很好。当程序属实时,我尽我所能在互联网上找到解决问题。但仍然没有运气。

我已经在proguard-rule文件中完成了以下事情

-keep class com.google.** { *; }

-keepattributes *Annotation*
-keepattributes Signature

-keepclassmembers enum * { public static **[] values(); public static ** valueOf(java.lang.String); }

枚举内容示例:

public enum TypeX {
@SerializedName("live")
LIVE,

@SerializedName("upcoming")
UPCOMING,

@SerializedName("replay")
REPLAY; }

我的堆栈跟踪

java.lang.AssertionError
        at com.google.gson.internal.bind.TypeAdapters$EnumTypeAdapter.<init>(Unknown Source)
        at com.google.gson.internal.bind.TypeAdapters$26.create(Unknown Source)
        at com.google.gson.Gson.getAdapter(Unknown Source)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(Unknown Source)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(Unknown Source)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(Unknown Source)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(Unknown Source)
        at com.google.gson.Gson.getAdapter(Unknown Source)
        at com.google.gson.internal.bind.CollectionTypeAdapterFactory.create(Unknown Source)
        at com.google.gson.Gson.getAdapter(Unknown Source)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(Unknown Source)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(Unknown Source)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(Unknown Source)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(Unknown Source)
        at com.google.gson.Gson.getAdapter(Unknown Source)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(Unknown Source)
        at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.write(Unknown Source)
        at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.write(Unknown Source)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(Unknown Source)
        at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(Unknown Source)
        at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(Unknown Source)
        at com.google.gson.Gson.toJson(Unknown Source)
        at com.google.gson.Gson.toJson(Unknown Source)
        at com.google.gson.Gson.toJson(Unknown Source)
        at com.google.gson.Gson.toJson(Unknown Source)
        at com.theice.gsonapp.app.MainActivity.a(Unknown Source)
        at com.theice.gsonapp.app.MainActivity.onCreate(Unknown Source)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)

答案 1

应使用以下规则解决此问题:

-keepclassmembers enum * { *; }

或使用以下方法:

-keepclassmembers enum com.your.package.** { *; }

相关答案:https://stackoverflow.com/a/30167048/4758255


答案 2

我通过在我的proguard规则文件中添加这些规则解决了这个问题:

    # GSON.
-keepnames class com.google.gson.** {*;}
-keepnames enum com.google.gson.** {*;}
-keepnames interface com.google.gson.** {*;}
-keep class com.google.gson.** { *; }
-keepnames class org.** {*;}
-keepnames enum org.** {*;}
-keepnames interface org.** {*;}
-keep class org.** { *; }

-keepnames class com.yourpackagename.net.** {*;}
-keepnames enum com.yourpackagename.net.** {*;}
-keepnames interface com.yourpackagename.net.** {*;}
-keep class com.yourpackagename.net.** { *; }

-keepattributes Signature
-keepattributes *Annotation*
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.stream.** { *; }

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}