GuilhE GuilhE - 2 months ago 28
Android Question

Android Proguard ExceptionInInitializerError and RuntimeException

I want to obfuscate my .apk and I'm having some troubles with Proguard. Using eclipse I've enabled this:

# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt


I'm using 4 external libs:


  • android-support-v4.jar

  • nine-old-android-lib.jar

  • gson-2.2.2.jar

  • commons-io-2.4.jar



I guess I'm having trouble with gson:


private static Type MY_DATA_TYPE = new TypeToken<Pair<Map<Point, Void>, Integer>>() {}.getType();


Every time I use it:


FATAL EXCEPTION: main
java.lang.ExceptionInInitializerError at com.myapp.MyActivity.onCreate(Unknown Source)

...

Caused by: java.lang.RuntimeException: Missing type parameter.
at com.google.gson.reflect.TypeToken.getSuperclassTypeParameter(Unknown Source)
at com.google.gson.reflect.TypeToken.<init>(Unknown Source)


I'm using this options but I guess it wont help:

-keepattributes Exceptions, InnerClasses, *Annotation*, EnclosingMethod

-dontskipnonpubliclibraryclassmembers
-libraryjars .../libs/android-support-v4.jar
-libraryjars .../libs/nine-old-android-lib.jar
-libraryjars .../libs/gson-2.2.2.jar
-libraryjars .../libs/commons-io-2.4.jar


-keep class java.** { *; }
-keep class android.** { *; }
-keep class org.** { *; }
-keep class com.google.** { *; }
-keep class com.facebook.** { *; }
-keep class com.nineoldandroids.** { *; }


How can I solve this and create an obfuscated .apk that works properly?

Thanks for your time.

Answer

Guess this is a gson "problem", here's the solution:

-keepattributes Signature
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.examples.android.model.** { *; }

Thanks to https://groups.google.com/forum/#!topic/google-gson/6XuHfOoZIKo