Application too big? Unable to execute dex: Cannot merge new index into a non-jumbo instruction

22,335

Solution 1

It's related to the number of methods of libraries included in the project. For example if you have tracking in your app, just Google Analytics is ~7000 methods. In one of my projects using Lombok (2MB of JAR) gave me these problem. Solved getting rid of this library.

Solution 2

Your error is for the amount of strings (methods, members, etc) in a single dex file.

You need to compile you app using jumbo in dex with:

dex.force.jumbo=true

in project.properties

This increment the limit for strings in a dex files. And your project will probably compile.

Also with jumbo set, the is another limit of 64K only for methods in an single dex. If you get this limit in the future , you will need to remove some dependencies.

UPDATE: for build with Gradle: In Gradle you can enable jumboMode also in the build.gradle file with:

dexOptions {
    jumboMode = true
}

Check: Android Build: Dex Jumbo Mode in Gradle

Also with Gradle you can avoid the 64K limit for methods using multidex build, tutorial here: https://developer.android.com/tools/building/multidex.html

Solution 3

For gradle build, just add the dexOptions into build.gradle to enable jumbo mode:

android {
    dexOptions {
        jumboMode = true
    }
}

Remember to run "gradle clean" before your new building.

Solution 4

It looks like the problem occurs because all the class files from your project and JAR files are packed together before DEXing. This may not be completely true but any way of controlling this in our project has proven to be quite difficult. Even removing stuff that initially caused this problem, cleaning and rebuilding didn't fix the issue for us in a consistent way.

So we took this opportunity to switch our project to Android Studio and managed to solve the problem by turning on ProGuard for debug builds as well. More precisely we only use the shrink phase of the ProGuard's processing chain.

Gradle makes it very easy to turn on ProGuard for debug builds:

buildTypes {
    debug {
        runProguard true
        proguardFile 'proguard-project-debug.txt'
    }
}

And here is the debug ProGuard config we use:

-keep class com.your.code.**
# Use -keep to explicitly keep any other classes shrinking would remove
-dontoptimize
-dontobfuscate
-ignorewarnings

This does increase the build time of the project but the good side is that the debugger still works.

The only faster alternative I can think of is that any JAR files are manually stripped of the unused class files. But this is not only difficult to do it is also inconvenient when you want to use a slightly larger part of a library at a later time.

I hope this helps other developers struggling with this issue. And perhaps in the future Google can improve the compiler that does this pruning by default. Our APK DEX file went from 8MB to 2.9MB.

Newer gradle (1.0.0+) versions

In newer Versions of Android studio (1.0+) the bundled Gradle got updated. There were some changes on how the build mechanism works so your project Gradle file can now take advantage of the minifyEnabled and shrinkResources parameters. Current version is 1.1.0.

Keeping up with changes on a fast moving platform like Android takes effort but it is often rewarded with new features, tools and faster build times. So updating Android Studio and (carefully) updating your projects is worth the time you invest.

buildTypes {
    debug {
        proguardFile 'proguard-project-debug.txt'
        minifyEnabled true
        shrinkResources true
    }
}
Share:
22,335
rupps
Author by

rupps

Programming everyday since I was a kid and got my first computer back in 1983, where I enjoyed fantastic games like Xenon 1 for a while. But as this was a niche computer, software was nearly non-existant. And my journey started there ... from BASIC and 6502 Assembler in the funny 80´s, to today´s distributed cloud computing, I have been around digging as deep as possible. During the journey I studied Telecommunications Engineering, worked in Telefonica R&D, Lucent Technologies and Alcatel, mostly in the IPTV field, always taking charge of hardcore programming tasks, and finally created my independent "garage company" Nebular Streams, where research does not stop. LD HL, #C000 LD DE, #C001 LD BC, #3FFF LDIR RET

Updated on January 22, 2020

Comments

  • rupps
    rupps over 4 years

    I am getting the following error when I compile my app:

    [2014-05-07 21:48:42 - Dex Loader] Unable to execute dex: Cannot merge new index 65536 into a non-jumbo instruction!
    

    I am at the point that if I declare a new method anywhere in my package, I get this error. If I don't, the app compiles.

    I would like to know what exactly (and accurately) does this error mean. My app is big, but I don't think its that big! So:

    • Does the error mean I have too many methods? public? static? package? members?
    • Is it related to the methods/members of my root package, or also to the included JAR libraries?
    • Is there a way to get more debug information about this?

    I already know about that "jumbo" enabling flag addressed in the similar questions here in SO, however, I think jumbo mode is not available on the API level I'm targeting (ICS).

  • rupps
    rupps about 10 years
    thanks for the reply! do you know if there's a way to know the number of methods of my included JARs & the project? Kind of a Verbose compilation or something like that?
  • fpanizza
    fpanizza about 10 years
    jumbo is enabled in ICS, so this could help you :)
  • rupps
    rupps about 10 years
    unfortunately it doesn't work for me after adding the flag, as I think I'm hitting the limit for declared methods rather than strings. That's why I am after a way of getting debug information on the number of methods of every JAR.
  • Mattia Franchetto
    Mattia Franchetto about 10 years
    Maybe you can try the accepted answer in this thread.
  • rupps
    rupps almost 10 years
    thanks for the link! I also found that if I compile the project with ANT rather than from eclipse IDE, the method count is printed to the console.
  • rupps
    rupps over 9 years
    +1 for the nice solution of turning proguard on for debug builds. With -dontoptimize and -dontobfuscate the extra delay for building a debug version is not too much. Unfortunately in my current project I can't afford to switch to Android Studio because it's too big a change, but for upcoming projects it's probably the way to go.