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

http://blog.csdn.net/xjanker2/article/details/6888479

发布在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 theproguard.cfgfile 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 theAndroidManifest.xmlfile
  • a method called from JNI
  • dynamically referenced fields and methods

The defaultproguard.cfgfile tries to cover general cases, but you might encounter exceptions such asClassNotFoundException, 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-keepline in theproguard.cfgfile. For example:

- keep public class < MyClass >

There are many options and considerations when using the-keepoption, so it is highly recommended that you read theProGuard Manualfor more information about customizing your configuration file. TheOverview of Keep optionsandExamples sectionare particularly helpful. TheTroubleshootingsection 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..., 搞定。


 
 
 
 
 
 

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值