解决“Cannot merge new index 67208 into a non-jumbo instruction”的问题

上一篇 / 下一篇  2014-07-08 15:07:24 / 个人分类:step by step android测试

转自:http://www.cnblogs.com/frydsh/archive/2013/02/20/2918969.html   

   在将ADT和SDK Tool升级到最新(分别是21.1和16.0.1)之后,我的一个工程(相对比较大)在编译并运行的时候,出现错误,Eclipse控制台输出如下信息:

      Unable to execute dex: Cannot merge new index 67208 into a non-jumbo instruction!
      Conversion to Dalvik format failed: Unable to execute dex: Cannot merge new index 67208 into a non-jumbo instruction!

      很多人在升级ADT和SDK Tool之后,都会遇到这个问题,只是错误信息中的数字不同而已。

      而且,我还发现一个现象:如果只是编译,但不生成APK,并不会出错;其实,从上面的错误信息中也可以看出一些线索 --- 它是在将jar文件转换成dex文件的时候出错的。

      网上给出的解决方案是,将dex.force.jumbo=true添加到project.properties文件中,然后清理工程,并重新编译。

      这个方法可以解决编译阶段问题,但是产生的APK在某些机器上不能安装(Installation error: INSTALL_FAILED_DEXOPT),针对这个问题的一个可能解释是:

      最新的ADT和SDK Tool在将jar转化成dex的时候,可能会合并类的代码,这将导致巨大的类;类中的每一个方法都分配有一个id,字节码中以id标识和调用方法;早期的Dalvik VM内部使用short类型变量来标识方法的id,最大值限制在65535;综合上述因素,代码在安装的时候,不能通过验证,所以安装失败。

      最新的Android可能已经解决了这个问题,但是更早的Android版本可能仍然存在此问题。

      因此,由于大量遗留机器的存在,这个问题是不能彻底解决的,一个临时的解决方案是:删掉没有实际使用的代码,或者使用ProGuard处理代码(可以减小代码体积)。

      一个不幸的推论是:随着一个软件功能的增加,代码的膨胀,APK包终将超出可以处理的范围,也许就是8M(指APK包里面的classes.dex).

      与此问题相关的两个讨论组是:

      https://code.google.com/p/android/issues/detail?id=40409

      https://groups.google.com/forum/?fromgroups=#!topic/adt-dev/tuLXN9GkVas

也可以在编译器的compile设置的地方进行选项的勾选。

我们这个问题的原因大概是:

Ufnortunately method call is encoded with a method_id being a short int (so only 65535 different methods), unlike const string access.

So, to summarize, even though the .dex specs allows more than 65535 classes and methods, the vm doesn't support large number of classes/methods, there wasn't any explicit error message.

一个vm最多只能有65536个方法!

dex.force.jumbo是干嘛的?

To my knowledge, dex.force.jumbo activates the const-string-jumbo opcode which allows to refer to static strings when you have more than 65k strings in your dex file.

如果超过了65k个字符串,启用dex.force.jumbo这个参数才可以引用到所有的字符串。

usually, dx will use the shortest instruction it can. this can make merging impossible if an instruction would need to be widened to fit more bits of string index (or whatever). dex.force.jumbo says "always use the wide form, even if you don't need to", to improve the chances of being able to merge later.

dex.disable.merger的官方解释参考:http://blog.toolib.net/tools/sdk/eclipse-adt.html:

Added a flag to disable dex merging to deal with cases where merging could generate a broken dex file. If this happens to your project, add the following setting to your project.properties file: dex.disable.merger=true。This setting causes the build system to revert to the older, slower dex processing that does not pre-dex libraries.


TAG:

 

评分:0

我来说两句

Open Toolbar