本文图文讲解Android Studio在打包时,构建失败报错误:Cannot fit requested classes in a single dex file (# methods: 67658 > 65536)的解决方案。
今天打算给APP加个视频播放功能,但加个十几兆依赖包后编译报错了:Error: Cannot fit requested classes in a single dex file (# methods: 67667 > 65536),实际上就是超过最大方法数限制的问题,是由于DEX文件格式限制,一个DEX文件中method个数采用使用原生类型short来索引文件中的方法,也就是2个字节共计最多表达65536个method,field/class的个数也均有此限制。对于DEX文件,则是将工程所需全部class文件合并且压缩到一个DEX文件期间,也就是Android打包的DEX过程中, 单个DEX文件可被引用的方法总数(自己开发的代码以及所引用的Android框架、类库的代码)被限制为65536。
Android 5.0(API 级别 21)之前的平台版本使用 Dalvik 运行时来执行应用代码。默认情况下,Dalvik 限制应用的每个 APK 只能使用单个 classes.dex 字节码文件。要想绕过这一限制,您可以使用multidex,然后管理对其他 DEX 文件及其所包含代码的访问。
Android 5.0(API 级别 21)及更高版本使用名为 ART 的运行时,后者原生支持从 APK 文件加载多个 DEX 文件。ART 在应用安装时执行预编译,扫描 classesN.dex 文件,并将它们编译成单个 .oat 文件,供 Android 设备执行。因此,如果您的 minSdkVersion为 21 或更高值,则不需要 Dalvik 可执行文件分包支持库。
也就是说你可以把minSdkVersion改成为21 或更高值来避免这个错误,或者引入multidex来解决这个问题,引入multidex网上的说法是要在app下的build.gradle加入multiDexEnabled true和implementation ‘com.android.support:multidex:1.0.3’,并且在加入“MultiDex.install(this);”这行代码,但我发现可以不用加入“MultiDex.install(this);”这行代码:
好了根据实际情况自己选择要用哪种方式处理吧。
参考:https://www.jianshu.com/p/9d9c2dbba223
历史上的今天:
- 2023: 【转载】人生的意义是什么?这个答案堪称完美(0)
展开阅读全文
I constantly emailed this web site post page to all my friends, because if like to read it after that my links will too. Chelsae Pen Voorhis
I simply wanted to thank you one more time for that amazing web page you have made here. Jacenta Anderson Darya
Great post! We are linking to this great post on our site. Randa Korey Seleta
Major thankies for the blog article. Thanks Again. Keep writing. Stephannie Zachariah Jehovah
Hello. This article was extremely fascinating, especially because I was searching for thoughts on this subject last Monday. Lolita Derk Eberly
There is definately a great deal to find out about this subject. I really like all the points you made. Genna Russell Niko
Wonderful post! We will be linking to this particularly great post on our site. Keep up the good writing. Anneliese Connie Teriann
Thanks a lot for the blog article. Much thanks again. Want more. Kaitlin Justis True
Hello, after reading this awesome paragraph i am also happy to share my knowledge here with colleagues. Missy Vance Kurzawa
I loved your blog post. Thanks Again. Really Great. Sharia Gavin Loveridge
Thanks for helping out, superb info. “You must do the things you think you cannot do.” by Eleanor Roosevelt. Nessie Tyrone Iverson