Android项目混淆配置

proguard-rules.pro文件中配置

# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in D:\work\Develop_tool\android-sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
#   public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in D:\work\Develop_tool\android-sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
#   public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
-renamesourcefileattribute SourceFile

#忽略警告
-ignorewarning

#指定代码的压缩级别,在0~7之间,默认为5,一般不下需要修改
-optimizationpasses 5

#包名不混合大小写,混淆后的类名为小写
# windows下的同学还是加入这个选项吧(windows大小写不敏感)
-dontusemixedcaseclassnames

#不去忽略非公共的库类
# 默认跳过,有些情况下编写的代码与类库中的类在同一个包下,并且持有包中内容的引用,此时就需要加入此条声明
-dontskipnonpubliclibraryclasses

# 指定不去忽略非公共的库的类的成员
#-dontskipnonpubliclibraryclassmembers

#不进行压缩,保证是独立的jar,没有任何项目引用,如果不写就会认为我们所有的代码时无用的,
#从而把所有的代码压缩掉,导出一个空的jar
#库的aar或者jar必须要加上这个
#-dontshrink

 #优化  不优化输入的类文件
-dontoptimize

#预校验,不做预检验,preverify是proguard的四个步骤之一
# Android不需要preverify,去掉这一步可以加快混淆速度
-dontpreverify

#把混淆类中的方法名也混淆了
#-useuniqueclassmembernames

#优化时允许访问并修改有修饰符的类和类的成员
#-allowaccessmodification

#混淆时是否记录日志
 # 有了verbose这句话,混淆后就会生成映射文件
 # 包含有类名->混淆后类名的映射关系
 # 然后使用printmapping指定映射文件的名称
-verbose

# 混淆时所采用的算法,后面的参数是一个过滤器
 # 这个过滤器是谷歌推荐的算法,一般不改变
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

#指定受保护的属性
# 可以直接写在一行
-keepattributes Exceptions, InnerClasses, Signature, Deprecated,SourceFile, LineNumberTable, *Annotation*, EnclosingMethod,*JavascriptInterface*

#指定受保护的属性
#保护注解
# 这在JSON实体映射时非常重要,比如fastJson
#-keepattributes *Annotation*,InnerClasses

# 抛出异常时保留代码行号
#-keepattributes SourceFile,LineNumberTable

# 保持哪些类不被混淆
#因为这些子类有可能被外部调用
-keep public class * extends android.app.Fragment
-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 com.android.vending.licensing.ILicensingService
#如果有引用v4包可以添加下面这行
-keep public class * extends android.support.v4.app.Fragment
-keep public class * extends android.support.annotation.**
-keep public class * extends android.support.v7.**

# 保留Activity中的方法参数是view的方法,
# 从而我们在layout里面编写onClick就不会影响
-keepclassmembers class * extends android.app.Activity {
    public void * (android.view.View);
}

-keep public class * extends android.widget.BaseAdapter {*;}

##记录生成的日志数据,gradle build时在本项目根目录输出##
#apk 包内所有 class 的内部结构
-dump proguard/class_files.txt
#未混淆的类和成员
-printseeds proguard/seeds.txt
#列出从 apk 中删除的代码
-printusage proguard/unused.txt
#混淆前后的映射
-printmapping proguard/mapping.txt
########记录生成的日志数据,gradle build时 在本项目根目录输出-end######

#如果引用了v4或者v7包
-dontwarn android.support.**

####混淆保护自己项目的部分代码以及引用的第三方jar包library-end####

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

#保持自定义控件类不被混淆
-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

#保持自定义控件类(继承自View)不被混淆
-keep public class * extends android.view.View {
    public <init>(android.content.Context);
    public <init>(android.content.Context, android.util.AttributeSet);
    public <init>(android.content.Context, android.util.AttributeSet, int);
    public void set*(...);
    *** get* ();
}

#WebView的处理
-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, java.lang.String);
}

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

#保持 Serializable 不被混淆
-keepnames class * implements java.io.Serializable

#保持 Serializable 不被混淆并且enum 类也不被混淆
-keepclassmembers class * implements java.io.Serializable {
    static final long serialVersionUID;
    private static final java.io.ObjectStreamField[] serialPersistentFields;
    !static !transient <fields>;
    !private <fields>;
    !private <methods>;
    private void writeObject(java.io.ObjectOutputStream);
    private void readObject(java.io.ObjectInputStream);
    java.lang.Object writeReplace();
    java.lang.Object readResolve();
}

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

#不混淆资源类,对R文件下的所有类及其方法,都不能被混淆
-keepclassmembers class **.R$* {
    public static <fields>;
}

# 保持测试相关的代码
-dontnote junit.framework.**
-dontnote junit.runner.**
-dontwarn android.test.**
-dontwarn android.support.test.**
-dontwarn org.junit.**

#避免混淆泛型 如果混淆报错建议关掉
# 这在JSON实体映射时非常重要,比如fastJson
#-keepattributes Signature

#移除Log类打印各个等级日志的代码,打正式包的时候可以做为禁log使用,这里可以作为禁止log打印的功能使用,另外的一种实现方案是通过BuildConfig.DEBUG的变量来控制
#-assumenosideeffects class android.util.Log {
#    public static *** v(...);
#    public static *** i(...);
#    public static *** d(...);
#    public static *** w(...);
#    public static *** e(...);
#}

#############################################################################################
########################                 以上通用           ##################################
#############################################################################################


########>>>>>>>>>>>>>>>>>>>     以下常用第三方模块的混淆选项      <<<<<<<<<<<<<<<<<##############

#############GSON#############
#https://github.com/google/gson/blob/master/examples/android-proguard-example/proguard.cfg
#如果用用到Gson解析包的,直接添加下面这几行就能成功混淆,不然会报错。
#gson
#如果用用到Gson解析包的,直接添加下面这几行就能成功混淆,不然会报错。
-keepattributes Signature
# Gson specific classes
-dontwarn sun.misc.**
-keep class sun.misc.Unsafe { *; }
# Application classes that will be serialized/deserialized over Gson
-dontwarn com.google.gson.**
-keep class com.google.gson.** { *; }
-keep class com.google.gson.stream.** { *; }

# Prevent proguard from stripping interface information from TypeAdapterFactory,
# JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer

##---------------End: proguard configuration for Gson  ----------

#############mob#############
-keep class android.net.http.SslError
-keep class android.webkit.**{*;}
-keep class cn.sharesdk.**{*;}
-keep class com.sina.**{*;}
-keep class m.framework.**{*;}
-keep class **.R$* {*;}
-keep class **.R{*;}
#忽略警告,保持cn.sharesdk.**这个包里面的所有类和所有方法不被混淆。
-dontwarn cn.sharesdk.**
-dontwarn com.sina.**
-dontwarn **.R$*

#############butterknife#############
#https://github.com/JakeWharton/butterknife/blob/master/butterknife/proguard-rules.txt
-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**
-keep class **$$ViewBinder { *; }

-keepclasseswithmembernames class * {
    @butterknife.* <fields>;
}

-keepclasseswithmembernames class * {
    @butterknife.* <methods>;
}

# Retain generated class which implement Unbinder.
-keep public class * implements butterknife.Unbinder { public <init>(**, android.view.View); }


#############OKHTTP3#############
#https://github.com/square/okhttp
-dontwarn okhttp3.**
-dontwarn com.squareup.okhttp3.**
-keep class com.squareup.okhttp3.** { *;}
-keep class okhttp3.**{*;}

-dontwarn okio.**
-keep class okio.**{*;}
-dontwarn javax.annotation.**
# A resource is loaded with a relative path so the package of this class must be preserved.
-keepnames class okhttp3.internal.publicsuffix.PublicSuffixDatabase


#############AndroidUtilCode#############
#https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/README-CN.md
-keep class com.blankj.utilcode.** { *; }
-keepclassmembers class com.blankj.utilcode.** { *; }
-dontwarn com.blankj.utilcode.**


#############greenDAO3#############
#http://greenrobot.org/greendao/documentation/updating-to-greendao-3-and-annotations/
-dontwarn org.greenrobot.greendao.**
-keep class org.greenrobot.greendao.**{*;}
-keepclassmembers class * extends org.greenrobot.greendao.AbstractDao {
public static java.lang.String TABLENAME;
}
#-keepclassmembers class * extends org.greenrobot.greendao.AbstractDao {
   #public static void dropTable(org.greenrobot.greendao.database.Database, boolean);
   #public static void createTable(org.greenrobot.greendao.database.Database, boolean);
#}

-keep class **$Properties

# If you do not use SQLCipher:
-dontwarn org.greenrobot.greendao.database.**
# If you do not use Rx:
-dontwarn rx.**

#############RxJava RxAndroid#############
-dontwarn sun.misc.**
-dontwarn rx.internal.util.unsafe.**
-keepclassmembers class rx.internal.util.unsafe.*ArrayQueue*Field* {
   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;
}

-dontnote rx.internal.util.PlatformDependent


#############Glide#############
#https://github.com/bumptech/glide
-dontwarn com.bumptech.glide.**
-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public class * extends com.bumptech.glide.module.AppGlideModule
-keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {
  **[] $VALUES;
  public *;
}

#############EventBus#############
#http://greenrobot.org/eventbus/documentation/proguard
-keepclassmembers class ** {
    @org.greenrobot.eventbus.Subscribe <methods>;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }

# Only required if you use AsyncExecutor
-keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
    <init>(java.lang.Throwable);
}

# compile 'org.greenrobot:eventbus:3.0.0'
-keep class org.greenrobot.eventbus.** { *; }
-keep interface org.greenrobot.eventbus.** { *; }
-dontwarn org.greenrobot.eventbus.**


#############Retrofit#############
#https://github.com/square/retrofit
-dontwarn retrofit2.**
-keep class retrofit2.** { *; }
-keepattributes Signature
-keepattributes Exceptions
-dontwarn okio.**
-dontwarn javax.annotation.**

#############################################################################################
########################                 以上第三方           ###############################
#############################################################################################


######引用的其他Module可以直接在app的这个混淆文件里配置


#####混淆保护自己项目的部分代码以及引用的第三方jar包library#######
#如果在当前的application module或者依赖的library module中使用了第三方的库,并不需要显式添加规则
#-libraryjars xxx
#添加了反而有可能在打包的时候遭遇同一个jar多次被指定的错误,一般只需要添加忽略警告和保持某些class不被混淆的声明。
#以libaray的形式引用了开源项目,如果不想混淆 keep 掉,在引入的module的build.gradle中设置minifyEnabled=false
#-keep class com.nineoldandroids.** { *; }
#-keep interface com.nineoldandroids.** { *; }
#-dontwarn com.nineoldandroids.**
## 下拉刷新
#-keep class in.srain.cube.** { *; }
#-keep interface in.srain.cube.** { *; }
#-dontwarn in.srain.cube.**
## observablescrollview:tab fragment
#-keep class com.github.ksoichiro.** { *; }
#-keep interface com.github.ksoichiro.** { *; }
#-dontwarn com.github.ksoichiro.**
#保护引用的第三方jar包不被混淆,在使用Eclipse+ADT时需要加入-libraryjars
#-libraryjars libs/baidumapapi_v3_2_0.jar



#############################################################################################
########################                 以下自己项目           #############################
#############################################################################################


#用到GSON解析的java Bean类不能混淆
#如果使用了Gson之类的工具要使被它解析的JavaBean类即实体类不被混淆。
# Application classes that will be serialized/deserialized over Gson
#例如 -keep class 包名.** { *; }
#例如 -keep class com.matrix.app.entity.json.** { *; }
#例如 -keep class com.matrix.appsdk.network.model.** { *; }
-keep class **.bean.** { *; }

#在不想混淆的方法、成员变量、类上使用@Keep注解
-keepclasseswithmembers class * {
    @android.support.annotation.Keep <methods>;
}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <fields>;
}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <init>(...);
}

 build.gradle文件中配置

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值