- ProGuard作用
ProGuard通過刪除無用代碼,將代碼中類名、方法名、屬性名用晦澀難懂的名稱重命名從而達到代碼混淆、壓縮和優化的功能,跟JavaScript的混淆壓縮類似。壓縮和優化使得編譯後apk包更小。混淆可以保證代碼在被反編譯後讀懂的難度很大,防止逆向工程。這點也是我們在應用發佈前需要ProGuard的一大原因。
- ProGuard的使用
buildTypes { release { minifyEnabled true//打開混淆開關 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'//指定混淆配置文件 } }
注意下列類不能進行混淆:
(1)、反射用到的類
(2)、在AndroidManifest中配置的類(四大組件Activity、Service等的子類及Framework類默認不會進行混淆)
(3)、Jni中調用的類
- 如何進行bug分析
代碼混淆生成apk之後,項目下面會多出來一個build\outputs\mapping\release文件夾,下面分別解釋release文件夾中四個文件的作用
先上圖
dump.txt : 描述了apk中所有類 文件中內部的結構體。( Describes the internal structure of all the class files in the .apk file )
mapping.txt : 列出了原始的類、方法和名稱與混淆代碼間的映射。( Lists the mapping between the original and obfuscated class, method, and field names. )
seeds.txt : 列出了沒有混淆的類和方法。( Lists the classes and members that are not obfuscated )
usage.txt : 列出從apk中刪除的代碼。( Lists the code that was stripped from the .apk )
接下來是操作步驟
1.cmd進入sdk/tools/proguard/bin目錄。
2.將混淆後的日誌cache_file_name.txt和上文提到的mapping文件放入此目錄中。
3.執行命令:retrace.bat mapping.txt cache_file_name.txt
我們來看下前後對比,不如我們的錯誤日誌是這樣的
Casused by: Java.lang.NullPointerException
at com.example.test d.a.c(SourceFile:110)
at com.example.test d.a.c(SourceFile:88)
at com.example.test .activity.CommitFragmentBase.init(SourceFile:1330)
執行完命令後得到這樣的
Casused by: java.lang.NullPointerException
at com.example.test loginActivity.login.setView(SourceFile:110)
at com.example.test dloginActivity.login.setView(SourceFile:88)
at com.example.test .activity.CommitFragmentBase.init(SourceFile:1330)