轉(zhuǎn)載請注明出處:http://m.tkk7.com/zh-weir/archive/2011/07/12/354190.html
Android Java混淆(ProGuard)
ProGuard簡介
ProGuard是一個SourceForge上非常知名的開源項目。官網(wǎng)網(wǎng)址是:http://proguard.sourceforge.net/。
Java的字節(jié)碼一般是非常容易反編譯的。為了很好的保護Java源代碼,我們往往會對編譯好的class文件進行混淆處理。ProGuard的主要作用就是混淆。當然它還能對字節(jié)碼進行縮減體積、優(yōu)化等,但那些對于我們來說都算是次要的功能。
引用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代碼只能手動添加proguard來實現(xiàn)代碼混淆,非常不方便。而2.3以后,Google已經(jīng)將這個工具加入到了SDK的工具集里。具體路徑:SDK\tools\proguard。當創(chuàng)建一個新的Android工程時,在工程目錄的根路徑下,會出現(xiàn)一個proguard的配置文件proguard.cfg。也就是說,我們可以通過簡單的配置,在我們的elipse工程中直接使用ProGuard混淆Android工程。
具體混淆的步驟非常簡單。首先,我們需要在工程描述文件default.properties中,添加一句話,啟用ProGuard。如下所示:
1 # This file is automatically generated by Android Tools.
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就可以使用了。當我們正常通過Android Tools導(dǎo)出Application Package時,Proguard就會自動啟用,優(yōu)化混淆你的代碼。
導(dǎo)出成功后,你可以反編譯看看混淆的效果。一些類名、方法名和變量名等,都變成了一些無意義的字母或者數(shù)字。證明混淆成功!
proguard.cfg配置
稍微深入想一下混淆后的結(jié)果,你就會發(fā)現(xiàn),如果一些提供給外部的類、方法、變量等名字被改變了,那么程序本身的功能就無法正常實現(xiàn)。那么Proguard如何知道哪些東西是可以改名,而哪些是不能改變的呢?
這個是靠proguard.cfg文件來進行配置的。Android工程中默認自動生成的proguard.cfg已經(jīng)針對Android的一般情況進行了配置,我們打開這個配置文件。內(nèi)容大概如下:
1 -optimizationpasses 5
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的子類。因為這些子類,都是可能被外部調(diào)用的。
另外,它還保留了含有native方法的類、構(gòu)造函數(shù)從xml構(gòu)造的類(一般為View的子類)、枚舉類型中的values和valueOf靜態(tài)方法、繼承Parcelable的跨進程數(shù)據(jù)類。
在實際的一個工程項目中,可能Google自動生成的配置不能勝任我們的混淆工作。所以,我們往往需要自己編寫一些ProGuard配置。這方面的資料在官網(wǎng)的Manual -> Usage里有詳細說明。大家可以研究一下。
參考資料
《ProGuard 官方》
《Android 2.3 代碼混淆proguard技術(shù)介紹》
轉(zhuǎn)載請注明出處:http://m.tkk7.com/zh-weir/archive/2011/07/12/354190.html