1.首先需要在app下的build.gradle下配置,true表示编译时会混淆代码\
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
保留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
对于带有回调函数的onXXEvent、*OnListener的,不能被混淆
-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.常见错误
Proguard returned with error code 1. See console
更新proguard版本
Android-support-v4 不进行混淆
添加缺少相应的库-
使用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.* { ; }
类型转换错误
在 proguard-project.txt 中添加
-keepattributes Signature空指针异常
混淆过滤掉相关类与方法java.lang.reflect.UndeclaredThrowableException
-keep interface com.dev.impl.**Error: Unable to access jarfile ..libproguard.jar
路径问题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 {
}
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
public
public
}
-keepclasseswithmembers class * {
public
public
}
-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
}
-keepclasseswithmembers class * {# 保持自定义控件类不被混淆
public
}
-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);
}