5步搞定android混淆

WilburnCalv 8年前
   <h2>前言</h2>    <p>混淆是上线前挺重要的一个环节。android使用的ProGuard,可以起到压缩,混淆,预检,优化的作用。但是很多童鞋还没有掌握正确的写混淆文件的姿势。我希望搞个模板化的东西,让大家轻松搞定混淆。那么,开始写一个项目的混淆吧。。。我是不是很直接。。。。</p>    <p>五步搞定</p>    <h2>1</h2>    <p>打开项目中的proguard-rules.pro,将下面的代码复制进去。我的思路是将混淆分为2个主要部分</p>    <ul>     <li>定制化区域。这里边的内容是我们主要需要补充的部分,大致分为4个小部分,我们接下来的步骤主要是补充这4个部分的东西。</li>     <li>基本不用动区域。顾名思义,基本不用动。这块的内容我已经写好了,除非有特殊的需求,基本不用改动。。。请叫我雷锋。。。。。</li>    </ul>    <pre>  <code class="language-java">#-------------------------------------------定制化区域----------------------------------------------  #---------------------------------1.实体类---------------------------------        #-------------------------------------------------------------------------    #---------------------------------2.第三方包-------------------------------        #-------------------------------------------------------------------------    #---------------------------------3.与js互相调用的类------------------------        #-------------------------------------------------------------------------    #---------------------------------4.反射相关的类和方法-----------------------        #----------------------------------------------------------------------------  #---------------------------------------------------------------------------------------------------    #-------------------------------------------基本不用动区域--------------------------------------------  #---------------------------------基本指令区----------------------------------  -optimizationpasses 5  -dontusemixedcaseclassnames  -dontskipnonpubliclibraryclasses  -dontskipnonpubliclibraryclassmembers  -dontpreverify  -verbose  -printmapping proguardMapping.txt  -optimizations !code/simplification/cast,!field/*,!class/merging/*  -keepattributes *Annotation*,InnerClasses  -keepattributes Signature  -keepattributes SourceFile,LineNumberTable  #----------------------------------------------------------------------------    #---------------------------------默认保留区---------------------------------  -keep public class * extends android.app.Activity  -keep public class * extends android.app.Appliction  -keep public class * extends android.app.Service  -keep public class * extends android.content.BroadcastReceiver  -keep public class * extends android.content.ContentProvider  -keep public class * extends android.app.backup.BackupAgentHelper  -keep public class * extends android.preference.Preference  -keep public class * extends android.view.View  -keep public class com.android.vending.licensing.ILicensingService  -keep class android.support.** {*;}    -keepclasseswithmembernames class * {      native <methods>;  }  -keepclassmembers class * extends android.app.Activity{      public void *(android.view.View);  }  -keepclassmembers enum * {      public static **[] values();      public static ** valueOf(java.lang.String);  }  -keep public class * extends android.view.View{      *** get*();      void set*(***);      public <init>(android.content.Context);      public <init>(android.content.Context, android.util.AttributeSet);      public <init>(android.content.Context, android.util.AttributeSet, int);  }  -keepclasseswithmembers class * {      public <init>(android.content.Context, android.util.AttributeSet);      public <init>(android.content.Context, android.util.AttributeSet, int);  }  -keep class * implements android.os.Parcelable {    public static final android.os.Parcelable$Creator *;  }  -keepclassmembers class * implements java.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();  }  -keep class **.R$* {   *;  }  -keepclassmembers class * {      void *(**On*Event);  }  #----------------------------------------------------------------------------    #---------------------------------webview------------------------------------  -keepclassmembers class fqcn.of.javascript.interface.for.webview {     public *;  }  -keepclassmembers class * extends android.webkit.webViewClient {      public void *(android.webkit.WebView, java.lang.String, android.graphics.Bitmap);      public boolean *(android.webkit.WebView, java.lang.String);  }  -keepclassmembers class * extends android.webkit.webViewClient {      public void *(android.webkit.webView, jav.lang.String);  }  #----------------------------------------------------------------------------  #---------------------------------------------------------------------------------------------------</code></pre>    <h2>2</h2>    <p>理解完战略级思想后,我们开始第一部分补充-实体类。实体类由于涉及到与服务端的交互,各种gson的交互如此等等,是要保留的。将你项目中实体类都拎出来,用以下语法进行保留。</p>    <pre>  <code class="language-java">-keep class 你的实体类所在的包.** { *; }</code></pre>    <p>如我的项目下类User的完整路径为:com.demo.login.bean.User, 那我的混淆如下</p>    <pre>  <code class="language-java">#---------------------------------1.实体类---------------------------------    -keep class com.demo.login.bean.** { *; }    #-------------------------------------------------------------------------</code></pre>    <p>当然你的实体类肯定不止这一个,请用上边的方式一一添加,如果你的实体类都在一个包下,那你就幸福了。</p>    <h2>3</h2>    <p>第2部分是第三方包。打开你的build.gradle文件,查看你用了哪些第三方的包。</p>    <pre>  <code class="language-java">dependencies {      compile 'com.github.bumptech.glide:glide:3.7.0'      compile 'org.greenrobot:eventbus:3.0.0'  }</code></pre>    <p>我这里用了glide,eventbus。我去他们的官网把已经写好的混淆copy下来。</p>    <pre>  <code class="language-java">#---------------------------------2.第三方包-------------------------------    #eventBus  -keepattributes *Annotation*  -keepclassmembers class ** {      @org.greenrobot.eventbus.Subscribe <methods>;  }  -keep enum org.greenrobot.eventbus.ThreadMode { *; }  -keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {      <init>(java.lang.Throwable);  }    #glide  -keep public class * implements com.bumptech.glide.module.GlideModule  -keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {    **[] $VALUES;    public *;  }    #-------------------------------------------------------------------------</code></pre>    <p>一般官网都是有混淆的,没有的话就google,也没有的话自己按照上面的写法自己写,还不会的话。。。。。只能换个包。。。。。如果你是直接包含的jar包的话,你这样写</p>    <pre>  <code class="language-java">#---------------------------------2.第三方包-------------------------------    #log4j  -libraryjars log4j-1.2.17.jar  -dontwarn org.apache.log4j.**  -keep class  org.apache.log4j.** { *;}    #-------------------------------------------------------------------------</code></pre>    <p>大致意思就是不混淆,不报warn。如果gradle报错的话,可以考虑注释掉-libraryjars log4j-1.2.17.jar这句。</p>    <h2>4</h2>    <p>第三部分与js互调的类,工程中没有直接跳过。一般你可以这样写</p>    <pre>  <code class="language-java">-keep class 你的类所在的包.** { *; }</code></pre>    <p>如果是内部类的话,你可以这样</p>    <pre>  <code class="language-java">-keepclasseswithmembers class 你的类所在的包.父类$子类 { <methods>; }</code></pre>    <p>例如</p>    <pre>  <code class="language-java">-keepclasseswithmembers class com.demo.login.bean.ui.MainActivity$JSInterface {         <methods>;   }</code></pre>    <h2>5</h2>    <p>第四部分与反射有关的类,工程中没有直接跳过。类的话直接这样</p>    <pre>  <code class="language-java">-keep class 你的类所在的包.** { *; }</code></pre>    <p>熟练后不用五分钟就能搞定。大家可以把自己写完混淆所用的时间写在评论里(0,0)</p>    <p>附:完整的混淆</p>    <pre>  <code class="language-java">#-------------------------------------------定制化区域----------------------------------------------  #---------------------------------1.实体类---------------------------------    -keep class com.demo.login.bean.** { *; }  -keep class com.demo.main.bean.** { *; }    #-------------------------------------------------------------------------    #---------------------------------2.第三方包-------------------------------    #eventBus  -keepattributes *Annotation*  -keepclassmembers class ** {      @org.greenrobot.eventbus.Subscribe <methods>;  }  -keep enum org.greenrobot.eventbus.ThreadMode { *; }  -keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {      <init>(java.lang.Throwable);  }    #glide  -keep public class * implements com.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.**  -keep class  org.apache.log4j.** { *;}    #-------------------------------------------------------------------------    #---------------------------------3.与js互相调用的类------------------------    -keepclasseswithmembers class com.demo.login.bean.ui.MainActivity$JSInterface {         <methods>;   }    #-------------------------------------------------------------------------    #---------------------------------4.反射相关的类和方法-----------------------    #TODO 我的工程里没有。。。    #----------------------------------------------------------------------------  #---------------------------------------------------------------------------------------------------    #-------------------------------------------基本不用动区域--------------------------------------------  #---------------------------------基本指令区----------------------------------  -optimizationpasses 5  -dontusemixedcaseclassnames  -dontskipnonpubliclibraryclasses  -dontskipnonpubliclibraryclassmembers  -dontpreverify  -verbose  -printmapping proguardMapping.txt  -optimizations !code/simplification/cast,!field/*,!class/merging/*  -keepattributes *Annotation*,InnerClasses  -keepattributes Signature  -keepattributes SourceFile,LineNumberTable  #----------------------------------------------------------------------------    #---------------------------------默认保留区---------------------------------  -keep public class * extends android.app.Activity  -keep public class * extends android.app.Appliction  -keep public class * extends android.app.Service  -keep public class * extends android.content.BroadcastReceiver  -keep public class * extends android.content.ContentProvider  -keep public class * extends android.app.backup.BackupAgentHelper  -keep public class * extends android.preference.Preference  -keep public class * extends android.view.View  -keep public class com.android.vending.licensing.ILicensingService  -keep class android.support.** {*;}    -keepclasseswithmembernames class * {      native <methods>;  }  -keepclassmembers class * extends android.app.Activity{      public void *(android.view.View);  }  -keepclassmembers enum * {      public static **[] values();      public static ** valueOf(java.lang.String);  }  -keep public class * extends android.view.View{      *** get*();      void set*(***);      public <init>(android.content.Context);      public <init>(android.content.Context, android.util.AttributeSet);      public <init>(android.content.Context, android.util.AttributeSet, int);  }  -keepclasseswithmembers class * {      public <init>(android.content.Context, android.util.AttributeSet);      public <init>(android.content.Context, android.util.AttributeSet, int);  }  -keep class * implements android.os.Parcelable {    public static final android.os.Parcelable$Creator *;  }  -keepclassmembers class * implements java.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();  }  -keep class **.R$* {   *;  }  -keepclassmembers class * {      void *(**On*Event);  }  #----------------------------------------------------------------------------    #---------------------------------webview------------------------------------  -keepclassmembers class fqcn.of.javascript.interface.for.webview {     public *;  }  -keepclassmembers class * extends android.webkit.webViewClient {      public void *(android.webkit.WebView, java.lang.String, android.graphics.Bitmap);      public boolean *(android.webkit.WebView, java.lang.String);  }  -keepclassmembers class * extends android.webkit.webViewClient {      public void *(android.webkit.webView, jav.lang.String);  }  #----------------------------------------------------------------------------  #---------------------------------------------------------------------------------------------------</code></pre>    <h2>后言</h2>    <h2>将build.gradle中minifyEnabled设置为true打个包试试吧</h2>    <pre>  <code class="language-java">   release {         minifyEnabled true         proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'     }</code></pre>    <p>其他需求可以留言讨论。如果觉得有用请帮忙戳喜欢。。</p>    <h2>感谢</h2>    <p>包老师的《App研发录》</p>    <p><br> <a href="/misc/goto?guid=4959671754649306765">文/wolearn(简书作者)</a><br>  </p>