Android中proguard混淆第三方jar包及内部类
现在大部分项目都使用第三方的jar包,包括json解析的gson.jar,统计用户信息的umeng.jar,support-v4.jar等等等等,这些jar包本身基本都做了混淆,如果再用proguard混淆的话会出现错误,所以要通过-keep等方法不混淆这些jar中的内容,把到现在用过的整理下仅供参考~~
1.项目中有gson的jar包怎么办?
-keep class sun.misc.Unsafe { *; } -keep class com.google.gson.stream.** { *; } -keep class com.google.gson.examples.android.model.** { *; } -keep class com.google.gson.** { *;}
2.项目中有commons-httpclient-3.1.jar怎么办?
-keep public class org.apache.commons.httpclient.** {*;} -keep public class org.apache.commons.httpclient.auth.** {*;} -keep public class org.apache.commons.httpclient.cookie.** {*;} -keep public class org.apache.commons.httpclient.methods.** {*;} -keep public class org.apache.commons.httpclient.params.** {*;} -keep public class org.apache.commons.httpclient.util.** {*;} -keep public class org.apache.commons.codec.net.** {*;} -keep public class org.apache.commons.logging.** {*;} -keep public class org.apache.commons.logging.impl.** {*;} -keep public class org.apache.commons.codec.** {*;} -keep public class org.apache.commons.codec.binary.** {*;}
3.项目中有umeng_sdk.jar怎么办?
-keepclassmembers class * { public <init>(org.json.JSONObject); } -keep public class [您的应用包名].R$*{ public static final int *; } 把[您的应用包名] 替换成您自己的包名,如com.yourcompany.example。
如果您使用了双向反馈功能,还需要添加下面代码,以免我们自定义的UI被混淆:
-keep public class com.umeng.fb.ui.ThreadView { }
4.项目中有baidumapapi.jar怎么办?
-keep class com.baidu.mapapi.** {*;}
5.项目中有android-support-v4.jar怎么办?
-dontwarn android.support.v4.** -keep class android.support.v4.** { *; } -keep public class * extends android.support.v4.** -keep public class * extends android.app.Fragment
之前我还这样做过
-dontwarn android.support.v4.** -keep public class android.support.v4.view.** { *; } -keep public class android.support.v4.accessibilityservice.** { *; } -keep public class android.support.v4.os.** { *; } -keep public class android.support.v4.app.** { *; } -keep public class android.support.v4.widget.** { *; }
6.项目中有ksoap2-android-assembly-2.3-jar-with-dependencies.jar怎么办?
-ignorewarnings -keep class org.kobjects.** { *; } -keep class org.ksoap2.** { *; } -keep class org.kxml2.** { *; } -keep class org.xmlpull.** { *; }
-ignorewarnings是忽略proguard过程中所有的警告,用了它就不必使用-dontwarn方法啦,但是用它要谨慎!
7.项目中的某个Activity中有webview与js交互的内部类,怎么才能不混淆这个内部类?
-keep class [某个包的包名].[含js交互的Activity]$*{ <methods>; } -keepclassmembers class [某个包的包名].[含js交互的Activity]$*{ *; }
例如:
-keep class com.xxx.xx.XxxActivity$*{ <methods>; } -keepclassmembers class com.xxx.xx.XxxActivity$*{ *; }
8.项目中有geronimo-spec-activation-1.0.2-rc2.jar怎么办?
-ignorewarnings -keep class javax.activation.DataHandler.** { *; } -keep class javax.activation.ActivationDataFlavor.** { *; }
9.项目中有微信分享的jar包怎么办?
-keep class com.tencent.mm.sdk.openapi.WXMediaMessage {*;} -keep class com.tencent.mm.sdk.openapi.** implements com.tencent.mm.sdk.openapi.WXMediaMessage$IMediaObject {*;}