Android方法數超出限定的問題(multiDex,jumboMode)


在Android項目開發中,項目代碼量過大或通過引入很多jar導致代碼量急劇增加,會出現錯誤:

 android.dex.DexIndexOverflowException: Cannot merge new index xxxx into a non-jumbo instruction!

 錯誤出現的原因是 Android設定的方法數是65536個(DEX 64K problem),超過這個方法數,導致dex無法生成,就無法生成APK.

 限制原因: 早期的Dalvik VM內部使用short類型變量來標識方法的id,就有了 最大方法數的限制65536。

解決方法:

  • 刪除不用的方法,刪除不使用的jar。

  • 分包
    通過在defaultConfig中設置multiDexEnabled開啟分包模式,分包之后的Dex就低於了限制數,保證了正常的打包。

1 defaultConfig {
2     multiDexEnabled=true
3 }
  • 忽略方法數限制的檢查
1 android.dexOptions {
2     jumboMode = true
3 }

設置dexOptions的,不做方法數限制的檢查,這樣做的缺點是apk無法再低版本的設備上面安裝,會出現錯誤:

INSTALL_FAILED_DEXOPT

關於dexoptionsjumboMode在stackoverflow中有一段描述:

In the standard java world:

  • When you compile standard java code : the compiler produce *.class file. A *.class file contains standard java bytecode that can be executed on a standard JVM.

In the Android world:

  • It is different. You use the java language to write your code, but the compiler don't produce *.class files, it produce *.dex file. A *.dex file contains bytecode that can be executed on the Android Virtual Machine (dalvik) and this is not a standard Java Virtual Machine.
    To be clear: a dex file in android is the equivalent of class in standard java.
    So dexoptions is a gradle object where some options to configure this java-code-to-android-bytecode transformation are defined. The options configured via this object are :
    • targetAPILevel
    • force-jumbo mode (when enabled it allows a larger number of strings in the dex files)

在標准Java的世界
  當編譯java代碼時,編譯器生成.class文件。.class文件包含了java的字節碼。這些字節碼在JVM中執行。

在安卓的世界則不同:

  • 用java語音寫安卓的代碼,但是編譯器生成的是.dex文件,不是.java文件。.dex文件包含了在Android虛擬機中可以執行的字節碼,而不是JVM。所以.dex文件的作用和標准Java中的.class文件差不多。
    dexoptions是一個gradle對象,這個對象用來設置從java代碼向.dex文件轉化的過程中的一些配置選項。其中一個就是force-jumbo mode。force-jumbo mode允許你創建更大的.dex文件。

參考資料:

  1. https://segmentfault.com/a/1190000004187484
  2. https://stackoverflow.com/questions/24224186/what-is-dex-in-gradle/24224385#24224385
  3. http://google.github.io/android-gradle-dsl/current/com.android.build.gradle.internal.dsl.DexOptions.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM