【Android 安全】DEX 加密 ( Proguard 简介 | 默认 ProGuard 分析 )





一、Proguard 配置简介



更多 ProGuard 混淆配置参考 : https://www.guardsquare.com/en/products/proguard/manual/usage


1 . 不进行优化 :

# 不要进行优化 
-dontoptimize

2 . 混淆大小写 : 不要使用混合大小写类名进行混淆 , 混淆后的名称全部都是小写 , 增加阅读难度

# 不要使用混合大小写类名进行混淆 , 混淆后的名称全部都是小写 , 增加阅读难度 
-dontusemixedcaseclassnames

3 . 保留反射属性 : 保留一些反射中可能用到的属性

# 保留一些反射中可能用到的属性 
-keepattributes *Annotation*,Signature,InnerClasses,EnclosingMethod


4 . 保留这些类和类成员 :

# 保留这些类和类成员 
-keep public class com.google.vending.licensing.ILicensingService

5 . 控制日志输出 : -dontnote , 控制编译时不在 Build 对话框输出一些日志信息 ;

# 控制编译时不在 Build 对话框输出一些日志信息 
-dontnote com.android.vending.licensing.ILicensingService

6 . Native 函数混淆设置 :

# 不混淆 Native 函数
# http://proguard.sourceforge.net/manual/examples.html#native
-keepclasseswithmembernames class * {
    native <methods>;
}

7 . 保留类成员 , 包括成员函数 和 成员变量 :

# 不要混淆 Activity 及 子类的 成员 , 以防在 XML 的 onCLick 属性中用到 .
-keepclassmembers class * extends android.app.Activity {
    public void *(android.view.View);
}

8 . 保留注解 : 保留 android.support.annotation.Keep 注解类 , 不被混淆 ;

# 保留注解 
-keep class android.support.annotation.Keep

9 . 保留被注解声明的类 : 被 @android.support.annotation.Keep 注解修饰的类不被混淆 ;

# 保留被 @android.support.annotation 注解声明的类 
-keep @android.support.annotation.Keep class * {*;}

10 . 保留被注解声明的函数 : 被 @android.support.annotation.Keep 注解修饰的函数不被混淆 ;

# 保留被 @android.support.annotation 注解声明的函数 
-keepclasseswithmembers class * {
    @android.support.annotation.Keep <methods>;
}

11 . 保留被注解声明的成员 : 被 @android.support.annotation.Keep 注解修改的成员 , 不会被混淆 ;

# 保留被 @android.support.annotation 注解声明的成员
-keepclasseswithmembers class * {
    @android.support.annotation.Keep <fields>;
}

12 . 保留被注解声明的构造函数 : 被 @android.support.annotation.Keep 修饰的构造函数不会被混淆 ;

# 保留被 @android.support.annotation 注解声明的构造函数 
-keepclasseswithmembers class * {
    @android.support.annotation.Keep <init>(...);
}




二、Proguard 完整注释



# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html
#
# 从 Gradle 插件 2.2 版本开始 , 该文件与插件一同发布, 在编译构建时取出 .
# 不再维护 $ANDROID_HOME 中的文件 , 新的 Gradle 插件版本将会忽略这些文件 .
# 
# 默认情况下 , 优化会被关闭 . 
# Dex 自己会执行优化 , 不建议在 ProGuard 步骤中进行优化 . 
# 如果想要启用优化 , 不能只在 ProGuard 项目配置中将优化标志设为 true ;
# 相反还要在 build.gradle 中指向 "proguard-android-optimize.txt" 文件 .
# 不要进行优化 
-dontoptimize

# 不要使用混合大小写类名进行混淆 , 混淆后的名称全部都是小写 , 增加阅读难度 
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-verbose

# 保留一些反射中可能用到的属性 
-keepattributes *Annotation*,Signature,InnerClasses,EnclosingMethod

# 保留这些类和类成员 
-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService
-keep public class com.google.android.vending.licensing.ILicensingService
# 控制编译时不在 Build 对话框输出一些日志信息 
-dontnote com.android.vending.licensing.ILicensingService
-dontnote com.google.vending.licensing.ILicensingService
-dontnote com.google.android.vending.licensing.ILicensingService

# 不混淆 Native 函数
# http://proguard.sourceforge.net/manual/examples.html#native
-keepclasseswithmembernames class * {
    native <methods>;
}

# 不要混淆继承自 View 的 get set 函数 , 以便让动画可以继续工作
# 指定类成员 ( 成员方法 / 成员变量 ) 不被混淆 
-keepclassmembers public class * extends android.view.View {
    void set*(***);
    *** get*();
}

# 不要混淆 Activity 及 子类的 成员 , 以防在 XML 的 onCLick 属性中用到 .
-keepclassmembers class * extends android.app.Activity {
    public void *(android.view.View);
}

# 枚举成员不要混淆 
# http://proguard.sourceforge.net/manual/examples.html#enumerations
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keepclassmembers class * implements android.os.Parcelable {
    public static final ** CREATOR;
}

-keepclassmembers class **.R$* {
    public static <fields>;
}

# Preserve annotated Javascript interface methods.
-keepclassmembers class * {
    @android.webkit.JavascriptInterface <methods>;
}

# The support libraries 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.
-dontnote android.support.**
-dontnote androidx.**
-dontwarn android.support.**
-dontwarn androidx.**

# This class is deprecated, but remains for backward compatibility.
-dontwarn android.util.FloatMath

# Understand the @Keep support annotation.
# 保留注解 
-keep class android.support.annotation.Keep
-keep class androidx.annotation.Keep

# 保留被 @android.support.annotation 注解声明的类 
-keep @android.support.annotation.Keep class * {*;}
# 保留被 @androidx.annotation 注解声明的类 
-keep @androidx.annotation.Keep class * {*;}

# 保留被 @android.support.annotation 注解声明的函数 
-keepclasseswithmembers class * {
    @android.support.annotation.Keep <methods>;
}
# 保留被 @androidx.annotation 注解声明的函数 
-keepclasseswithmembers class * {
    @androidx.annotation.Keep <methods>;
}

# 保留被 @android.support.annotation 注解声明的成员
-keepclasseswithmembers class * {
    @android.support.annotation.Keep <fields>;
}

# 保留被 @androidx.annotation 注解声明的成员 
-keepclasseswithmembers class * {
    @androidx.annotation.Keep <fields>;
}

# 保留被 @android.support.annotation 注解声明的构造函数 
-keepclasseswithmembers class * {
    @android.support.annotation.Keep <init>(...);
}

# 保留被 @androidx.annotation 注解声明的构造方法 
-keepclasseswithmembers class * {
    @androidx.annotation.Keep <init>(...);
}

# These classes are duplicated between android.jar and org.apache.http.legacy.jar.
-dontnote org.apache.http.**
-dontnote android.net.http.**

# These classes are duplicated between android.jar and core-lambda-stubs.jar.
-dontnote java.lang.invoke.**

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值