Android打正式的release包混淆是必备的,避免APP被反编译,使项目中隐秘的技术或信息被别人查看。
Eclipse中混淆配置写在proguard-android.txt文件中,并在project.properties文件中设置proguard.config=proguard-project.txt
Android studio则写在proguard-rules.pro中,当然写在proguard-android.txt中也可以。
gradle配置:
buildTypes { release { buildConfigField"boolean","LOG_DEBUG","false"//不显示log zipAlignEnabledtrue//Zipalign优化 shrinkResourcestrue// 移除无用的resource文件 minifyEnabledtrue//混淆 proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'}}
经常用第三方SDK的时候会忘了顺手把混淆加进来,到了打包的时候又去找,百度噼里啪啦一堆,很是麻烦,因此在这里总结一下,下次用的时候偶就可以直接CC了,哈哈。如有不对欢迎指正。
-optimizationpasses5# 指定代码的压缩级别-dontusemixedcaseclassnames# 是否使用大小写混合-dontpreverify# 混淆时是否做预校验-verbose# 混淆时是否记录日志-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*# 混淆时所采用的算法-keep publicclass*extendsandroid.app.Activity# 保持哪些类不被混淆-keep publicclass*extendsandroid.app.Application# 保持哪些类不被混淆-keep publicclass*extendsandroid.app.Service# 保持哪些类不被混淆-keep publicclass*extendsandroid.content.BroadcastReceiver# 保持哪些类不被混淆-keep publicclass*extendsandroid.content.ContentProvider# 保持哪些类不被混淆-keep publicclass*extendsandroid.app.backup.BackupAgentHelper# 保持哪些类不被混淆-keep publicclass*extendsandroid.preference.Preference# 保持哪些类不被混淆-keep publicclasscom.android.vending.licensing.ILicensingService# 保持哪些类不被混淆-keepclasseswithmembernamesclass* {# 保持 native 方法不被混淆 native ;}-keepclasseswithmembersclass* {# 保持自定义控件类不被混淆 public (android.content.Context, android.util.AttributeSet);}-keepclasseswithmembersclass* {# 保持自定义控件类不被混淆 public (android.content.Context, android.util.AttributeSet, int); }-keepclassmembersclass*extendsandroid.app.Activity{# 保持自定义控件类不被混淆 public void *(android.view.View);}-keepclassmembers enum * {# 保持枚举 enum 类不被混淆 public static **[] values(); public static ** valueOf(java.lang.String);}-keepclass*implementsandroid.os.Parcelable{#保持Parcelable不被混淆public static final android.os.Parcelable$Creator *;}# Explicitly preserve all serialization members. The Serializable interface# is only a marker interface, so it wouldn't save them.-keep publicclass*implementsjava.io.Serializable{*;}-keepclassmembersclass*implementsjava.io.Serializable{static final long serialVersionUID; private static final java.io.ObjectStreamField[] serialPersistentFields; private void writeObject(java.io.ObjectOutputStream); private void readObject(java.io.ObjectInputStream); java.lang.Object writeReplace(); java.lang.Object readResolve();}-keepclassmembersclass* {public (org.json.JSONObject);}#com.demo.demo是你的包名-keep publicclasscom.demo.demo.R$*{public static final int *;}
以上部分摘自:http://blog.csdn.net/bjstyle/article/details/47001277
Gsongithub地址
# removes such information by default, so configure it to keep all of it.-keepattributes Signature# Gson specific classes-keepclasssun.misc.Unsafe{ *; }-keepclasscom.google.gson.stream.**{ *; }# Application classes that will be serialized/deserialized over Gson-keepclasscom.google.gson.examples.android.model.**{ *; }-keepclasscom.google.gson.**{ *;}#这句非常重要,主要是滤掉 com.demo.demo.bean包下的所有.class文件不进行混淆编译,com.demo.demo是你的包名-keepclasscom.demo.demo.bean.**{*;}
Butterknifegithub地址
-keepclassbutterknife.**{ *; }-dontwarn butterknife.internal.**-keepclass**$$ViewBinder{ *; }-keepclasseswithmembernamesclass*{@butterknife.* ;}-keepclasseswithmembernamesclass*{@butterknife.* ;}
SlidingMenugithub地址
-dontwarn com.jeremyfeinstein.slidingmenu.lib.**-keepclasscom.jeremyfeinstein.slidingmenu.lib.**{*;}
ImageLoadergithub地址
#Universal Image Loader-keepclasscom.nostra13.universalimageloader.**{ *; }-keepattributes Signature
ShareSDK文档
-keepclasscn.sharesdk.**{*;}-keepclasscom.sina.**{*;}-keepclass**.R$* {*;}-keepclass**.R{*;}-dontwarn cn.sharesdk.**-dontwarn **.R$*-dontwarn com.tencent.**-keepclasscom.tencent.** {*;}
环信SDK文档
-keepclasscom.easemob.**{*;}-keepclassorg.jivesoftware.**{*;}-keepclassorg.apache.**{*;}-dontwarn com.easemob.**#2.0.9后的不需要加下面这个keep#-keep class org.xbill.DNS.** {*;}#另外,demo中发送表情的时候使用到反射,需要keep SmileUtils-keepclasscom.easemob.chatuidemo.utils.SmileUtils{*;}#注意前面的包名,如果把这个类复制到自己的项目底下,比如放在com.example.utils底下,应该这么写(实际要去掉#)#-keep class com.example.utils.SmileUtils {*;}#如果使用easeui库,需要这么写-keepclasscom.easemob.easeui.utils.EaseSmileUtils{*;}#2.0.9后加入语音通话功能,如需使用此功能的api,加入以下keep-dontwarn ch.imvs.**-dontwarn org.slf4j.**-keepclassorg.ice4j.**{*;}-keepclassnet.java.sip.**{*;}-keepclassorg.webrtc.voiceengine.**{*;}-keepclassorg.bitlet.**{*;}-keepclassorg.slf4j.**{*;}-keepclassch.imvs.**{*;}
Okhttputilsgithub地址
#okhttputils-dontwarn com.zhy.http.**-keepclasscom.zhy.http.**{*;}#okhttp-dontwarn okhttp3.**-keepclassokhttp3.**{*;}#okio-dontwarn okio.**-keepclassokio.**{*;}
RxJava相关文档
# rx-dontwarn rx.**-keepclassmembersclassrx.** {*; }# retrolambda-dontwarn java.lang.invoke.*
Glidegithub地址
-keep publicclass*implementscom.bumptech.glide.module.GlideModule-keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** { **[] $VALUES; public *;}#下面这句不加#-keepresourcexmlelements manifest/application/meta-data@value=GlideModu
作者:钟子明
链接:https://www.jianshu.com/p/155430a27f00
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。