一、概述

Android源码很容易被工具反编译出来,因此,对源码做混淆是一种非常常用的保护源码的方式,不仅如此,proguard还会对源码做一些优化。

  • 删除了源文件中没有调用的那部分代码,最大化的精简了字节码文件,使得最终生成的apk文件更小。
  • 使用语义混淆的命名替换了代码中的类、字段和函数等,使得其他人无法反编译获取源代码,起到对代码的保护作用。

ProGuard工具是已经集成到我们android系统中的,所以不需要用户手动的去集成。但是有一点需要注意,仅在程序处于Release模式时ProGuard才有效,反之在Debug模式是不能通过ProGuard来混淆代码的。

二、案例说明

当我们在Eclipse中新建一个项目后,在工程的根目录,会自动生成两个ProGuard的混淆文件:proguard-project.txt和project.properties(在老版本的ADT中,只会生成一个叫proguard.cfg的文件)。我们先看下文件project.properties :

# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system edit
# "ant.properties", and override values to adapt the script to your
# project structure.
#
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

# Project target.
target=android-21

看后面一段注释:To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home) ,意指要让ProGuard 来压缩和混淆代码,把这句注释去掉即可!

这里是通过设置proguard.config属性来指定混淆代码的配置文件为本机SDK目录下面的proguard-android.txt文件,以及制定混淆的个性化配置文件为当前工程(eclipse下)根目录下面的proguard-project.txt文件 ,而后面这个文件,恰是我们刚才看到的原本在根目录下自动生成的另外一个文件!

其实打开了这个地方,我们就已经可以混淆代码了,不过这里要注意:不能试图通过运行eclipse中的Run as 和 Debug as 菜单来生成混淆代码,必须通过在工程上右键->”Export…”->“Export Android Application”,导出apk包。
这样一步操作后,算是代码混淆完成了。那么怎么才能检验我们真的混淆了代码了呢?首先,我们能够看到在工程的根目录新生产了一个文件夹proguard,里面有四个文件,其内容如下:

  • dump.txt : 描述了apk中所有类 文件中内部的结构体。( Describes the internal structure of
    all the class files in the .apk file )
  • mapping.txt : 列出了原始的类、方法和名称与混淆代码间的映射。( Lists the mapping between the
    original and obfuscated class, method, and field names. )
  • seeds.txt : 列出了没有混淆的类和方法。( Lists the classes and members that are not
    obfuscated )
  • usage.txt : 列出了源代码中被删除在apk中不存在的代码。( Lists the code that was stripped
    from the .apk )

同时使用反编译软件对新生成的apk反编译后会发现,里面的类名、方法和变量等,都变成了简单的a、b、c、d等毫无含义的字母,这样就达到了混淆的目的

三、注意事项

在实际使用过程中,我们会发现当前apk中的有些方法和类,是要供外部使用的,而此时混淆了名称,外部调用就会报错了,那么怎么解决这个问题?此时就要用到我们刚才提到的混淆的个性化配置文件proguard-project.txt,在其中去配置不需要混淆的类、方法和变量等。

这里提供一段网上有人共享的配置代码,这个配置代码保留了工程文件中的Activity、Application、Service、BroadcastReceiver、ContentProvider、BackupAgentHelper、Preference和ILicensingService的子类,并保留了所有的Native变量名及类名,所有类中部分以设定了固定参数格式的构造函数,枚举等等,用来防止外部调用出错,大家可以借鉴下,以后来配置自己的文件:
proguard-project.txt

-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

-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

-keepclasseswithmembernames class * {
native <methods>;
}

-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}

-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}