Android 中的混淆技术

从上一篇 【 Android反编译】博文中,我们知道:通过 apktool 和 dex2jar 工具,我们不仅可以反编译资源还可以反编译代码,甚至重新打包签名。因此,如果我们想让我们的 App 里核心不被窃取,我们必须采取一些措施来保护,而混淆技术则是最简单粗暴的方法。

在Android Studio中 混淆APK 是件及其简单的事,只需要修改build.gradle 中的一行配置(minifyEnabled false 改为 true)即可。

这里写图片描述

注意:只有正式版(签名)的 APK 才会被混淆

而这个混淆是按照一定的规则来执行的,我们需要了解的是这个规则,然后根据自己的需求来设定这个规则。

默认的规则文件 proguard-android.txt 放在 SDK路径\tools\proguard 下。
这里写图片描述

proguard-android.txt 规则解释:

# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html

#不使用大小写混合类名
-dontusemixedcaseclassnames
#不跳过非public库类
-dontskipnonpubliclibraryclasses
#打印混淆的详细信息
-verbose

# Optimization is turned off by default. Dex does not like code run
# through the ProGuard optimize and preverify steps (and performs some
# of these optimizations on its own).
#进行优化
-dontoptimize
#不预校验
-dontpreverify
# Note that if you want to enable optimization, you cannot just
# include optimization flags in your own project configuration file;
# instead you will need to point to the
# "proguard-android-optimize.txt" file instead of this one from your
# project.properties file.
#保留注释中的参数
-keepattributes *Annotation*
#保留这两个public 类
-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService

# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
#保留类名及native方法名
-keepclasseswithmembernames class * {
    native <methods>;
}

# keep setters in Views so that animations can still work.
# see http://proguard.sourceforge.net/manual/examples.html#beans
#保留View 中的getXxx()和setXxx()方法,因为属性动画需要有相应的setter和getter的方法实现,混淆了就无法工作
-keepclassmembers public class * extends android.view.View {
   void set*(***);
   *** get*();
}

# We want to keep methods in Activity that could be used in the XML attribute onClick
#保留在Activity的onClick方法,因为它可能在XML参数中使用
-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}

# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
#不混淆枚举类中的values()和valueOf()方法
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

#不混淆Parcelable实现类中的CREATOR字段,毫无疑问,CREATOR字段是绝对不能改变的,包括大小写都不能变,不然整个Parcelable工作机制都会失败。
-keepclassmembers class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator CREATOR;
}

#不混淆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.
#对android.support包下的代码不警告,因为support包中有很多代码都是在高版本中使用的
-dontwarn android.support.**

# Understand the @Keep support annotation.
#保留 android.support.annotation.Keep 类、相关的字段以及方法包括构造方法
-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>(...);
}

更多混淆信息请参考:guardsquare

注意:修改 SDK路径\tools\proguard 下 proguard-android.txt 规则对所有项目都有效,如果只想对当前项目有效,可以修改项目下的 proguard-rules.pro 文件

这里写图片描述

修改规则例子,可参阅郭霖大大的例子,相信收获会很大!

扫描二维码关注公众号,回复: 2710227 查看本文章

前面是介绍 混淆 APK ,在实际当中,我们还需要对 Jar 包进行混淆。

混淆 Jar 包,我们可以利用 SDK 提供的一个图形化工具:proguardgui ,此工具在 SDK路径\tools\proguard\bin 下。
这里写图片描述

双击后,即可使用。

但前提是我们自己得先有 Jar 包,我们编写好代码后,AS 可以帮我们把 Java 代码生成字节码 .class 文件。 字节码文件生成 Jar 包,可以使用 Jar 命令:

这里写图片描述

打开 proguardgui 后,相信大家都会用了!

添加包和依赖时,一定得把相关的所有包和依赖都添加
添加包和依赖时,一定得把相关的所有包和依赖都添加
添加包和依赖时,一定得把相关的所有包和依赖都添加
这是个很考验程序猿的问题!

注:此篇博文为阅读 郭霖大大的博文后的学习笔记。详情请阅读 Android安全攻防战,反编译与混淆技术完全解析(下)

猜你喜欢

转载自blog.csdn.net/MOESECSDN/article/details/78555849