您的位置:首页 > 移动开发 > Android开发

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

2012-01-10 22:41 429 查看
/article/1393562.html

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








                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐