3

When I was trying to run my project in Android Studio, I got the following error message

Execution failed for task ':CricHQ:dexDebug'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
/Applications/Android Studio.app/sdk/build-tools/19.0.1/dx -JXmx4g --dex --output 
Error Code:
2
Output:
    UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Cannot merge new index 66514 into a non-jumbo instruction!
    at com.android.dx.merge.InstructionTransformer.jumboCheck(InstructionTransformer.java:108)
    at com.android.dx.merge.InstructionTransformer.access$800(InstructionTransformer.java:25)
    at com.android.dx.merge.InstructionTransformer$StringVisitor.visit(InstructionTransformer.java:71)
    at com.android.dx.io.CodeReader.callVisit(CodeReader.java:114)
    at com.android.dx.io.CodeReader.visitAll(CodeReader.java:89)
    at com.android.dx.merge.InstructionTransformer.transform(InstructionTransformer.java:48)
    at com.android.dx.merge.DexMerger.transformCode(DexMerger.java:840)
    at com.android.dx.merge.DexMerger.transformMethods(DexMerger.java:811)
    at com.android.dx.merge.DexMerger.transformClassData(DexMerger.java:783)
    at com.android.dx.merge.DexMerger.transformClassDef(DexMerger.java:680)
    at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:540)
    at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:170)
    at com.android.dx.merge.DexMerger.merge(DexMerger.java:188)
    at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:439)
    at com.android.dx.command.dexer.Main.runMonoDex(Main.java:287)
    at com.android.dx.command.dexer.Main.run(Main.java:230)
    at com.android.dx.command.dexer.Main.main(Main.java:199)
    at com.android.dx.command.Main.main(Main.java:103)

Anyone can help me with this problem? Thanks a lot.

user3229536
  • 33
  • 2
  • 5

2 Answers2

17

If you're using Android Studio without gradle then Preferences->Compiler->Android Compilers->Force Jumbo Mode

If your using gradle then add to build.gradle

android {
    dexOptions {
       jumboMode true
    }
pzulw
  • 1,716
  • 15
  • 22
8

There's a limitation in Android on the number of Java methods you can have in a single dex file, and you're hitting that limit. Unfortunately, there's no easy solution to it. Your choices are:

  • Simplify your code. If you're using a lot of libraries, see if you can cut back on the number of libraries you're using. Some libraries have a lot of methods in them.
  • Use ProGuard even in your debug builds. ProGuard will strip out unused methods and can save a lot of space. To get started on ProGuard, look at these links:

If those approaches don't work, it's possible to split your application into multiple dex files and use classloader tricks to make it work at runtime, but that's more complicated.

Community
  • 1
  • 1
Scott Barta
  • 79,344
  • 24
  • 180
  • 163
  • Thanks a lot for your help. After I removed a lib from the project, it becomes fine to compile and run. – user3229536 Feb 12 '14 at 03:59
  • Can you give me more information about how to use ProGuard in debug builds? – user3229536 Feb 12 '14 at 04:02
  • _"If those approaches don't work, it's possible to split your application into multiple dex files and use classloader tricks to make it work at runtime, but that's more complicated."_ even more complicated than proguard? unbelievable. – Lovis Sep 03 '14 at 12:53
  • http://android-developers.blogspot.com/2011/07/custom-class-loading-in-dalvik.html -- basically you need a custom class loader, and you have to be careful about how you split classes between individual dex files so that all the dependencies work out, provided it can be done at all. – Scott Barta Sep 03 '14 at 16:27