现在大部分项目都使用第三方的jar包,包括json解析的gson.jar,统计用户信息的umeng.jar,support-v4.jar等等等等,这些jar包本身基本都做了混淆,如果再用progurad混淆的话会出现错误,所以要通过-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.** { *; }
7.微信
注意
如果需要混淆代码,为了保证sdk的正常使用,需要在proguard.cfg加上下面两行配置:
-keep class com.tencent.mm.sdk.openapi.WXMediaMessage {*;}
-keep class com.tencent.mm.sdk.openapi.** implements com.tencent.mm.sdk.openapi.WXMediaMessage$IMediaObject {*;}
8.项目中有新浪微博weibo.sdk.android.sso.jar怎么办?
-dontwarn android.net.http.**
-keep public class android.webkit.WebView {*;}
-keep public class android.webkit.WebViewClient {*;}
-keep class com.weibo.net.** {*;}
2014.12.05更新
9 、sharesdk
关于sharesdk的混淆 http://write.blog.csdn.net/postedit/9465439
<span style="font-size:18px;">-keep class cn.sharesdk.**{*;} -keep class com.sina.**{*;} -keep class **.R$* {*;} -keep class **.R{*;} -dontwarn cn.sharesdk.** -dontwarn **.R$* 上面的脚本分别keep了ShareSDK和新浪微博SSO授权方式所需要的所有jar和源码。 考虑到部分开发者会使用附带Sample中的侧栏控件,那么此时还需要keep这个控件的代码: -keep class m.framework.**{*;} 最后请注意,注册到cn.sharesdk.framework.ShareSDKUIShell下用于自定义授权页面的类也是需要keep的,否则ShareSDK将找不到它。</span></span>
2015.02.02更新
百度地图
<span style="font-size:18px;">为何我的程序在不混淆之前可以运行,混淆之后不能运行了? 矢量版SDK(即2.0.0及之后的版本): 如果是用proguard ,以下配置供参考: -libraryjars libs/baidumapapi_v2_1_2.jar 替换成自己所用版本的jar包 -keep class com.baidu.** { *; } -keep class vi.com.gdi.bgl.android.**{*;} 其他混淆工具也请进行与此类似的配置,指定对mapapi中的类和方法不进行混淆</span>
2015.07.23更新
fastjson
<span style="font-size:18px;">-libraryjars libs/fastjson.jar -keep class com.alibaba.fastjson.** { *; } -keepclassmembers class * { public <methods>; }</span>