Android 2.3 代码混淆proguard技术介绍 收藏

来源:互联网 发布:ubuntu 端口流量 编辑:程序博客网 时间:2024/04/28 16:07

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

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

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

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

 

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

默认的default.properties代码如下

view plaincopy to clipboardprint?
# 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 use, 
# "build.properties", and override values to adapt the script to your 
# project structure. 
# Project target.  
target=android-9 
# 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 use,
# "build.properties", and override values to adapt the script to your
# project structure.
# Project target.
target=android-9
 

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

view plaincopy to clipboardprint?
-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 com.android.vending.licensing.ILicensingService  
-keepclasseswithmembernames class * {  
    native <methods>;  
}  
-keepclasseswithmembernames class * {  
    public <init>(android.content.Context, android.util.AttributeSet);  
}  
-keepclasseswithmembernames class * {  
    public <init>(android.content.Context, android.util.AttributeSet, int);  
}  
-keepclassmembers enum * {  
    public static **[] values();  
    public static ** valueOf(java.lang.String);  
}  
-keep class * implements android.os.Parcelable {  
  public static final android.os.Parcelable$Creator *;  

-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 com.android.vending.licensing.ILicensingService
-keepclasseswithmembernames class * {
    native <methods>;
}
-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}
-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}
 

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

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

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

view plaincopy to clipboardprint?
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. 
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

如下

view plaincopy to clipboardprint?
# 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 use, 
# "build.properties", and override values to adapt the script to your 
# project structure. 
# Project target.  
target=android-9  
proguard.config=proguard.cfg 
# 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 use,
# "build.properties", and override values to adapt the script to your
# project structure.
# Project target.
target=android-9
proguard.config=proguard.cfg
 

然后正常的编译签名即可

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

 

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

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

 

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

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Zengyangtech/archive/2011/01/10/6127600.aspx

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 百度云照片压缩后模糊怎么办 手机点+号无法上传图片怎么办 支付宝账户支付功能关闭怎么办 一件衣服买小了怎么办 交行u盾密码忘了怎么办 老婆婚内出轨丈夫应该怎么办 微信小程序获取用户信息失败怎么办 拼多多砍价没有完成怎么办 微信人数上限了怎么办 微信5000人满了怎么办 微信朋友满了怎么办啊 失业金签到忘签怎么办 gec安全密码忘了怎么办 唯品会微信商城地址错怎么办跨省 电脑突然没网了怎么办 移动光猫只能连一台电脑怎么办 网上银行用户名和密码忘了怎么办 中国银行网银用户名忘了怎么办 中行企业网银证书过期怎么办 腾讯会员被别人登录了怎么办 微信限90天提现转帐功能怎么办? 星巴克券过期了怎么办 京东买面膜发个空包怎么办 手机低于4.4版本怎么办才能恢复 263云通信密码忘了怎么办 有信电话拉黑了怎么办 微信手机充值冲错了怎么办 国通石油卡怎么办红卡 善融商城有假货怎么办 中百购物卡丢了怎么办 cncbk被骗25万该怎么办 中银e贷额度冻结怎么办 招行抵押贷循环额度怎么办 小额钱袋被拒了怎么办 银行需要提供消费贷款发票怎么办 中银e贷款逾期怎么办 按揭房不想要了怎么办 房贷不想还了怎么办 融e借没有u盾怎么办 窗式空调声音大怎么办 瑞得卡过期了怎么办