前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Android 安全】DEX 加密 ( Proguard 简介 | 默认 ProGuard 分析 )

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

作者头像
韩曙亮
发布2023-03-28 18:51:38
4120
发布2023-03-28 18:51:38
举报
文章被收录于专栏:韩曙亮的移动开发专栏

文章目录

一、Proguard 配置简介


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

1 . 不进行优化 :

代码语言:javascript
复制
# 不要进行优化 
-dontoptimize

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

代码语言:javascript
复制
# 不要使用混合大小写类名进行混淆 , 混淆后的名称全部都是小写 , 增加阅读难度 
-dontusemixedcaseclassnames

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

代码语言:javascript
复制
# 保留一些反射中可能用到的属性 
-keepattributes *Annotation*,Signature,InnerClasses,EnclosingMethod

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

代码语言:javascript
复制
# 保留这些类和类成员 
-keep public class com.google.vending.licensing.ILicensingService

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

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

6 . Native 函数混淆设置 :

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

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

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

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

代码语言:javascript
复制
# 保留注解 
-keep class android.support.annotation.Keep

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

代码语言:javascript
复制
# 保留被 @android.support.annotation 注解声明的类 
-keep @android.support.annotation.Keep class * {*;}

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

代码语言:javascript
复制
# 保留被 @android.support.annotation 注解声明的函数 
-keepclasseswithmembers class * {
    @android.support.annotation.Keep <methods>;
}

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

代码语言:javascript
复制
# 保留被 @android.support.annotation 注解声明的成员
-keepclasseswithmembers class * {
    @android.support.annotation.Keep <fields>;
}

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

代码语言:javascript
复制
# 保留被 @android.support.annotation 注解声明的构造函数 
-keepclasseswithmembers class * {
    @android.support.annotation.Keep <init>(...);
}

二、Proguard 完整注释


代码语言:javascript
复制
# 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.**
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-11-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 一、Proguard 配置简介
  • 二、Proguard 完整注释
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档