前言:
该内容只作为开发笔记,较随意,勿喷~
一、在build.gradle(app)文件中新增以下代码块,目的是开启框架自带的proguard代码混淆机制;
buildTypes {
release {
// 不显示Log
buildConfigField "boolean", "LOG_DEBUG", "false"
//混淆
minifyEnabled true
//Zipalign优化
zipAlignEnabled true
// 移除无用的resource文件
shrinkResources true
//加载默认混淆配置文件
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
二、在proguard-rules.pro(app)文件中新增以下混淆规则,实际项目中需按照自己项目要求进行混淆,这里只拿我们项目混淆为例~
#不优化输入的class文件,默认情况下是启用优化的
-dontoptimize
#Proguard对你的代码进行迭代优化的次数 0~7,一直优化到代码不能被优化为止
-optimizationpasses 5
#跳过库中非public的类,可以加快proguard处理速度;但是有些类库包含从public类中继承来的非public类。
-skipnonpubliclibraryclasses
#指定不去忽略包可见的库类的成员。
-dontskipnonpubliclibraryclassmembers
#.确定统一的混淆类的成员名称来增加混淆
-useuniqueclassmembernames
#混淆时不会产生形形色色的类名
-dontusemixedcaseclassnames
#混淆前认证,去掉可加快混淆速度
-dontpreverify
#假如项目中有用到注解,保留注解,不混淆
#-keepattributes *Annotation*
#保留所有的v4包中类不被混淆
-keep class android.support.v4.**
#.保留所有的v4包中接口不被混淆
-keep interface android.support.v4.**
#保留系统中继承v4/v7包的类,不被混淆
-keep class android.support.** { *; }
-keep class android.support.v4.** { *; }
-keep public class * extends android.support.v4.**
-keep interface android.support.v4.app.** { *; }
-keep class android.support.v7.** { *; }
-keep public class * extends android.support.v7.**
-keep interface android.support.v7.app.** { *; }
-dontwarn android.support.**
#保留系统中继实现v4/v7包的接口,不被混淆
-keep public class * implements android.support.v4.**
-dontwarn android.support.v4.**
#.所有的native方法不被混淆
-keepclasseswithmembers class * {
native <methods>;
}
#自定义View构造方法不混淆
-keepclasseswithmembers class * { public <init>(android.content.Context); }
-keepclasseswithmembers class * { public <init>(android.content.Context,android.util.AttributeSet); }
-keepclasseswithmembers class * { public <init>(android.content.Context,android.util.AttributeSet,int); }
#枚举不被混淆
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
#保持 Serializable 不被混淆
-keepnames class * implements java.io.Serializable
-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();
}
#xUtils(保持注解,及使用注解的Activity不被混淆,不然会影响Activity中你使用注解相关的代码无法使用)
-keep class * extends java.lang.annotation.Annotation {*;}
-keep class com.otb.designerassist.activity.** {*;}
###-------- pulltorefresh 相关的混淆配置---------
-dontwarn com.handmark.pulltorefresh.library.**
-keep class com.handmark.pulltorefresh.library.** { *;}
-dontwarn com.handmark.pulltorefresh.library.extras.**
-keep class com.handmark.pulltorefresh.library.extras.** { *;}
-dontwarn com.handmark.pulltorefresh.library.internal.**
-keep class com.handmark.pulltorefresh.library.internal.** { *;}
###--------- reservoir 相关的混淆配置-------
-keep class com.anupcowkur.reservoir.** { *;}
###-------- ShareSDK 相关的混淆配置---------
-keep class cn.sharesdk.** { *; }
-keep class com.sina.sso.** { *; }
###--------------umeng 相关的混淆配置-----------
-keep class com.umeng.** { *; }
-keep class com.umeng.analytics.** { *; }
-keep class com.umeng.common.** { *; }
-keep class com.umeng.newxp.** { *; }
#release版不打印log
#-assumenosideeffects class android.util.Log {
# public static *** d(...);
# public static *** v(...);
# public static *** i(...);
# public static *** e(...);
# public static *** w(...);
# }
#四大组件不能混淆
-dontwarn android.support.v4.** -keep class android.support.v4.app.** { *; }
-keep interface android.support.v4.app.** { *; }
-keep class android.support.v4.** { *; }
-keep public class * extends android.app.Application -dontwarn android.support.v7.**
-keep class android.support.v7.internal.** { *; }
-keep interface android.support.v7.internal.** { *; }
-keep class android.support.v7.** { *; }
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Fragment
-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 com.android.vending.licensing.ILicensingService
# 保持自定义控件类不被混淆
-keepclassmembers class * extends android.app.Activity {public void *(android.view.View);}
# 保持 Parcelable 不被混淆
-keep class * implements android.os.Parcelable {public static final android.os.Parcelable$Creator *;}
#Design包不混淆
-dontwarn android.support.design.** -keep class android.support.design.** { *; }
-keep interface android.support.design.** { *; }
-keep public class android.support.design.R$* { *; }
#确保JavaBean不被混淆-否则Gson将无法将数据解析成具体对象
-keep class com.rair.circle.bean.** { *; }
-keep class com.rair.circle.db.bean.** { *; }
#不混淆资源类
-keepclassmembers class **.R$* {
public static <fields>;
}
#忽略警告
-ignorewarning
#apk包内所有 class 的内部结构、混淆时是否记录日志
-dump class_files.txt
#未混淆的类和成员
-printseeds seeds.txt
#列出从 apk 中删除的代码
-printusage unused.txt
#混淆前后的映射
-printmapping mapping.txt
#忽略第三方jar
#-keep class cz.msebera..** { *; }
#
#-keep class com.google.android.gms.** { *; }
#
#-keep class com.google.gson.** { *; }
#
#-keep class com.fasterxml.jackson.core.** { *; }
#
#-keep class com.fasterxml.jackson.databind.** { *; }
#
#-keep class com.fasterxml.jackson.annotation.** { *; }
#
#-keep class junit.** { *; }
#-keep class org.junit.** { *; }
-keep class com.spdb.itest.entity.** { *; }