Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App crashing when I run Proguard on GSON (which using enum)

I am using GSON in my Android app to parser the data. I have added an enum in my Model object. After adding an enum my app started crashing. When I made runProguard false everything works fine. I did everything I could find on internet to fix the problem when program is true. But still no luck.

I have already done following thing in proguard-rule file

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

-keepattributes *Annotation*
-keepattributes Signature

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

Enum Stuff example:

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

@SerializedName("upcoming")
UPCOMING,

@SerializedName("replay")
REPLAY; }

My stack trace

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)
like image 531
AAV Avatar asked Apr 18 '14 14:04

AAV


2 Answers

This should be solved by using the following rule:

-keepclassmembers enum * { *; }

or the use the following:

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

Related answer: https://stackoverflow.com/a/30167048/4758255

like image 198
ישו אוהב אותך Avatar answered Nov 16 '22 17:11

ישו אוהב אותך


I resolved this problem with adding those rules in my proguard-rule file :

    # 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);
}
like image 23
Fadwa Bechri Avatar answered Nov 16 '22 17:11

Fadwa Bechri