[android deverlop 学习笔记]ProGuard代码混淆

来源:互联网 发布:淘宝暗语大全 编辑:程序博客网 时间:2024/05/20 08:41

一、ProGuard代码混淆的意义:

proguard代码混淆就是用一些无意义的字母来替代类名和函数名。它可以帮助去掉一些无用的代码和类,减小apk文件。同时,用无意义的字母代替类和方法名,可以防止反编译。

二、ProGuard代码混淆的方法:

1、设置proguard文件

使用Eclipse生成工程的时候,会在根目录自动生成一个proguard.cfg的文件,只需要把“proguard.config=proguard.cfg”这一句添加到project.properties文件里面。

2、设置proguard属性

proguard有时候会误删一些类,一下几种情况会导致误删:

 1)只在 AndroidManifest.xml 文件定义的类

 2)jni调用的方法

3)动态引用的方法或字段

为了防止这些误删,可以在proguard.cfg里,设置“-keep”,表示不对某些方法混淆。例如,设置:

-keep public class <MyClass>

可以不对MyClass进行混淆。当然,keep还有 很多别的选项可以设置,据需参考keep的帮助文档。

三、发布apk

在Eclipse中,可以file--export菜单中,找到发布apk的向导。然后就可以发布apk了。这时候,就会在工程的根目录里生成一个文件夹proguard,里面包含四个文件:

dump.txt:
    Describes the internal structure of all the class files in the .apk file
mapping.txt
    Lists the mapping between the original and obfuscated class, method, and field names. This file is important when you receive a bug report from a release build, because it translates the obfuscated stack trace back to the original class, method, and member names. See Decoding Obfuscated Stack Traces for more information.
seeds.txt
    Lists the classes and members that are not obfuscated
usage.txt
    Lists the code that was stripped from the .apk


其中,mapping.txt描述混淆代码和原始代码之间的队员关系,在debug的时候,很有用。

有一点要注意,每次发布apk,都会把之前的文件都覆盖掉,所以,为了debug,需要向办法把progurad生成的文件保存起来。

四、从Stack Traces得出原始代码

当程序错误抛出异常生成Stack Traces之后,以下方法可以从混淆的代码中,得到原始的代码:

retrace.bat -verbose mapping.txt obfuscated_trace.txt



0 0
原创粉丝点击