Android Proguard ExceptionInInitializerError 和 RuntimeException
Posted
技术标签:
【中文标题】Android Proguard ExceptionInInitializerError 和 RuntimeException【英文标题】:Android Proguard ExceptionInInitializerError and RuntimeException 【发布时间】:2013-06-30 00:37:45 【问题描述】:我想混淆我的 .apk,但我在使用 Proguard 时遇到了一些问题。使用 eclipse 我启用了这个:
# 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
我正在使用 4 个外部库:
android-support-v4.jar 九老-android-lib.jar gson-2.2.2.jar commons-io-2.4.jar我想我在使用 gson 时遇到了问题:
private static Type MY_DATA_TYPE = new TypeToken<Pair<Map<Point, Void>, Integer>>() .getType();
每次使用时:
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)
我正在使用这个选项,但我想它不会有帮助:
-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.** *;
我该如何解决这个问题并创建一个可以正常工作的混淆 .apk? 感谢您的宝贵时间。
【问题讨论】:
我正在使用... Type type = new TypeToken猜这是一个gson“问题”,这是解决方案:
-keepattributes Signature
-keep class sun.misc.Unsafe *;
-keep class com.google.gson.examples.android.model.** *;
感谢https://groups.google.com/forum/#!topic/google-gson/6XuHfOoZIKo
【讨论】:
我正在使用... Type type = new TypeToken如google gson proguard 示例所示的Proguard 配置。
google gson proguard configuration link
##---------------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
# For using GSON @Expose annotation
-keepattributes *Annotation*
# 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.** *;
# Prevent proguard from stripping interface information from TypeAdapterFactory,
# JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer
##---------------End: proguard configuration for Gson ----------
【讨论】:
以上是关于Android Proguard ExceptionInInitializerError 和 RuntimeException的主要内容,如果未能解决你的问题,请参考以下文章