处理Android Studio编译出现com.android.dex.DexIndexOverflowExcepti: method ID not in [0, 0xffff]: 65536

同时在工程中引入了多个第三方jar包,导致调用的方法数超过了android设定的65536个(DEX 64K problem),进而导致dex无法生成,也就无法生成APK文件,编译工具提示的错误信息可能像这样:

UNEXPECTEDTOP-LEVEL EXCEPTION:
com.Android.dex.DexIndexOverflowException:method ID not in [0, 0xffff]: 65536
         atcom.android.dx.merge.DexMerger$6.updateIndex(DexMerger.Java:484)
         atcom.android.dx.merge.DexMerger$IdMerger.mergeSorted(DexMerger.java:261)
         atcom.android.dx.merge.DexMerger.mergeMethodIds(DexMerger.java:473)
         atcom.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:161)
         atcom.android.dx.merge.DexMerger.merge(DexMerger.java:188)
         atcom.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:504)
         atcom.android.dx.command.dexer.Main.runMonoDex(Main.java:334)
         atcom.android.dx.command.dexer.Main.run(Main.java:277)
         atcom.android.dx.command.dexer.Main.main(Main.java:245)
         atcom.android.dx.command.Main.main(Main.java:106)
FAILED
FAILURE:Build failed with an exception.
* Whatwent wrong:
Executionfailed for task ':app:compileDebugJavaWithJavac'.
>Compilation failed; see the compiler error output for details.
* Try:
Run with--stacktrace option to get the stack trace. Run with --info or --debug optionto get more log output

Conversionto Dalvik format failed:
Unableto execute dex: method ID not in [0, 0xffff]: 65536
或者
troublewriting output:
Too manyfield references: 131000; max is 65536.
You maytry using --multi-dex option.

采用分包的编译方式,解决步骤如下:

1. 在主模块的build.gradle文件的dependencies标签下加入如下代码:

dependencies { 
	... 
   	compile 'com.android.support:multidex:' 
   	... 
}


2. 在主模块的build.gradle文件的android标签下的defaultConfig标签下加入multiDexEnabled项

defaultConfig { 
  	... 
	multiDexEnabled true 
	... 
}


3.在工程代码中找到自己的Application类,根据项目具体情况,按如下方案的其中一个做修改:

a. 没有自定义的Application,所以就在manifest文件中直接加入如下代码:

  
  
      
        ...  
      
  


b. 如果有自定义的Application,让该类继承android.support.multidex.MultiDexApplication类,而不是android.app.Application

c.   如果有自定义的Application,但不想按照步骤b来改变继承的父类,请重载Application的attachBaseContext方法

protected void attachBaseContext(Context base) {  
	super.attachBaseContext(base);  
	MultiDex.install(this);  
}  



你可能感兴趣的:(Android)