在實際的開發成產品apk的發佈過程中,我們經常會使用到代碼混淆技術。不過在講解如何進行代碼混淆之前,我先跟大家演示一下反編譯的過程,這樣才能讓大家明白爲什麼需要進行代碼混淆。
一、代碼反編譯
1.準備工作
首先我們準備好一個項目的apk文件(未進行混淆打包)、dex2jar工具(下載地址:http://code.google.com/p/dex2jar/)、jd-gui工具(下載地址:http://jd-gui.softpedia.com/)。
2.獲取CodeMixTest.apk中的classes.dex文件
將CodeMixTest.apk解壓,這裏我們爲了方便就直接解壓到當前文件夾下。在解壓出的文件中,我們可以看見有一個classes.dex文件,這就是我們的源代碼打包生成的文件。
3.使用dex2jar工具還原jar文件
dex2jar工具的作用就是將class.dex文件還原成.jar文件。先將dex2jar-0.0.9.15.zip解壓,然後將上面獲得的classes.dex文件拷貝至解壓的目錄中。
查看解壓目錄中,有dex2jar.bat文件。
啓動cmd,進入到dex2jar工具解壓的目錄中,運行命令:dex2jar.bat classes.dex,有如下結果。
查看dex2jar工具解壓的目錄中,生成了我們想要得到的classes_dex2jar.jar文件。
4.使用jd-gui工具查看jar文件
將jd-gui-0.3.5.windows.zip工具解壓,爲了方便我們直接解壓到當前文件夾,我們會看見一個jd-gui.exe文件。
雙擊jd-gui.exe,使用工具打開前面我們獲得的classes_dex2jar.jar文件。可以清楚的看見源代碼。
是企業真實的項目中,如果不經過代碼混淆,你的源碼就如此赤裸裸的暴露着。這樣對於公司來說是非常不安全的,並且不想發生的情況。既然我們已經看見了反編譯如此的威力,那麼我們有什麼辦法可以解決這個問題呢?看下面講解。
二、代碼混淆
1.準備工作
首先,我們得將我們的項目源碼、proguard工具(放心這個工具Google已經在sdk中爲我們提供了,在sdk\tools目錄下可以看到)、 proguard.cfg文件(該文件定義了混淆的規則,創建2.3以上項目自動生成,或者在網上可以很容易下載到)。
2.在你的項目中添加proguard.cfg文件
該文件Google公司已經爲我們準備好了,創建項目時,在項目的根目錄下就有了,但是是從2.3開始的。不過我們基於2.2的項目只需要將該文件拷貝到項目根目錄下即可。
3.修改項目的default.properties配置文件
Android項目默認在打包的時候是不混淆代碼的,所以我們必須得在default.properties文件中,添加該配置:proguard.config=proguard.cfg。
4.進行apk打包
好了,一切就緒,接下來我們就按着平常的打包步驟,進行打包操作。打完包後,我們使用上面講述的反編譯方法,查看現在的jar文件。
會發現,混淆了許多aa、bb、cc的包、aa、bb、cc的類和方法,這樣的代碼根本無法閱讀,也就達到了我們保護源代碼的目的了。
三、produard.cfg文件
我們在看看produard.cfg文件,該文件中描述了混淆代碼的規則,你如果想根據自己的需求來混淆代碼,就可以在本文件中編寫你的混淆規則。一般情況下,該文件的內容如下:
- -optimizationpasses 5
- -dontusemixedcaseclassnames//混淆時不會產生形形色色的類名
- -dontskipnonpubliclibraryclasses//指定不去忽略非公共的類庫
- -dontpreverify//不預校驗
- -verbose
- -optimizations !code/simplification/arithmetic,!field/*,!class/merging/*//優化
- //不進行混淆保持原樣
- -keep public class * extends android.app.Activity
- -keep public class * extends android.app.Application
- -keep public class * extends android.app.Service
- -keep public class * extends android.content.BroadcastReceiver
- -keep public class * extends android.content.ContentProvider
- -keep public class * extends android.app.backup.BackupAgentHelper
- -keep public class * extends android.preference.Preference
- -keep public class com.android.vending.licensing.ILicensingService
- //保護指定的類和類的成員的名稱,如果所有指定的類成員出席
- -keepclasseswithmembernames class * {
- native <methods>;
- }
- //保護指定的類和類的成員,但條件是所有指定的類和類成員是要存在
- -keepclasseswithmembers class * {
- public <init>(android.content.Context, android.util.AttributeSet);
- }
- -keepclasseswithmembers class * {
- public <init>(android.content.Context, android.util.AttributeSet, int);
- }
- //保護指定類的成員,如果此類受到保護他們會保護的更好
- -keepclassmembers class * extends android.app.Activity {
- public void *(android.view.View);
- }
- -keepclassmembers enum * {
- public static **[] values();
- public static ** valueOf(java.lang.String);
- }
- //保護指定的類文件和類成員
- -keep class * implements android.os.Parcelable {
- public static final android.os.Parcelable$Creator *;
- }
四、可能出現的問題
1.因爲使用了升級後使用android-support-v4而發生混淆錯誤,無法打包出現如下錯誤:
Unexpected error while evaluating instruction: Class = [android/support/v4/view/AccessibilityDelegateCompat$AccessibilityDelegateJellyBeanImpl] Method = [newAccessiblityDelegateBridge(Landroid/support/v4/view/AccessibilityDelegateCompat;)Ljava/lang/Object;] Instruction = [18] areturn Exception = 1 (Can't find any super classes of [android/support/v4/view/AccessibilityDelegateCompatIcs$1] (not even immediate super class [android/view/View$AccessibilityDelegate])) Unexpected error while performing partial evaluation: Class = [android/support/v4/view/AccessibilityDelegateCompat$AccessibilityDelegateJellyBeanImpl] Method = [newAccessiblityDelegateBridge(Landroid/support/v4/view/AccessibilityDelegateCompat;)Ljava/lang/Object;] Exception = 1 (Can't find any super classes of [android/support/v4/view/AccessibilityDelegateCompatIcs$1] (not even immediate super class [android/view/View$AccessibilityDelegate])) java.lang.IllegalArgumentException: Can't find any super classes of [android/support/v4/view/AccessibilityDelegateCompatIcs$1] (not even immediate super class [android/view/View$AccessibilityDelegate]) at proguard.evaluation.value.ReferenceValue.generalize(ReferenceValue.java:287)
你需要在proguard.cfg文件中添加如下代碼:
-libraryjars libs/android-support-v4.jar
-dontwarn android.support.v4.**
-keep class android.support.v4.** { *; }
-keep interface android.support.v4.app.** { *; }
-keep public class * extends android.support.v4.**
-keep public class * extends android.app.Fragment
2.如果你使用了第三方的包,你需要使用一下配置,讓ProGuard知道庫中的一些類並不是在所有的API中可用:
-libraryjars libs/roboguice-2.0.jar
-dontwarn roboguice.**
3.在打包的過程中,出現如下錯誤:
Proguard returned
with error code 1. See console
[2012-09-25
09:50:39 - estore3601] Error: @C:\Users\hp\AppData\Local\Temp\android_5192919586673521494.pro (文件名、目錄名或卷標語法不正確。)
你需要系在ProGuard最新版本,如v4.8。然後解壓produard4.8.zip文件,將bin和lib兩個文件夾覆蓋sdk目錄\tool\proguard目錄中同名的文件夾。