代码混淆proguard技术介绍

因为各类反编译工具的泛滥,做为Android程序员在2.3版本之前只能经过手动添加proguard来实现代码混淆 java

 

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

 

在2.3版本的sdk中 咱们能够看到在android-sdk-windows/tools/下面多了一个proguard文件夹 程序员

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

 

能够看见新建一个工程里面有default.properties和proguard.cfg app

 

默认的default.properties代码以下 ide

[c-sharp] view plain copy
  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. target=android-9  
 

咱们能够看到proguard.cfg已经帮咱们写好了优化代码脚本 函数

[c-sharp] view plain copy
  1. -optimizationpasses 5  
  2. -dontusemixedcaseclassnames  
  3. -dontskipnonpubliclibraryclasses  
  4. -dontpreverify  
  5. -verbose  
  6. -optimizations !code/simplification/arithmetic,!field/*,!class/merging/*  
  7. -keep public class * extends android.app.Activity  
  8. -keep public class * extends android.app.Application  
  9. -keep public class * extends android.app.Service  
  10. -keep public class * extends android.content.BroadcastReceiver  
  11. -keep public class * extends android.content.ContentProvider  
  12. -keep public class com.android.vending.licensing.ILicensingService  
  13. -keepclasseswithmembernames class * {  
  14.     native <methods>;  
  15. }  
  16. -keepclasseswithmembernames class * {  
  17.     public <init>(android.content.Context, android.util.AttributeSet);  
  18. }  
  19. -keepclasseswithmembernames class * {  
  20.     public <init>(android.content.Context, android.util.AttributeSet, int);  
  21. }  
  22. -keepclassmembers enum * {  
  23.     public static **[] values();  
  24.     public static ** valueOf(java.lang.String);  
  25. }  
  26. -keep class * implements android.os.Parcelable {  
  27.   public static final android.os.Parcelable$Creator *;  
  28. }  
 

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

 

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

 

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

[c-sharp] view plain copy
  1. To enable ProGuard so that it runs as part of an Ant or Eclipse build, set the proguard.config property in the <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

以下

[c-sharp] view plain copy
  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. target=android-9  
  11. proguard.config=proguard.cfg  
 

 

而后正常的编译签名便可

 

而后用Android Tools生成一个发布的apk便可

 

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

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

 

 

 

是否是很轻松加愉快!但愿各位程序员都能保护好本身的Android代码!

 

 

 

分享到:
查看评论
相关文章
相关标签/搜索