您的位置:首页 > 移动开发 > Android开发

Android_Proguard代码混淆器

2016-05-04 08:43 429 查看


Android_Proguard代码混淆器


1.混淆器概述

混淆器通过删除从未用过的代码和使用晦涩名字重命名类、字段和方法,对代码进行压缩,优化和混淆,生成一个比较小的.apk文件,该文件比较难进行逆向工程,是一种重要的保护手段。
混淆器被集成在android 构建系统中,所以你不必手动调用它。同时混淆器仅在发布模式下进行构建应用程序的时候才会运行起来,所以在调试模式下构建程序时,你不必处理混淆代码。这个文档描述了怎样启用并配置混淆器,以及使用跟踪(retrace)工具对混淆的堆栈跟踪信息(stack traces)进行解码。


2.启使混淆器

当你新建了一个Android工程之后,一个proguard.cfg文件会在工程的根目录下自动创建。这个文件定义了混淆器是怎样优化和混淆你的代码的,所以懂得怎样根据你的需要来定制是非常重要的。缺省的配置文件仅覆盖到了通常情况,所以根据你的需求,很可能需要编辑它。接下来的内容是关于通过定制混淆器配置文件来对混淆器配置。
为了让启用混淆器作为Ant或者Eclipse构建过程中一部分,可以在/default.properties文件中,设置proguard.config属性。路径可以是绝对路径或者工程根目录的相对路径。
如果你让proguard.cfg文件在缺省位置(工程的根目录),你可以像这样指定位置:proguard.config=proguard.cfg,debug模式混淆无效,通过export导出有效。


3.混淆器后生成文件含义

(1).dump.txt

描述.apk包中所有class文件的内部结构。

(2).mapping.txt

列出了源代码与混淆后的类,方法和属性名字之间的映射。这个文件对于在构建之后得到的bug报告是有用的,因为它把混淆的堆栈跟踪信息反翻译为源代码中的类,方法和成员名字。更多信息,查看解码混淆过的堆栈跟踪信息。

(3).seeds.txt

列出那些未混淆的类和成员。

(4).usage.txt

列出从.apk中剥离的代码。

注意: 每次在发布模式下构建时,这些文件都会被最新的文件覆盖。所以每次发布程序时候,为了反混淆来自构建时产生的bug报告,请保存这些文件的一个拷贝。需要保存这些产生的Proguard文件。


4.混淆代码堆栈跟踪信息

当混淆代码并输出了一个堆栈调试信息时,这些方法名字是混淆过的,虽然可以进行调试,但是调试变得困难。幸运的是,每当混淆器运行时候,它都会输出到文件/bin/proguard/mapping.txt中,该文件包含了从原始类,方法和属性名字到混淆后名字的映射,每次打包时应注意保存好该文件。使用/tools/proguard/retrace.sh脚本命令能把混淆后的堆栈调试信息转换为可以未混淆前的堆栈调试信息。
retrace.bat|retrace.sh [-verbose] mapping.txt []

$sh /Applications/ADT/sdk/tools/proguard/bin/retrace.sh  -verbose /Users/zf/Documents/workspace/GridTest/proguard/mapping.txt /Users/zf/Desktop/bug.txt



5.常见混淆代码

(1).Android工程混淆

# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html 
# Optimizations: If you don't want to optimize, use the
# proguard-android.txt configuration file instead of this one, which
# turns off the optimization flags.  Adding optimization introduces
# certain risks, since for example not all optimizations performed by
# ProGuard works on all versions of Dalvik.  The following flags turn
# off various optimizations known to have issues, but the list may not
# be complete or up to date. (The "arithmetic" optimization can be
# used if you are only targeting Android 2.0 or later.)  Make sure you
# test thoroughly if you go this route.
-optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/*
-optimizationpasses 5
-allowaccessmodification
-dontpreverify

# The remainder of this file is identical to the non-optimized version
# of the Proguard configuration file (except that the other file has
# flags to turn off optimization).

-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-verbose

-keepattributes *Annotation*
-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService

# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native -keepclasseswithmembernames class * {
native ;
}

# keep setters in Views so that animations can still work.
# see http://proguard.sourceforge.net/manual/examples.html#beans -keepclassmembers public class * extends android.view.View {
void set*(***);
*** get*();
}

# We want to keep methods in Activity that could be used in the XML attribute onClick
-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}

# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations -keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}

-keepclassmembers class **.R$* {
public static ;
}


(2).引入Gson包

##---------------Begin: proguard configuration for Gson  ----------
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature

# Gson specific classes
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.stream.** { *; }
-keep class com.google.gson.examples.android.model.** { *; }
-keep class com.google.gson.** { *;}

##---------------End: proguard configuration for Gson  ----------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: