Android ProGuard实例教程

       ProGuard工具通过移除不用的代码,用语义上混淆的名字来重命名类、字段和方法等手段来压缩、优化和混淆你的代码。结果是更小的.apk文件,并且更难于被反编译。由于ProGuard能够让你的程序难于被反编译,因此,当你的程序使用了一些机密的信息的时,使用它就显得更加重要。 

       ProGuard已经集成到Android的编译环境中,因此,用不着手动来触发它。ProGuard只在release模式下编译应用程序才会运行,所以,在debug模式下编译,你就不必处理混淆的代码。是否运行ProGuard是完全可选的,但强烈推荐使用。 

        启用ProGuard 
        当你创建Android工程时,proguard.cfg文件会在工程的根目录自动创建。这个文件定义了ProGuard如何优化和混淆代码,因此,理解如何定制它是非常重要的。默认的配置文件只是覆盖了一些通用的情况,所以,基本上你需要编辑它来满足你的需求。参考后面的“配置ProGuard”章节来了解如何定制ProGuard的相关信息。 

       启用ProGuard让它跟随Ant或Eclipse编译时一起运行,你需要在<project_root>/default.properties文件中设置proguard.config属性。路径可以是绝对路径或是工程根目录的相对路径。 如果你把proguard.cfg文件放在默认的位置(工程的根目录),你可以像这样来指定它的位置: proguard.config=proguard.cfg 
你还可以把该文件移到任何你想放的位置,然后指定绝对路径:proguard.config=/path/to/proguard.cfg 
当你在release模式下编译你的程序,不管是用ant release还是用Eclipse的导出向导,编译系统都会自动检查proguard.config属性是否设置。如果设置了,ProGuard就会在打包成.apk文件之前,自动处理应用程序的字节码。Debug模式编译,不会触发ProGuard,因为它会使得调试更加复杂累赘。 

       ProGuard运行结束后,输出以下文件: dump.txt 
       描述.apk文件中所有类文件间的内部结构 

       mapping.txt 
       列出了原始的类,方法和字段名与混淆后代码间的映射。这个文件很重要,当你从release版本中收到一个bug报告时,可以用它来翻译被混淆的代码。 

       seeds.txt 
       列出了未被混淆的类和成员 
       usage.txt 
       列出了从.apk中删除的代码

       这些文件放在以下文件夹中: 
       Ant:<project_root>/bin/proguard 
       Eclipse: <project_root>/proguard 

       注意:每当你在release模式下编译时,这些文件都会被覆盖重写,当然,是被ProGuard工具生成的最新的文件所覆盖。每次你发布你的程序时,都应该保存一份,为了将来能够解码bug报告。

       配置 ProGuard 
       一些情况下,proguard.cfg文件中的默认配置就足够了。然而,有些情况ProGuard也很难正确分析,它可能会删除它认为不用的代码,但实际上正是你的程序所需要的。例如: 
       只在AndroidManifest.xml文件中引用的类 
       由JNI调用的方法 
       动态引用的字段和方法 

       默认的proguard.cfg文件努力去覆盖通用的情况,但有可能你会遇到如ClassNotFoundException这样的异常,而这正好是由于ProGuard移除了整个类造成的。 

       你可以修正由于ProGuard移除代码造成的错误,只需要在proguard.cfg文件中添加一行“-keep”。例如: 
-keep public class <MyClass> 

       使用-keep选项时,有一些选项和建议,因此,强烈建议你阅读ProGuard手册来了解更多关于定制配置文件的信息。“Overview of Keep options”和“Examples section”将非常有用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ProGuard is a tool used in Android development to optimize and obfuscate the compiled code. It helps reduce the size of the APK file and makes it harder for reverse engineers to understand and modify the code. ProGuard performs several tasks to achieve this, including: 1. Shrinking: It analyzes the code and removes unused classes, fields, and methods, thereby reducing the size of the application. 2. Optimization: ProGuard applies various optimizations, such as inlining methods, removing dead code, and simplifying constant expressions, to improve the app's performance. 3. Obfuscation: It renames classes, fields, and methods with short, meaningless names, making it difficult for others to understand the code. To enable ProGuard in an Android project, you need to add the following lines to your project's `build.gradle` file: ```groovy android { // ... buildTypes { release { // ... minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } } ``` Here, `minifyEnabled true` enables code shrinking and obfuscation, while `proguardFiles` specifies the ProGuard configuration files. The `getDefaultProguardFile('proguard-android-optimize.txt')` line includes a default set of rules provided by the Android SDK, and you can customize these rules in your own `proguard-rules.pro` file. It's important to note that ProGuard can sometimes cause issues if it mistakenly removes code that is actually used, or if it conflicts with certain libraries or reflection-based code. In such cases, you may need to tweak the ProGuard configuration to exclude specific classes or methods. Overall, ProGuard is a useful tool for optimizing and securing your Android app.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值