Proguard拆分-方便管理

Proguard

最近开始做新项目,需要做代码混淆。就直接从之前的项目中将proguard-rules.pro文件拷贝过来,然后在gradle中配置:

buildTypes {
    release {
        minifyEnabled true
        zipAlignEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), "$rootDir/config/proguard-rules.pro"
    }
}

存在的不足

像我这种情况,新启动的一个项目,需要Proguard。但是如果写在一个文件中,而且没有任何注释,如果还没有写在一块,拷贝过来还是需要一个个去检查是否需要,去掉了某个库的混淆,增加了某些库的混淆。

比如我之前用的是Retrofit1,在proguard-rules.pro配置:

# Retrofit 1.X

-keep class com.squareup.okhttp.** { *; }
-keep class retrofit.** { *; }
-keep interface com.squareup.okhttp.** { *; }

-dontwarn com.squareup.okhttp.**
-dontwarn okio.**
-dontwarn retrofit.**
-dontwarn rx.**

-keepclasseswithmembers class * {
    @retrofit.http.* <methods>;
}

# If in your rest service interface you use methods with Callback argument.
-keepattributes Exceptions

# If your rest service methods throw custom exceptions, because you've defined an ErrorHandler.
-keepattributes Signature

# Also you must note that if you are using GSON for conversion from JSON to POJO representation, you must ignore those POJO classes from being obfuscated.
# Here include the POJO's that have you have created for mapping JSON response to POJO for example.

现在我升级到retrofit2,由于retrofit2库做了修改,Proguard规则也需要更新为:

# Retrofit 2.X
## https://square.github.io/retrofit/ ##

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

-keepclasseswithmembers class * {
    @retrofit2.http.* <methods>;
}

如果存在多个这样的库,那是不是很增加工作量。下面我们换一种姿势来看。

换一种姿势

参考库android-proguard-snippets 在项目的根目录建立config/proguard-pro/目录,将所有第三方的库和一些通用的配置全部配置在里面:

  • 基本不用动的规则

    proguard-normal.pro

    ## Proguard normal ##
    
    # ============================== 基本不用动区域 ==============================
    
    # ------------------------------- 基本指令区 -------------------------------
    
    # 代码混淆的压缩比例(0-7) , 默认为5 , 一般不需要改
    -optimizationpasses 5
    
    # 混淆后类名都小写 (windows最后加上 , 因为windows大小写敏感)
    -dontusemixedcaseclassnames
    
    # 指定不去忽略非公共的库的类(即混淆第三方, 第三方库可能自己混淆了 , 可在后面配置某些第三方库不混淆)
    # 默认跳过,有些情况下编写的代码与类库中的类在同一个包下,并且持有包中内容的引用,此时就需要加入此条声明
    -dontskipnonpubliclibraryclasses
    
    # 指定不去忽略非公共的库的类的成员
    -dontskipnonpubliclibraryclassmembers
    
    # 不做预检验,preverify是proguard的四个步骤之一
    # Android不需要preverify,去掉这一步可以加快混淆速度
    -dontpreverify
    
    # 有了verbose这句话,混淆后就会生成映射文件
    # 包含有类名->混淆后类名的映射关系
    # 然后使用printmapping指定映射文件的名称
    -verbose
    -printmapping proguardMapping.txt
    
    # 指定混淆时采用的算法,后面的参数是一个过滤器
    # 这个过滤器是谷歌推荐的算法,一般不改变
    -optimizations !code/simplification/cast,!field/*,!class/merging/*
    
    # 保护代码中的Annotation不被混淆
    # 这在JSON实体映射时非常重要,比如fastJson
    -keepattributes *Annotation*,InnerClasses
    
    -keep public class com.google.vending.licensing.ILicensingService
    -keep public class com.android.vending.licensing.ILicensingService
    
    # 避免混淆泛型
    # 这在JSON实体映射时非常重要,比如fastJson
    -keepattributes Signature
    
    #抛出异常时保留源文件和代码行号
    -keepattributes SourceFile,LineNumberTable
    # ------------------------------- 基本指令区 -------------------------------
    
    # ------------------------------- 默认保留区 -------------------------------
    
    # 保留四大组件
    -keep public class * extends android.app.Activity
    # 保留就保证layout中定义的onClick方法不影响
    # We want to keep methods in Activity that could be used in the XML attribute onClick
    -keepclassmembers class * extends android.app.Activity{
        public void *(android.view.View);
    }
    -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.Application
    
    -keep public class * extends android.app.backup.BackupAgentHelper
    -keep public class * extends android.preference.Preference
    
    # For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
    # 保留类名和native成员方法
    -keepclasseswithmembernames class * {
        native <methods>;
    }
    
    # 枚举类不能被混淆
    # # For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
    -keepclassmembers enum * {
        public static **[] values();
        public static ** valueOf(java.lang.String);
    }
    
    # 保留自定义控件(继承自View)的setter、getter和构造方法
    # keep setters in Views so that animations can still work.
    # see http://proguard.sourceforge.net/manual/examples.html#beans
    -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);
        *** get*();
        void set*(***);
    }
    
    # 保留Parcelable序列化的类不能被混淆
    #-keep class * implements android.os.Parcelable {
    #  public static final android.os.Parcelable$Creator *;
    #}
    # 官方
    -keepclassmembers class * implements android.os.Parcelable {
      public static final android.os.Parcelable$Creator CREATOR;
    }
    
    # 所有实现了 Serializable 接口的类及其成员都不进行混淆
    -keepnames class * implements java.io.Serializable
    -keepclassmembers class * implements java.io.Serializable {
        static final long serialVersionUID;
        private static final java.io.ObjectStreamField[] serialPersistentFields;
        !static !transient <fields>;
        private void writeObject(java.io.ObjectOutputStream);
        private void readObject(java.io.ObjectInputStream);
        java.lang.Object writeReplace();
        java.lang.Object readResolve();
    }
    
    # 对R文件下的所有类及其方法 , 都不能被混淆
    #-keep class **.R$* {
    # *;
    #}
    # 官方
    -keepclassmembers class **.R$* {
        public static <fields>;
    }
    
    # The support library contains references to newer platform versions.
    # Don't warn about those in case this app is linking against an older
    # platform version.  We know about them, and they are safe.
    -dontwarn android.support.**
    
    # Understand the @Keep support annotation.
    -keep class android.support.annotation.Keep
    
    -keep @android.support.annotation.Keep class * {*;}
    
    -keepclasseswithmembers class * {
        @android.support.annotation.Keep <methods>;
    }
    
    -keepclasseswithmembers class * {
        @android.support.annotation.Keep <fields>;
    }
    
    -keepclasseswithmembers class * {
        @android.support.annotation.Keep <init>(...);
    }
    
    # ------------------------------- 默认保留区 end-------------------------------
    
    #------------------------------- 以上内容基本是SDK目录下的proguard-android-optimize.txt内容 ------------------------------- #
    
    # ------------------------------- webview相关 -------------------------------
    
    -dontwarn android.webkit**
    
    # WebView(可选)
    -keepclassmembers class * extends android.webkit.WebView {
       public *;
    }
    
    # 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.WebChromeClient {
         public void *(android.webkit.WebView,java.lang.String);
    }
    
    # 与JS交互
    -keepattributes SetJavaScriptEnabled
    -keepattributes JavascriptInterface
    
    # 保留与JS交互接口 , API17+
    -keepclassmembers class * {
        @android.webkit.JavascriptInterface <methods>;
    }
    
    # ------------------------------- webview相关 end -------------------------------
    
    -dontwarn org.apache.**
    
    # ============================== 基本不动区域 end ==============================
    

  • okhttp3

    proguard-square-okhttp3.pro

    # OkHttp3 specific rules #
    
    -keepattributes Signature
    -keepattributes *Annotation*
    -keep class okhttp3.** { *; }
    -keep interface okhttp3.** { *; }
    -dontwarn okhttp3.**
    
  • eventbus3

    proguard-eventbus-3.pro

    ## EventBus3  specific rules ##
    # http://greenrobot.org/eventbus/documentation/proguard/
    
    -keepattributes *Annotation*
    -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);
    }
    

最后在proguard-rules.pro将他们全部导入进来:

-basedirectory proguard-pro

-include proguard-normal.pro

-include proguard-google.pro
-include proguard-google-gson2.pro
-include proguard-google-protobuf.pro
-include proguard-google-volley.pro

-include proguard-eventbus-3.pro

-include proguard-facebook-stetho.pro

-include proguard-glide.pro

-include proguard-leakcanary.pro

-include proguard-jpush.pro

-include proguard-qiniu.pro

-include proguard-tencent-bugly.pro

-include proguard-umeng.pro

-include proguard-baidu-map.pro

如果后面替换了某个库,或者增加了某个库,直接在这个文件注释或者增加一条-include就行了。

build.gradle中配置:

buildTypes {
    release {
        minifyEnabled true
        zipAlignEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), "$rootDir/config/proguard-rules.pro"
    }
}

当然也可以采用这种方式,但这种会导致build.gradle文件庞大:

android {
  buildTypes {
    release {
      minifyEnabled true
      // Library specific proguard files
      proguardFile 'proguard-google-play-services.pro'
      proguardFile 'proguard-gson.pro'
      ...
      // Default proguard files & project app specific rules,
      //  see examples folder for more information
      proguardFile 'proguard-project-app.pro'
      proguardFile getDefaultProguardFile('proguard-android.txt')
      // As of Gradle Android plugin 1.1.0, the test APK has a separate config
      testProguardFile 'proguard-project-test.pro'
    }
  }
}

完整见源码

blog

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 151,829评论 1 331
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 64,603评论 1 273
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 101,846评论 0 226
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 42,600评论 0 191
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 50,780评论 3 272
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 39,695评论 1 192
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,136评论 2 293
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 29,862评论 0 182
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,453评论 0 229
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 29,942评论 2 233
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,347评论 1 242
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 27,790评论 2 236
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,293评论 3 221
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,839评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,448评论 0 181
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 34,564评论 2 249
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 34,623评论 2 249

推荐阅读更多精彩内容