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

StackOverflow https://stackoverflow.com/questions/23527218

  •  17-07-2023
  •  | 
  •  

Question

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).

Was it helpful?

Solution 3

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.

OTHER TIPS

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

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.

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
    }
}

Some interesting observations. Same error may appear if you have multi-flavor project. It's confusing. Turned out that I attempted run app with generic command: gradlew installDebug. When I've changed command line to look like this problem is gone. Don't forget to replace Flavor part with your actual one.

gradlew installFlavorDebug
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top