Android Trick 14: proguard导致签名后的apk报错:ClassNotFoundException 的问题解决

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


发布了56 篇原创文章 · 获赞 6 · 访问量 51万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览