前言
混淆是上线前挺重要的一个环节。Android使用的ProGuard,可以起到压缩,混淆,预检,优化的作用。但是很多童鞋还没有掌握正确的写混淆文件的姿势。我希望搞个模板化的东西,让大家轻松搞定混淆。那么,开始写一个项目的混淆吧。。。我是不是很直接。。。。
打开项目中的proguard-rules.pro,将下面的代码复制进去。我的思路是将混淆分为2个主要部分
定制化区域。这里边的内容是我们主要需要补充的部分,大致分为4个小部分,我们接下来的步骤主要是补充这4个部分的东西。
基本不用动区域。顾名思义,基本不用动。这块的内容我已经写好了,除非有特殊的需求,基本不用改动。。。请叫我雷锋。。。。。
#-------------------------------------------定制化区域----------------------------------------------#---------------------------------1.实体类---------------------------------#-------------------------------------------------------------------------#---------------------------------2.第三方包-------------------------------#-------------------------------------------------------------------------#---------------------------------3.与js互相调用的类------------------------#-------------------------------------------------------------------------#---------------------------------4.反射相关的类和方法-----------------------#----------------------------------------------------------------------------#---------------------------------------------------------------------------------------------------#-------------------------------------------基本不用动区域--------------------------------------------#---------------------------------基本指令区-----------------------------------optimizationpasses5-dontusemixedcaseclassnames-dontskipnonpubliclibraryclasses-dontskipnonpubliclibraryclassmembers-dontpreverify-verbose-printmapping proguardMapping.txt-optimizations !code/simplification/cast,!field/*,!class/merging/*-keepattributes *Annotation*,InnerClasses-keepattributes Signature-keepattributes SourceFile,LineNumberTable#----------------------------------------------------------------------------#---------------------------------默认保留区----------------------------------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 publicclass*extendsandroid.view.View-keep publicclasscom.android.vending.licensing.ILicensingService-keepclassandroid.support.** {*;}-keepclasseswithmembernamesclass* {native ;}-keepclassmembersclass*extendsandroid.app.Activity{public void *(android.view.View);}-keepclassmembers enum * {public static **[] values(); public static ** valueOf(java.lang.String);}-keep publicclass*extendsandroid.view.View{*** get*(); void set*(***); public (android.content.Context); public (android.content.Context, android.util.AttributeSet); public (android.content.Context, android.util.AttributeSet, int);}-keepclasseswithmembersclass* {public (android.content.Context, android.util.AttributeSet); public (android.content.Context, android.util.AttributeSet, int);}-keepclass*implementsandroid.os.Parcelable{public static final android.os.Parcelable$Creator *;}-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();}-keepclass**.R$* {*;}-keepclassmembersclass* {void *(**On*Event);}#----------------------------------------------------------------------------#---------------------------------webview-------------------------------------keepclassmembersclassfqcn.of.javascript.interface.for.Webview{public *;}-keepclassmembersclass*extendsandroid.webkit.WebViewClient{public void *(android.webkit.WebView, java.lang.String, android.graphics.Bitmap); public boolean *(android.webkit.WebView, java.lang.String);}-keepclassmembersclass*extendsandroid.webkit.WebViewClient{public void *(android.webkit.WebView, jav.lang.String);}#----------------------------------------------------------------------------#---------------------------------------------------------------------------------------------------
理解完战略级思想后,我们开始第一部分补充-实体类。实体类由于涉及到与服务端的交互,各种gson的交互如此等等,是要保留的。将你项目中实体类都拎出来,用以下语法进行保留。
-keepclass你的实体类所在的包.** { *;}
如我的项目下类User的完整路径为:com.demo.login.bean.User, 那我的混淆如下
#---------------------------------1.实体类----------------------------------keepclasscom.demo.login.bean.**{ *; }#-------------------------------------------------------------------------
当然你的实体类肯定不止这一个,请用上边的方式一一添加,如果你的实体类都在一个包下,那你就幸福了。
第2部分是第三方包。打开你的build.gradle文件,查看你用了哪些第三方的包。
dependencies{compile'com.github.bumptech.glide:glide:3.7.0'compile'org.greenrobot:eventbus:3.0.0'}
我这里用了glide,eventbus。我去他们的官网把已经写好的混淆copy下来。
#---------------------------------2.第三方包-------------------------------#eventBus-keepattributes *Annotation*-keepclassmembersclass** {@org.greenrobot.eventbus.Subscribe ;}-keepenumorg.greenrobot.eventbus.ThreadMode{ *;}-keepclassmembersclass*extendsorg.greenrobot.eventbus.util.ThrowableFailureEvent{(java.lang.Throwable);}#glide-keep publicclass*implementscom.bumptech.glide.module.GlideModule-keep publicenumcom.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {**[] $VALUES; public *;}#-------------------------------------------------------------------------
一般官网都是有混淆的,没有的话就google,也没有的话自己按照上面的写法自己写,还不会的话。。。。。只能换个包。。。。。如果你是直接包含的jar包的话,你这样写
#---------------------------------2.第三方包-------------------------------#log4j-libraryjars log4j-1.2.17.jar-dontwarn org.apache.log4j.**-keepclassorg.apache.log4j.** { *;}#-------------------------------------------------------------------------
大致意思就是不混淆,不报warn。如果gradle报错的话,可以考虑注释掉-libraryjars log4j-1.2.17.jar这句。
第三部分与js互调的类,工程中没有直接跳过。一般你可以这样写
-keepclass你的类所在的包.** { *;}
如果是内部类的话,你可以这样
-keepclasseswithmembersclass你的类所在的包.父类$子类 { ;}
例如
-keepclasseswithmembersclasscom.demo.login.bean.ui.MainActivity$JSInterface{; }
第四部分与反射有关的类,工程中没有直接跳过。类的话直接这样
-keepclass你的类所在的包.** { *;}
熟练后不用五分钟就能搞定。大家可以把自己写完混淆所用的时间写在评论里(0,0)
先逐条介绍下基本指令区指令的含义
-optimizationpasses 5
代码混淆的压缩比例,值在0-7之间
-dontusemixedcaseclassnames
混淆后类名都为小写
-dontskipnonpubliclibraryclasses
指定不去忽略非公共的库的类
-dontskipnonpubliclibraryclassmembers
指定不去忽略非公共的库的类的成员
-dontpreverify
不做预校验的操作
-verbose-printmapping proguardMapping.txt
生成原类名和混淆后的类名的映射文件
-optimizations !code/simplification/cast,!field/*,!class/merging/*
指定混淆是采用的算法
-keepattributes*Annotation*,InnerClasses
不混淆Annotation
-keepattributes Signature
不混淆泛型
-keepattributes SourceFile,LineNumberTable
抛出异常时保留代码行号
基本指令区基本介绍完了,说下2条用的最多的指令
-keepclassXXXX
保留类名不变,也就是类名不混淆,而类中的成员名不保证。当然也可以是继承XXX类的所有类名不混淆,具体代码不贴了,重在理解。
-keepclasseswithmembersclassXXXX
保留类名和成员名。当然也可以是类中特定方法,代码不贴了,理由同上。
#-------------------------------------------定制化区域----------------------------------------------#---------------------------------1.实体类----------------------------------keepclasscom.demo.login.bean.** { *;}-keepclasscom.demo.main.bean.** { *;}#-------------------------------------------------------------------------#---------------------------------2.第三方包-------------------------------#eventBus-keepattributes *Annotation*-keepclassmembersclass** {@org.greenrobot.eventbus.Subscribe ;}-keep enum org.greenrobot.eventbus.ThreadMode { *; }-keepclassmembersclass*extendsorg.greenrobot.eventbus.util.ThrowableFailureEvent{(java.lang.Throwable);}#glide-keep publicclass*implementscom.bumptech.glide.module.GlideModule-keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {**[] $VALUES; public *;}#log4j-libraryjars log4j-1.2.17.jar-dontwarn org.apache.log4j.**-keepclassorg.apache.log4j.** { *;}#-------------------------------------------------------------------------#---------------------------------3.与js互相调用的类-------------------------keepclasseswithmembersclasscom.demo.login.bean.ui.MainActivity$JSInterface{; }#-------------------------------------------------------------------------#---------------------------------4.反射相关的类和方法-----------------------#TODO 我的工程里没有。。。#----------------------------------------------------------------------------#---------------------------------------------------------------------------------------------------#-------------------------------------------基本不用动区域--------------------------------------------#---------------------------------基本指令区-----------------------------------optimizationpasses5-dontusemixedcaseclassnames-dontskipnonpubliclibraryclasses-dontskipnonpubliclibraryclassmembers-dontpreverify-verbose-printmapping proguardMapping.txt-optimizations !code/simplification/cast,!field/*,!class/merging/*-keepattributes *Annotation*,InnerClasses-keepattributes Signature-keepattributes SourceFile,LineNumberTable#----------------------------------------------------------------------------#---------------------------------默认保留区----------------------------------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 publicclass*extendsandroid.view.View-keep publicclasscom.android.vending.licensing.ILicensingService-keepclassandroid.support.** {*;}-keepclasseswithmembernamesclass* {native ;}-keepclassmembersclass*extendsandroid.app.Activity{public void *(android.view.View);}-keepclassmembers enum * {public static **[] values(); public static ** valueOf(java.lang.String);}-keep publicclass*extendsandroid.view.View{*** get*(); void set*(***); public (android.content.Context); public (android.content.Context, android.util.AttributeSet); public (android.content.Context, android.util.AttributeSet, int);}-keepclasseswithmembersclass* {public (android.content.Context, android.util.AttributeSet); public (android.content.Context, android.util.AttributeSet, int);}-keepclass*implementsandroid.os.Parcelable{public static final android.os.Parcelable$Creator *;}-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();}-keepclass**.R$* {*;}-keepclassmembersclass* {void *(**On*Event);}#----------------------------------------------------------------------------#---------------------------------webview-------------------------------------keepclassmembersclassfqcn.of.javascript.interface.for.Webview{public *;}-keepclassmembersclass*extendsandroid.webkit.WebViewClient{public void *(android.webkit.WebView, java.lang.String, android.graphics.Bitmap); public boolean *(android.webkit.WebView, java.lang.String);}-keepclassmembersclass*extendsandroid.webkit.WebViewClient{public void *(android.webkit.WebView, jav.lang.String);}#----------------------------------------------------------------------------#---------------------------------------------------------------------------------------------------
将build.gradle中minifyEnabled设置为true打个包试试吧
release{minifyEnabledtrueproguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'}
其他需求可以留言讨论。如果觉得有用请帮忙戳喜欢。。
1.模板中的规则有部分与默认的proguard-android.txt文件中的重复了,可以精简一下
#-------------------------------------------定制化区域----------------------------------------------#---------------------------------1.实体类---------------------------------#-------------------------------------------------------------------------#---------------------------------2.第三方包-------------------------------#-------------------------------------------------------------------------#---------------------------------3.与js互相调用的类------------------------#-------------------------------------------------------------------------#---------------------------------4.反射相关的类和方法-----------------------#----------------------------------------------------------------------------#---------------------------------------------------------------------------------------------------#-------------------------------------------基本不用动区域--------------------------------------------#---------------------------------基本指令区-----------------------------------optimizationpasses5-dontskipnonpubliclibraryclassmembers-printmapping proguardMapping.txt-optimizations !code/simplification/cast,!field/*,!class/merging/*-keepattributes *Annotation*,InnerClasses-keepattributes Signature-keepattributes SourceFile,LineNumberTable#----------------------------------------------------------------------------#---------------------------------默认保留区----------------------------------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 publicclass*extendsandroid.view.View-keep publicclasscom.android.vending.licensing.ILicensingService-keepclassandroid.support.** {*;}-keep publicclass*extendsandroid.view.View{*** get*(); void set*(***); public (android.content.Context); public (android.content.Context, android.util.AttributeSet); public (android.content.Context, android.util.AttributeSet, int);}-keepclasseswithmembersclass* {public (android.content.Context, android.util.AttributeSet); public (android.content.Context, android.util.AttributeSet, int);}-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();}-keepclass**.R$* {*;}-keepclassmembersclass* {void *(**On*Event);}#----------------------------------------------------------------------------#---------------------------------webview-------------------------------------keepclassmembersclassfqcn.of.javascript.interface.for.Webview{public *;}-keepclassmembersclass*extendsandroid.webkit.WebViewClient{public void *(android.webkit.WebView, java.lang.String, android.graphics.Bitmap); public boolean *(android.webkit.WebView, java.lang.String);}-keepclassmembersclass*extendsandroid.webkit.WebViewClient{public void *(android.webkit.WebView, jav.lang.String);}#----------------------------------------------------------------------------#---------------------------------------------------------------------------------------------------