發布在Android Market上的apk應用需要經過簽名,簽名的時候如果存在proguard.cfg並且在default.properties裡邊啟動了proguard:proguard.config=proguard.cfg,那麼在簽名打包時會使用proguard對代碼進行壓縮、最佳化和混淆。
在使用時遇到了一些問題,
首先,隨Android SDK內建的預設的proguard4.4?,簽名後的apk安裝成功,但運行時報ClassNotFoundException。檢查了一遍簽名的過程,發現簽名完成時會彈出“ conversion to dalvik format failed with error 1” 錯誤,查網上看到了遇到相同問題的開發,據他們說在官網下載最新版4.6後,此問題解決。於是在http://proguard.sourceforge.net/下載到最新版後,重新運行簽名,發現錯誤沒有了。
但是,新簽的apk然後報ClassNotFoundException異常,最初的問題還沒解,繼續查。發現,在sdk文檔中有相關描述:
Configuring ProGuard
For some situations, the default configurations in the proguard.cfg
file will suffice. However, many situations are hard for ProGuard to analyze correctly and it might remove code that it thinks is not used, but your application actually needs. Some examples include:
- a class that is referenced only in the
AndroidManifest.xml
file
- a method called from JNI
- dynamically referenced fields and methods
The default proguard.cfg
file tries to cover general cases, but you might encounter exceptions such as ClassNotFoundException
, which happens when ProGuard strips away an entire class that your application calls.
You can fix errors when ProGuard strips away your code by adding a -keep
line in the proguard.cfg
file. For example:
-keep public class <MyClass>
There are many options and considerations when using the -keep
option, so it is highly recommended that you read the ProGuard Manual for more information about customizing your configuration file. The Overview of Keep options and Examples section are particularly helpful. The Troubleshooting section of the ProGuard Manual outlines other common problems you might encounter when your code gets stripped away. (developer.android.com/guide/developing/tools/proguard.html)
OK! 重要知道問題原因,預設的proguard配置還是不太全面,有些類在壓縮混淆的時候去掉了,這種情況碰巧這次中招了,於是按照指南,馬上在proguard.cfg配置中加入一行 keep public class ...sth..., 搞定。