Android Java混淆(ProGuard)
轉(zhuǎn)載請(qǐng)注明出處:http://www.aygfsteel.com/zh-weir/archive/2011/07/12/354190.html
Android Java混淆(ProGuard)
ProGuard簡(jiǎn)介
ProGuard是一個(gè)SourceForge上非常知名的開源項(xiàng)目。官網(wǎng)網(wǎng)址是:http://proguard.sourceforge.net/。
Java的字節(jié)碼一般是非常容易反編譯的。為了很好的保護(hù)Java源代碼,我們往往會(huì)對(duì)編譯好的class文件進(jìn)行混淆處理。ProGuard的主要作用就是混淆。當(dāng)然它還能對(duì)字節(jié)碼進(jìn)行縮減體積、優(yōu)化等,但那些對(duì)于我們來說都算是次要的功能。
引用ProGuard官方的一段話來介紹就是:
ProGuard is a free Java class file shrinker, optimizer, obfuscator, and preverifier. It detects and removes unused classes, fields, methods, and attributes. It optimizes bytecode and removes unused instructions. It renames the remaining classes, fields, and methods using short meaningless names. Finally, it preverifies the processed code for Java 6 or for Java Micro Edition.
Android Eclipse開發(fā)環(huán)境與ProGuard
在Android 2.3以前,混淆Android代碼只能手動(dòng)添加proguard來實(shí)現(xiàn)代碼混淆,非常不方便。而2.3以后,Google已經(jīng)將這個(gè)工具加入到了SDK的工具集里。具體路徑:SDK\tools\proguard。當(dāng)創(chuàng)建一個(gè)新的Android工程時(shí),在工程目錄的根路徑下,會(huì)出現(xiàn)一個(gè)proguard的配置文件proguard.cfg。也就是說,我們可以通過簡(jiǎn)單的配置,在我們的elipse工程中直接使用ProGuard混淆Android工程。
具體混淆的步驟非常簡(jiǎn)單。首先,我們需要在工程描述文件default.properties中,添加一句話,啟用ProGuard。如下所示:
2 # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 #
4 # This file must be checked in Version Control Systems.
5 #
6 # To customize properties used by the Ant build system use,
7 # "build.properties", and override values to adapt the script to your
8 # project structure.
9 # Indicates whether an apk should be generated for each density.
10 split.density=false
11 # Project target.
12 target=android-10
13 proguard.config=proguard.cfg
14
這樣,Proguard就可以使用了。當(dāng)我們正常通過Android Tools導(dǎo)出Application Package時(shí),Proguard就會(huì)自動(dòng)啟用,優(yōu)化混淆你的代碼。
導(dǎo)出成功后,你可以反編譯看看混淆的效果。一些類名、方法名和變量名等,都變成了一些無意義的字母或者數(shù)字。證明混淆成功!
proguard.cfg配置
稍微深入想一下混淆后的結(jié)果,你就會(huì)發(fā)現(xiàn),如果一些提供給外部的類、方法、變量等名字被改變了,那么程序本身的功能就無法正常實(shí)現(xiàn)。那么Proguard如何知道哪些東西是可以改名,而哪些是不能改變的呢?
這個(gè)是靠proguard.cfg文件來進(jìn)行配置的。Android工程中默認(rèn)自動(dòng)生成的proguard.cfg已經(jīng)針對(duì)Android的一般情況進(jìn)行了配置,我們打開這個(gè)配置文件。內(nèi)容大概如下:
2 -dontusemixedcaseclassnames
3 -dontskipnonpubliclibraryclasses
4 -dontpreverify
5 -verbose
6 -optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
7 -keep public class * extends android.app.Activity
8 -keep public class * extends android.app.Application
9 -keep public class * extends android.app.Service
10 -keep public class * extends android.content.BroadcastReceiver
11 -keep public class * extends android.content.ContentProvider
12 -keep public class * extends android.app.backup.BackupAgentHelper
13 -keep public class * extends android.preference.Preference
14 -keep public class com.android.vending.licensing.ILicensingService
15
16 -keepclasseswithmembernames class * {
17 native <methods>;
18 }
19
20 -keepclasseswithmembernames class * {
21 public <init>(android.content.Context, android.util.AttributeSet);
22 }
23
24 -keepclasseswithmembernames class * {
25 public <init>(android.content.Context, android.util.AttributeSet, int);
26 }
27
28 -keepclassmembers enum * {
29 public static **[] values();
30 public static ** valueOf(java.lang.String);
31 }
32
33 -keep class * implements android.os.Parcelable {
34 public static final android.os.Parcelable$Creator *;
35 }
36
它主要保留了繼承自Activity、Application、Service、BroadcastReceiver、ContentProvider、BackupAgentHelper、Preference和ILicensingService的子類。因?yàn)檫@些子類,都是可能被外部調(diào)用的。
另外,它還保留了含有native方法的類、構(gòu)造函數(shù)從xml構(gòu)造的類(一般為View的子類)、枚舉類型中的values和valueOf靜態(tài)方法、繼承Parcelable的跨進(jìn)程數(shù)據(jù)類。
在實(shí)際的一個(gè)工程項(xiàng)目中,可能Google自動(dòng)生成的配置不能勝任我們的混淆工作。所以,我們往往需要自己編寫一些ProGuard配置。這方面的資料在官網(wǎng)的Manual -> Usage里有詳細(xì)說明。大家可以研究一下。
參考資料
《Android 2.3 代碼混淆proguard技術(shù)介紹》
轉(zhuǎn)載請(qǐng)注明出處:http://www.aygfsteel.com/zh-weir/archive/2011/07/12/354190.html
posted on 2011-07-12 19:29 zh.weir 閱讀(6049) 評(píng)論(0) 編輯 收藏 所屬分類: Android軟件安全