由于各种反编译工具的泛滥,作为Android程序员在2.3版本以前只能通过手动添加proguard来实现代码混淆


proguard这个工具是一个java代码混淆的工具


在2.3版本的sdk中 我们可以看到在android-sdk-windows/tools/下面多了一个proguard文件夹

google已经把proguard技术放在了android sdk里面 可以通过正常的编译方式也能实现代码混淆了



Android 2.3 代码混淆proguard技术…_android

可以看见新建一个工程里面有default.properties和proguard.cfg


默认的default.properties代码如下




1. This file is automatically generated by Android Tools. 
2. Do not modify this file -- YOUR CHANGES WILL BE ERASED! 
3.  # 
4. This file must be checked in Version Control Systems. 
5.  # 
6. To customize properties used by the Ant build system use, 
7. "build.properties", and override values to adapt the script to your 
8. project structure. 
9. Project target. 
10.


我们可以看到proguard.cfg已经帮我们写好了优化代码脚本





1. 5  
2. es  
3. classes  
4.  
5.  
6. !code/simplification/arithmetic,!field/*,!class/merging/*  
7. publicclass* extends android.app.Activity  
8. publicclass* extends android.app.Application  
9. publicclass* extends android.app.Service  
10. publicclass* extends android.content.BroadcastReceiver  
11. publicclass* extends android.content.ContentProvider  
12. publicclasscom.android.vending.licensing.ILicensingService  
13. es class* {  
14.    native <methods>;  
15.  
16. es class* {  
17.    public<init>(android.content.Context, android.util.AttributeSet);  
18.  
19. es class* {  
20.    public<init>(android.content.Context, android.util.AttributeSet, int);  
21.  
22. enum* {  
23.    publicstatic**[] values();  
24.    publicstatic** valueOf(java.lang.String);  
25.  
26. class* implements android.os.Parcelable {  
27.  publicstaticfinal android.os.Parcelable$Creator *;  
28.


从脚本中可以看到,混淆中保留了继承自Activity、Service、Application、BroadcastReceiver、ContentProvider等基本组件以及com.android.vending.licensing.ILicensingService


并保留了所有的Native变量名及类名,所有类中部分以设定了固定参数格式的构造函数,枚举等等。(详细信息请参考<proguard_path>/examples中的例子及注释。)


接下来 按照google帮助文档里说的




1. enable ProGuard so that it runs aspart of an Ant or Eclipse build, setthe proguard.config property inthe <project_root>/default.properties file. The path can be an absolute path or a path relative to the project's root.

所以我们修改default.properties file

加上一句

proguard.config=proguard.cfg

如下

1. This file is automatically generated by Android Tools. 
2. Do not modify this file -- YOUR CHANGES WILL BE ERASED! 
3.  # 
4. This file must be checked in Version Control Systems. 
5.  # 
6. To customize properties used by the Ant build system use, 
7. "build.properties", and override values to adapt the script to your 
8. project structure. 
9. Project target. 
10.
  1.  

 



然后正常的编译签名即可


然后用Android Tools生成一个发布的apk即可


Android 2.3 代码混淆proguard技术…_android


然后用反编译工具查看dex文件

最后导出反编译之后的混淆代码如下图



是不是很轻松加愉快!希望各位程序员都能保护好自己的Android代码!