在實際的開發成產品apk的發佈過程當中,咱們常常會使用到代碼混淆技術。不過在講解如何進行代碼混淆以前,我先跟你們演示一下反編譯的過程,這樣才能讓你們明白爲何須要進行代碼混淆。 java
1、代碼反編譯 android
1.準備工做 windows
首先咱們準備好一個項目的apk文件(未進行混淆打包)、dex2jar工具(下載地址:http://code.google.com/p/dex2jar/)、jd-gui工具(下載地址:http://jd-gui.softpedia.com/)。 安全
2.獲取CodeMixTest.apk中的classes.dex文件 app
將CodeMixTest.apk解壓,這裏咱們爲了方便就直接解壓到當前文件夾下。在解壓出的文件中,咱們能夠看見有一個classes.dex文件,這就是咱們的源代碼打包生成的文件。 ide
3.使用dex2jar工具還原jar文件 工具
dex2jar工具的做用就是將class.dex文件還原成.jar文件。先將dex2jar-0.0.9.15.zip解壓,而後將上面得到的classes.dex文件拷貝至解壓的目錄中。 優化
查看解壓目錄中,有dex2jar.bat文件。 ui
啓動cmd,進入到dex2jar工具解壓的目錄中,運行命令:dex2jar.bat classes.dex,有以下結果。 google
查看dex2jar工具解壓的目錄中,生成了咱們想要獲得的classes_dex2jar.jar文件。
4.使用jd-gui工具查看jar文件
將jd-gui-0.3.5.windows.zip工具解壓,爲了方便咱們直接解壓到當前文件夾,咱們會看見一個jd-gui.exe文件。
雙擊jd-gui.exe,使用工具打開前面咱們得到的classes_dex2jar.jar文件。能夠清楚的看見源代碼。
是企業真實的項目中,若是不通過代碼混淆,你的源碼就如此赤裸裸的暴露着。這樣對於公司來講是很是不安全的,而且不想發生的狀況。既然咱們已經看見了反編譯如此的威力,那麼咱們有什麼辦法能夠解決這個問題呢?看下面講解。
2、代碼混淆
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的類和方法,這樣的代碼根本沒法閱讀,也就達到了咱們保護源代碼的目的了。
3、produard.cfg文件
咱們在看看produard.cfg文件,該文件中描述了混淆代碼的規則,你若是想根據本身的需求來混淆代碼,就能夠在本文件中編寫你的混淆規則。通常狀況下,該文件的內容以下:
[java] view plaincopy
-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 *;
}
4、可能出現的問題
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目錄中同名的文件夾。
轉載地址:http://blog.csdn.net/p106786860/article/details/11974863