標籤:optimization android 原始碼 proguard 發布
1.混淆器概述
混淆器通過刪除從未用過的代碼和使用晦澀名字重新命名類、欄位和方法,對代碼進行壓縮,最佳化和混淆,產生一個比較小的.apk檔案,該檔案比較難進行逆向工程,是一種重要的保護手段。
混淆器被整合在android 構建系統中,所以你不必手動調用它。同時混淆器僅在發布模式下進行構建應用程式的時候才會運行起來,所以在偵錯模式下構建程式時,你不必處理混淆代碼。這個文檔描述了怎樣啟用並配置混淆器,以及使用跟蹤(retrace)工具對混淆的堆疊追蹤資訊(stack traces)進行解碼。
2.啟使混淆器
當你建立了一個Android工程之後,一個proguard.cfg檔案會在工程的根目錄下自動建立。這個檔案定義了混淆器是怎樣最佳化和混淆你的代碼的,所以懂得怎樣根據你的需要來定製是非常重要的。預設的設定檔僅覆蓋到了通常情況,所以根據你的需求,很可能需要編輯它。接下來的內容是關於通過定製混淆器設定檔來對混淆器配置。
為了讓啟用混淆器作為Ant或者Eclipse構建過程中一部分,可以在<project_root>/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.混淆代碼堆疊追蹤資訊
當混淆代碼並輸出了一個堆棧調試資訊時,這些方法名字是混淆過的,雖然可以進行調試,但是調試變得困難。幸運的是,每當混淆器運行時候,它都會輸出到檔案<project_root>/bin/proguard/mapping.txt中,該檔案包含了從原始類,方法和屬性名稱字到混淆後名字的映射,每次打包時應注意儲存好該檔案。使用<sdk_root>/tools/proguard/retrace.sh指令碼命令能把混淆後的堆棧調試資訊轉換為可以未混淆前的堆棧調試資訊。
retrace.bat|retrace.sh [-verbose] mapping.txt [<stacktrace_file=程式bug產生的堆棧資訊檔>]
$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 <methods>;}# 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 <fields>;}
(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 ----------
Android_Proguard代碼混淆器