1、问题描述
当打包应用不成功报以下错误
dalvik compiler limitation on 65536 methods (Unable to execute dex: method ID not in [0, 0xffff]: 65536)
2、问题分析
查看相关资料后发现,android编译应用时,生成的dex文件最多包含65535个方法,如果我们在应用中依赖过多的包,而这些包又包含了 很多的方法,
最后编译的时候会将依赖的包的所有方法打入到应用.dex文件中。
-
应用包方法的统计
如何统计应用.dex文件包含的方法数量,在github上下载对应的工具,如下连接所示:
https://github.com/mihaip/dex-method-counts,下载连接中的工具
操作步骤如下所示:
Simple tool to output per-package method counts in an Android DEX executable grouped by package, to aid in getting under the 65,536 referenced method limit. More details are in this blog post. To run it with Ant: $ ant jar $ ./dex-method-counts path/to/App.apk # or .zip or .dex or directory or with Gradle: $ ./gradlew assemble $ ./dex-method-counts path/to/App.apk # or .zip or .dex or directory You'll see output of the form: Read in 65490 method IDs. <root>: 65490 : 3 android: 6837 accessibilityservice: 6 bluetooth: 2 content: 248 pm: 22 res: 45 ... com: 53881 adjust: 283 sdk: 283 codebutler: 65 android_websockets: 65 ... Overall method count: 65490 |
另外该工具一般在linux环境中运行,运行后可以查看对应包包含的方法数量
- 问题原因
通过包包含的方法数量,可以知道那个依赖包包含了特别多的方法数量,Insteon应用中发现google-service.jar包含了了大概3万个方法的数量占了总数量的一半。
3、解决方案
通过一些资料的说明,解决这种问题有两种方案:
- 裁剪过大的依赖包
裁剪google-service.jar - 拆分dex文件
https://github.com/mmin18/Dex65536
如连接所示,我们将一些过大的依赖包单独编译成apk,然后程序动态加载dex文件,即可解决。