3

我想混淆我的 .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 个外部库:

  • 安卓支持-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?
谢谢你的时间。

4

2 回答 2

7

猜猜这是一个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

于 2013-07-02T16:02:46.043 回答
1

Proguard 配置如 google gson proguard 示例所示。

google gson proguard 配置链接

##---------------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  ----------
于 2016-09-29T14:24:11.517 回答