美文网首页
【Android】App混淆文件配置

【Android】App混淆文件配置

作者: 紫豪 | 来源:发表于2017-10-25 16:50 被阅读197次

    1. 基础的指令配置

    -optimizationpasses 5 # 指定代码的压缩级别
    -dontusemixedcaseclassnames # 混淆后类名都为小写
    -dontskipnonpubliclibraryclasses #指定不去忽略非公共的库的类
    -dontskipnonpubliclibraryclassmembers #指定不去忽略非公共的库的类的成员
    -dontpreverify # 不做预校验的操作
    -verbose # 混淆时是否记录日志
    -printmapping proguardMapping.txt #生成原类名和混淆后的类名的映射文件
    -optimizations !code/simplification/arithmetic,!field/*,!class/merging/*  # 混淆时所采用的算法
    -keepattributes *Annotation*,InnerClasses #不混淆Annotation
    -keepattributes Signature #不混淆泛型
    -keepattributes SourceFile,LineNumberTable #抛出异常时保留代码行号
    -ignorewarnings #抑制警告
    

    2. 一些通用的必要配置

    #系统类不需要混淆
    -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.ILicensingS
    -keep class android.support.** {*;}
    -keep public class * extends android.app.Fragment
    
    -keepclasseswithmembernames class * {  # 保持 native 方法不被混淆
        native <methods>;
    }
    -keepclassmembers class * extends android.app.Activity { # 保持自定义控件类不被混淆
        public void *(android.view.View);
    }
    -keepclassmembers enum * {# 保持枚举 enum 类不被混淆
        public static **[] values();
        public static ** valueOf(java.lang.String);
    }
    -keep public class * extends android.view.View{# 保持View类不被混淆
        *** get*();
        void set*(***);
        public <init>(android.content.Context);
        public <init>(android.content.Context, android.util.AttributeSet);
        public <init>(android.content.Context, android.util.AttributeSet, int);
    }
    -keepclasseswithmembers class * {# 保持自定义控件类不被混淆
        public <init>(android.content.Context, android.util.AttributeSet);
        public <init>(android.content.Context, android.util.AttributeSet, int);
    }
    -keep class * implements android.os.Parcelable { # 保持 Parcelable 不被混淆
        public static final android.os.Parcelable$Creator *;
    }
    -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 *(**On*Event);
    }
    
    #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, java.lang.String);
    }
    
    # 其它
    -keep class com.google.android.gms.** {*;}
    -dontwarn com.google.android.gms.**
    -dontwarn com.viewpagerindicator.*
    -dontwarn com.amazonaws.**
    -dontnote com.google.vending.licensing.ILicensingService
    -dontnote com.android.vending.licensing.ILicensingService
    

    3. 实体类

    # 可能会存在多处,需要一一添加
    -keep class 包名.bean.** {*;}
    

    4. 第三方Library

    # 本地.so库文件声明
    -libraryjars libs/armeabi/xxx.so
    -libraryjars libs/armeabi-v7/xxx.so
    -libraryjars libs/x86/xxx.so
    
    # 常用第三方Library混淆配置
    # 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 *;
    }
    
    # Alipay
    -dontwarn com.alipay.**
    -dontwarn com.ta.utdid2.**
    -dontwarn com.ut.device.**
    -dontwarn android.net.**
    -keep class com.alipay.** {*;}
    -keep class com.ta.utdid2.** {*;}
    -keep class com.ut.device.** {*;}
    -keep class android.net.SSLCertificateSocketFactory
    
    # butterknife
    -keep class butterknife.** {*;}
    -dontwarn butterknife.internal.**
    -keep class **$$ViewBinder {*;}
    -keepclasseswithmembernames class * {
        @butterknife.* <fields>;
    }
    -keepclasseswithmembernames class * {
        @butterknife.* <methods>;
    }
    
    # eventbus
    -keepattributes *Annotation*
    -keepclassmembers class ** {
        @com.google.common.eventbus.Subscribe <methods>;
            public void onEvent*(**);
            void onEvent*(**);
    }
    -keep enum de.greenrobot.event.ThreadMode {*;}
    -keepclassmembers class * extends de.greenrobot.event.util.ThrowableFailureEvent {
        <init>(java.lang.Throwable);
    }
    
    # Gson
    -dontwarn com.google.gson.**
    -keep class com.google.gson.** {*;}
    -keep class sun.misc.Unsafe {*;}
    
    # 科大讯飞相关(ifly_push_sdk && Msc)
    -dontwarn com.iflytek.**
    -keep class com.iflytek.** {*;}
    
    # 腾讯相关(libammsdk && open_sdk)
    -dontwarn com.tencent.**
    -keep class com.tencent.** {*;}
    
    # weibosdkcore
    -dontwarn com.sina.weibo.sdk.**
    -keep class com.sina.weibo.sdk.** {*;}
    
    # Dagger
    -dontwarn dagger.internal.codegen.**
    -dontwarn javax.inject.**
    -keepclassmembers,allowobfuscation class * {
        @javax.inject.* *;
        @dagger.* *;
        <init>();
    }
    -keep class dagger.** {*;}
    -keep class javax.inject.** {*;}
    -keep class * extends dagger.internal.Binding
    -keep class * extends dagger.internal.ModuleAdapter
    -keep class * extends dagger.internal.StaticInjection
    
    

    这里要注意的一点是,要避免重复声明jar包——如果在build.gradle中使用了compile fileTree(include: ['*.jar'], dir: 'libs'),就不需要在混淆文件中添加-libraryjars libs/xxx.jar去声明对应的jar了,否则会报错提示重复添加。

    5. 反射类及其相关方法

    -keep class java.lang.annotation.** {*;}
    

    6. JS相关交互类(方法互调)


    【拓展阅读】
    整理Android最全的混淆规则大全(最新的开源框架混淆)
    Android混淆

    【声明】
    本文部门内容参考自5分钟搞定android混淆,根据实际的需求有所修改整理。

    相关文章

      网友评论

          本文标题:【Android】App混淆文件配置

          本文链接:https://www.haomeiwen.com/subject/yxnqpxtx.html