在2.3版本的sdk中可以看到在ANDROID_SDK _PATH/tools/下面多了一个proguard文件夹,google把proguard技术放在了android sdk里面,现在已经可以通过正常的编译方式实现代码混淆了。

在2.3版本以后,无论开发用的是哪一个版本的SDK,新建一个工程里面都会有default.properties和proguard.cfg两个文件。(http://www.my400800.cn )

proguard4.6.zip 下载

默认的default.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 use,# "build.properties", and override values to adapt the script to your# project structure.# Project target.target=android-9

默认的proguard.cfg中也已经有了混淆代码的脚本:

-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中的例子及注释。

使用代码混淆

依据官方文档所说:

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文件,加上一句:

proguard.config=proguard.cfg

加完后如下面的样子:

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-9proguard.config=proguard.cfg

然后正常的编译签名即可生成一个经过代码混淆的apk。

如果编译出现警告可以在【proguard.cfg】文件中添加【-ignorewarnings】忽略警告项目进行混淆处理。

Eclipse+ADT进行Android应用程序的代码混淆和签名 图解

启动代码混淆功能

在较新版本的Android tools和ADT,项目工程里面是带有proguard.cfg的代码混淆配置文件,但默认是没有启动这个配置的,需要手动地在default.properties里面添加指定这个配置文件: # Project target. target=android-3 proguard.config=proguard.cfg 然后按F5刷新当前项目工程,这时候Eclipse检测了文件的变动而重新编译!

生成签名发布apk

1.Eclipse工程中右键工程,弹出选项中选择 android工具-生成签名应用包:


2.选择需要打包的android项目工程(注:这里会自动选择当前的Project的):


3.如果已有私钥文件,选择私钥文件 输入密码,如果没有私钥文件见 第6和7步创建私钥文件:


4.输入私钥别名和密码:


5.选择APK存储的位置,并完成设置 开始生成:


6.没有私钥文件的情况,创建私钥文件(注:这里私钥文件的Location位置最好自己选择一个新位置,便于牢记,而且最好把这个私钥文件备份到其他地方去以免丢失,因为应用程序的更新需要同一私钥文件):


7.输入私钥文件所需信息,并创建(注:这里的密码是用于Key的别名的,和上面的KeyStore文件的不同,这点可以看步骤3和4。另外下面的名字,开发者资料等是不需要全部填写的,dialog会有提示的):


这时候生成的apk,我发现是比debug版本的要小!如果你发现没有变小的话,请确认项目工程是重新编译的!但代码混淆的效果一般般,基本上还是可以看到原来的语句!

如果运行上面的代码出现错误请下载 proguard4.6.rar解压后把目录下面的 lib 目录中的文件复制到 【androidsdk的Developer目录下的lib目录下面就可以了】

更多相关文章

  1. 详解Android系统中的root权限获得原理
  2. Android(安卓)View Animation Example
  3. Android(安卓)基于Http协议的文件上传
  4. Android(安卓)获取Root权限 详细图解
  5. 在Ubuntu上下载、编译和安装Android最新源代码(转载)
  6. android资源文件详解
  7. Android(安卓)NDK 环境配置
  8. [置顶] Android:图解Activity启动流程源码(整体流程)
  9. 第一行代码 第三章 RecyclerView

随机推荐

  1. android二维码扫描(最近做的项目中用到的
  2. android编译系统makefile
  3. 介绍一种不同流式标签的实现方式
  4. [转]Android中两种设置全屏的方法
  5. Linux环境下Android使用Jenkins自动打包
  6. Activity的onConfigurationChanged事件
  7. android连接真机 调试未测试
  8. Android入门:4种布局
  9. 【转】Android学习笔记-常用的一些源码,防
  10. android properties