技术收藏

ProGuard混淆Exception

2016-08-04  本文已影响788人  walker113

转:Android-Exception整理之com.google.gson.internal.xxx cannot be cast to xxx

Android混淆打包及混淆后运行错误

1. 有时我们的Android使用Google的Gson库,在混淆后会报出一些异常:

java.lang.ClassCastException: com.google.gson.internal.StringMap cannot be cast to xxx
java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to xxx

这些异常往往是在配置混淆文件时出现的问题:

-keepattributes Signature
-keepattributes *Annotation*
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.examples.android.model.** { *; }
# com.package.name是你的项目包名
-keep class com.package.name.bean.** { *; }
-keep class com.package.name.bean.entity.** { *; }
-keep public class com.package.name.R$*{ 
    public static final int *;
}

2. Android引用第三方jar包混淆时打包报错(can't find referenced class)

项目打包的时候,总是失败,提示Proguard returned with error code 1. See console。然后看控制台,报出好多can't find referenced class......

首先要把你项目中所引入的第三方jar包使用"-libraryjars 包路径"指定好。
然后,用
-dontwarn com.xx.bbb.**
-keep class com.xx.bbb.** { *;}
告诉proguard.cfg不用混淆指定的jar包,也不用提示这个包所报出的错。
注意:要是用dontwarn取消指定包报错之前,你要保证第三方引用的类在你的项目中没有用到。

3. Android混淆后项目运行的时候出现异常(混淆之前没有该异常)

a. ClassNotFoundException,NoSuchMethodError

 -keep class package.classname{*;}

b. ExceptionInInitializerError

c. ClassCastException

d. Resources$NotFoundException(resource not found)

上一篇 下一篇

猜你喜欢

热点阅读