Android Trick 14: proguard導致簽名後的apk報錯:ClassNotFoundException 的問題解決

來源:互聯網
上載者:User

發布在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..., 搞定。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.