android代碼混淆筆記
混淆處理的apk被反編譯後代碼中包名類名等都變成abcd之類,很難看懂。
使用代碼混淆,啟用混淆器,對相關檔案進行編輯,然後打包簽名就可以了;
------------
在2.3的版本中,項目中有這個檔案 proguard.cfg (沒有的可以手動添加)
添加一句: proguard.config=proguard.cfg
proguard.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 com.android.vending.licensing.ILicensingService-keepclasseswithmembernames class * { native ;}-keepclasseswithmembernames class * { public (android.content.Context, android.util.AttributeSet);}-keepclasseswithmembernames class * { public (android.content.Context, android.util.AttributeSet, int);}-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.0以後的版本,項目中的檔案是project.properties和proguard-project.txt。
開啟project.properties,取消下面這行代碼的注釋:
proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
預設的設定是不帶最佳化功能的,可以用以下設定加上代碼最佳化功能:
#proguard.config=${sdk.dir}/tools/proguard/proguard-android-optimize.txt:proguard-project.txt
-------------------------------------------------------
proguard-project.txt 檔案的一些編輯規則:
-libraryjars libs/android-support-v4.jar
-libraryjars libs 載入第三方Jar包
-ignorewarnings 去除代碼中的警告
-keep class com.xxx.xxx.**
-keep 保留不混淆的類
此類的公用方法保留,不混淆。
-keep class com.xx.xx.Test{
public *;
}
保護指定的類檔案和類的成員
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
----------------------------------------------------
用Eclipse工具打包簽名:
在Eclipse選中工程項目,右鍵菜單--> Android Tools
---> Export Signed Application Package...帶RSA數位簽章
---> Export Unsigned Application Package...不帶數位簽章
選擇一種方式按照嚮導操作,產生的Apk就是混淆處理過的。
----------------------------------------