關於AndroidStudio混淆配置

小二陽發表於2017-12-14

1.首先需要在app下的build.gradle下配置,true表示編譯時會混淆程式碼\

Paste_Image.png

Paste_Image.png

2.在proguard-rules.pro中配置,android常用的混淆配置

對於一些基本指令的新增############################################### 程式碼混淆壓縮比,在0~7之間,預設為5,一般不做修改

-optimizationpasses 5

混合時不使用大小寫混合,混合後的類名為小寫

-dontusemixedcaseclassnames

指定不去忽略非公共庫的類

-dontskipnonpubliclibraryclasses

這句話能夠使我們的專案混淆後產生對映檔案# 包含有類名->混淆後類名的對映關係

-verbose

指定不去忽略非公共庫的類成員

-dontskipnonpubliclibraryclassmembers

不做預校驗,preverify是proguard的四個步驟之一,Android不需要preverify,去掉這一步能夠加快混淆速度。

-dontpreverify

保留Annotation不混淆

-keepattributes Annotation,InnerClasses

避免混淆泛型

-keepattributes Signature

丟擲異常時保留程式碼行號

-keepattributes SourceFile,LineNumberTable

指定混淆是採用的演算法,後面的引數是一個過濾器

這個過濾器是谷歌推薦的演算法,一般不做更改

-optimizations !code/simplification/cast,!field/,!class/merging/ ############################################### Android開發中一些需要保留的公共部分############################################### 保留我們使用的四大元件,自定義的Application等等這些類不被混淆# 因為這些子類都有可能被外部呼叫 -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

保留support下的所有類及其內部類

-keep class android.support.** {*;}

保留繼承的

-keep public class * extends android.support.v4.** -keep public class * extends android.support.v7.** -keep public class * extends android.support.annotation.**

保留R下面的資源

-keep class *.R$ {*;}

保留本地native方法不被混淆

-keepclasseswithmembernames class * { native ;}

保留在Activity中的方法引數是view的方法,

這樣以來我們在layout中寫的onClick就不會被影響-keepclassmembers class * extends android.app.Activity{ public void *(android.view.View);}

保留列舉類不被混淆

-keepclassmembers enum * { public static **[] values(); public static ** valueOf(java.lang.String);}

保留我們自定義控制元件(繼承自View)不被混淆

-keep public class * extends android.view.View{ *** get*(); void set*(***); public (android.content.Context); public (android.content.Context, android.util.AttributeSet); public (android.content.Context, android.util.AttributeSet, int);}

保留Parcelable序列化類不被混淆

-keep class * implements android.os.Parcelable { public static final android.os.Parcelable$Creator *;}

保留Serializable序列化的類不被混淆

-keepclassmembers class * implements java.io.Serializable { static final long serialVersionUID; private static final java.io.ObjectStreamField[] serialPersistentFields; !static !transient ; !private ; !private ; private void writeObject(java.io.ObjectOutputStream); private void readObject(java.io.ObjectInputStream); java.lang.Object writeReplace(); java.lang.Object readResolve();}

對於帶有回撥函式的onXXEvent、**On*Listener的,不能被混淆

-keepclassmembers class * { void (**OnEvent); void (**OnListener);}

webView處理,專案中沒有使用到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); }

移除Log類列印各個等級日誌的程式碼,打正式包的時候可以做為禁log使用,這裡可以作為禁止log列印的功能使用

記得proguard-android.txt中一定不要加-dontoptimize才起作用

另外的一種實現方案是通過BuildConfig.DEBUG的變數來控制

#-assumenosideeffects class android.util.Log { public static int v(...); public static int i(...); public static int w(...); public static int d(...); public static int e(...);

某個包下的類不用混淆

#-------------處理本應用不需要混淆的程式碼------------------- keep class com.android.smarttouch.ctrl.** { *; }

網路上有許多混淆規則的程式碼,看官方文件最好:gradle example,這裡面有對各種sdk和library混淆時候時已經寫好的混淆程式碼,直接去抄裡面的示例程式碼就好。混淆時最麻煩的幾點就是: 第三方lib包的混淆(不再有警告提醒)比如對apache的common包,dom4j的混淆,可以不用混淆 ############################################## 不警告 -dontwarn org.dom4j.**

不混淆

-keep class org.dom4j.** { ; } -dontwarn org.apache.* -keep class org.apache.** { ; } -dontwarn org.xmlpull.* -keep class org.xmlpull.** { ; } -dontwarn com.jcraft.* -keep class com.jcraft.** { *; }

3.不能混淆的程式碼

  顧名思義,不能混淆程式碼如果被混淆了,就會出現錯誤。

  1)需要反射的程式碼
  2)系統介面
  3)Jni介面
  4)需要序列號和反序列化的程式碼(即實現Serializable介面的JavaBean)
  5)與服務端進行後設資料互動的JavaBean(JSON、XML中對應的類)
複製程式碼

4.常見錯誤

  1. Proguard returned with error code 1. See console 更新proguard版本  Android-support-v4 不進行混淆  新增缺少相應的庫

  2. 使用gson包解析資料時,出現 missing type parameter 異常

    在 proguard-project.txt 中新增 -dontobfuscate -dontoptimize 在 proguard-project.txt 中新增

    removes such information by default, so configure it to keep all of it.

    -keepattributes Signature

    Gson specific classes

    -keep class sun.misc.Unsafe { ; } #-keep class com.google.gson.stream. { ; }

    Application classes that will be serialized/deserialized over Gson

-keep class com.google.gson.examples.android.model.* { ; } 3) 型別轉換錯誤 在 proguard-project.txt 中新增  -keepattributes Signature

  1. 空指標異常 混淆過濾掉相關類與方法

  2. java.lang.reflect.UndeclaredThrowableException -keep interface com.dev.impl.**

  3. Error: Unable to access jarfile ..libproguard.jar 路徑問題

  4. java.lang.NoSuchMethodError 這也是最常見的問題,因為找不到相關方法,方法被混淆了,混淆過濾掉相關方法便可。

5.示例:

#---------------------------------1.實體類--------------------------------- -keep class com.package.bean.** { *; }

#-------------------------------------------------------------------------

#---------------------------------2.第三方包-------------------------------

#eventBus -keepattributes Annotation -keepclassmembers class ** { @org.greenrobot.eventbus.Subscribe ; } -keep enum org.greenrobot.eventbus.ThreadMode { *; } -keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent { (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 ; } -keep class com.bumptech.glide. { *; }

#retrofit2 -dontwarn retrofit2.** -keep class retrofit2.** { *; } -keepattributes Signature -keepattributes Exceptions

-dontwarn org.robovm.** -keep class org.robovm.** { *; }

#okhttp3 -dontwarn com.squareup.okhttp3.** -keep class com.squareup.okhttp3.** { ;} -keep class okhttp3.* { ;} -keep class okio.* { ;} -dontwarn sun.security.* -keep class sun.security.** { ;} -dontwarn okio.* -dontwarn okhttp3.**

#rxjava -dontwarn rx.** -keep class rx.** { *; }

-dontwarn sun.misc.** -keepclassmembers class rx.internal.util.unsafe.ArrayQueueField* { long producerIndex; long consumerIndex; } -keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueProducerNodeRef { rx.internal.util.atomic.LinkedQueueNode producerNode; } -keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueConsumerNodeRef { rx.internal.util.atomic.LinkedQueueNode consumerNode; }

#baidu -keep class com.baidu.** { ; } -dontwarn com.baidu.*

#alipay -keep class com.alipay.android.app.IAlixPay{;} -keep class com.alipay.android.app.IAlixPay$Stub{;} -keep class com.alipay.android.app.IRemoteServiceCallback{;} -keep class com.alipay.android.app.IRemoteServiceCallback$Stub{;} -keep class com.alipay.sdk.app.PayTask{ public ;} -keep class com.alipay.sdk.app.AuthTask{ public ;} -keep class com.alipay.mobilesecuritysdk. -keep class com.ut.

-dontwarn android.net.** -keep class android.net.** { *; }

#gson -keep class com.google.gson.** {;} #-keep class com.google.**{;} -keep class sun.misc.Unsafe { ; } -keep class com.google.gson.stream.* { ; } -keep class com.google.gson.examples.android.model.* { ; } -keep class com.google.* { ; ; } -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(); } -dontwarn com.google.gson.**

#umeng -dontwarn com.umeng.** -keep class com.umeng.{*;} -keep class u.aly.{;} -keep class com.google.**{;}

#butterknife -keep class butterknife.** { ; } -dontwarn butterknife.internal.* -keep class **$$ViewBinder { *; }

-keepclasseswithmembernames class * { @butterknife.* ; }

-keepclasseswithmembernames class * { @butterknife.* ; }

#pinyin4j -dontwarn net.soureceforge.pinyin4j.** -dontwarn demo.** -libraryjars src/libs/pinyin4j-2.5.0.jar -keep class net.sourceforge.pinyin4j.** { ;} -keep class demo.* { ;} -keep class com.hp.* { *;}

#httpclient (org.apache.http.legacy.jar) -dontwarn android.net.compatibility.** -dontwarn android.net.http.** -dontwarn com.android.internal.http.multipart.** -dontwarn org.apache.commons.** -dontwarn org.apache.http.** -dontwarn org.apache.http.protocol.** -keep class android.net.compatibility.{*;} #-keep class android.net.http.{;} -keep class com.android.internal.http.multipart.**{;} -keep class org.apache.commons.{*;} -keep class org.apache.org.{;} -keep class org.apache.harmony.**{;}

#lib-wheel -dontwarn kankan.wheel.** -keep class kankan.wheel.**{*;}

#PhotoPicker -dontwarn me.iwf.photopicker.** -keep class me.iwf.photopicker.**{*;}

#nineoldandroids -dontwarn com.nineoldandroids.* -keep class com.nineoldandroids.** { *;}

#weixin -dontwarn com.tencent.mm.** -keep class com.tencent.mm.**{*;}

#topsnackbar -dontwarn com.androidadvance.topsnackbar.** -keep class com.androidadvance.topsnackbar.**{*;}

#pull_recyclerview_library -dontwarn com.cundong.recyclerview.** -keep class com.cundong.recyclerview.**{*;}

#-------------------------------------------------------------------------

#---------------------------------3.與js互相呼叫的類------------------------

#-------------------------------------------------------------------------

#---------------------------------4.反射相關的類和方法-----------------------

#----------------------------------------------------------------------------

#-------------------------------------------基本不用動區域-------------------------------------------- #---------------------------------基本指令區---------------------------------- -optimizationpasses 5 -dontskipnonpubliclibraryclassmembers -printmapping proguardMapping.txt -optimizations !code/simplification/cast,!field/,!class/merging/ -keepattributes Annotation,InnerClasses -keepattributes Signature -keepattributes SourceFile,LineNumberTable #-ignorewarnings #----------------------------------------------------------------------------

#---------------------------------預設保留區--------------------------------- -keep public class * extends android.app.Activity -keep public class * extends android.app.Application -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.** {*;} -keep public class * extends android.os.IInterface

-keep public class * extends android.view.View{ *** get*(); void set*(***); public (android.content.Context); public (android.content.Context, android.util.AttributeSet); public (android.content.Context, android.util.AttributeSet, int); } -keepclasseswithmembers class * { public (android.content.Context, android.util.AttributeSet); public (android.content.Context, android.util.AttributeSet, int); } -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 (**OnEvent); }

-keepclasseswithmembernames class * { # 保持 native 方法不被混淆 native ; }

-keepclasseswithmembers class * { # 保持自定義控制元件類不被混淆 public (android.content.Context, android.util.AttributeSet); }

-keepclasseswithmembers class * {# 保持自定義控制元件類不被混淆 public (android.content.Context, android.util.AttributeSet, int); }

-keepclassmembers class * extends android.app.Activity { # 保持自定義控制元件類不被混淆 public void *(android.view.View); }

-keepclassmembers enum * { # 保持列舉 enum 類不被混淆 public static **[] values(); public static ** valueOf(java.lang.String); }

-keep class * implements android.os.Parcelable { # 保持 Parcelable 不被混淆 public static final android.os.Parcelable$Creator *; }

#----------------------------------------------------------------------------

#---------------------------------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); } #---------------------------------------------------------------------------- #---------------------------------------------------------------------------------------------------

相關文章