Android: my application is too large and gives "Unable to execute dex: method ID not in [0, 0xffff]: 65536"?

I am trying to integrate my application with Box, Dropbox, and Google Drive. All 3 of these services require a number of 3rd party jars. Additionally, my application already requires a few 3rd party jars. Now when I try to run my application from eclipse I get the following error:

Unable to execute dex: method ID not in [0, 0xffff]: 65536 Conversion to Dalvik format failed: Unable to execute dex: method ID not in [0, 0xffff]: 65536

It seems that this error occurs because my application has too many methods. I'm fairly certain the bulk of these methods are from the 3rd party jars, so it is unrealistic to try to solve this by simplifying my code. I found these two suggestions online.

  1. add dex.force.jumbo=trueto project.properties (and use adt version 21). I did this but still get the error.

  2. Use multiple dex files as explained here: http://android-developers.blogspot.co.il/2011/07/custom-class-loading-in-dalvik.html. This seems likely to be the only option, but I don't understand how it applies in my case. The issue is that services like Drive have too many dependencies. Wouldn't this solution require that I modify the Drive source to use inflection when referring to its dependencies? (this is clearly not an option).

  3. Use proguard to shrink remove unused code/methods. Exporting my application with proguard does work, and the document service integration works as expected on a >4.0 device. However, classnotfound errors are thrown when testing on a 2.3 device.

So, I am hoping for some advice on this issue. Is option 2 a solution for my case? Is there another solution I should consider?

Answers


You can also develop one or more of these as a plugin to your main app, in the form of a separate APK available for download. That APK would expose some component that the main app would use -- since I do not know the nature of your integration with these services, I cannot make a more specific recommendation about that. You would use your own signature-level custom <permission> to secure communications between the two apps. And, as a bonus, if using the third-party library adds requirements for additional permissions, you would only need those permissions in the plugin APK, keeping your main APK smaller.


***NEW**** All of the other answers are now outdated. Here's the new fix

Android 5.0 and higher

Multi-dex support is included automatically. From the docs:

Android 5.0 and higher uses a runtime called ART which natively supports loading multiple dex files from application APK files. ART performs pre-compilation at application install time which scans for classes(..N).dex files and compiles them into a single .oat file for execution by the Android device. For more information on the Android 5.0 runtime, see Introducing ART.

Below Android 5.0

Simply add the Android mult-dex support tool to your gradle build:

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.0"

    defaultConfig {
        ...
        minSdkVersion 14
        targetSdkVersion 21
        ...

        // Enabling multidex support.
        multiDexEnabled true
    }
    ...
}

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

The Dalvik VM can have a maximum of 65536 methods per dex file, due to the bytecode instruction set not having a way to refer to method numbers requiring more than 16 bits (as pointed out by @danfuzz in the comments).

While it is possible to fix this using multiple dex files, Facebook found another fix that they could deploy within their app to get around the problem.


See vm/LinearAlloc.c and you can find this code: (5MiB under Android 2.3.3, 8MiB after Android 4.0 as my investigation)

#define DEFAULT_MAX_LENGTH (5*1024*1024)

...

LinearAllocHdr* pHdr;

...

pHdr->mapLength = DEFAULT_MAX_LENGTH;

I suppose that the 'Facebook fix' is editing this memory by using native C pointer. IMHO LinearAlloc problem and this method ID problem is different thing.


I faced this issue recently. After scouring the web for some more detailed implementation, I realized there wasn't much out there other than:

I realized that the problem was not necessarily that there were too many methods in my code, but that the full dex of my code and other libraries was the issue. So, if I could compile my code against the libraries but not include them in the classes.dex, and then dex the libraries separately, and then put it all together at runtime it should work. The only issue left to address is the class loader, which Facebook mentioned in passing.

So with a little reflection and some Groovy code, I think I came up with a relatively stable way to package the libraries and the application code into separate dex files.

https://gist.github.com/nickcaballero/7045993


Most of the problems with hitting the 65k method limit are related with the use of the mastodontic Google Play Services in your apps. Recently, you can get more granularity when using it.

Following this guide, you can use only parts that you want. Probably this will fix the problem, avoiding some black-magic tricks, or using multiDex. For example, if you only want Google Maps in your app (and you aren't using ads, wallet, google wear, analytics, etc...), using the entire dependency is a waste of time/space. You can use it that way:

compile com.google.android.gms:play-services-base:6.5.87
compile com.google.android.gms:play-services-maps:6.5.87

You can read the entire list of "parts" in this link


Here is a script I wrote for counting the number of methods in each jar (and in total) for a specific folder.

Once you count the methods you can concentrate on refactoring and removing heavy libraries.


You need to enable the Dex support for that. So you need to do these steps:

  1. Gradle plugin v0.14.0 for Android adds support for multi-dex. To enable, you just have to declare it in build.gradle:
android {
   defaultConfig {
      ...
      multiDexEnabled = true
   }
}
  1. if app support > 5.0 (that is, if your minSdkVersion is 20 or below) you also have to dynamically patch the application ClassLoader, so it will be able to load classes from secondary dexes. for that you can add this lib.
 dependencies {
      ...
      compile 'com.android.support:multidex:1.0.0'
    }
  1. enable in code for that you have these option. choose one which suits you best

A. Add MultiDexApplication in manifest manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.multidex.App"> <application
android:name="android.support.multidex.MultiDexApplication">
</application>
</manifest>

B. Extend the application by MultiDexApplication

public class App extends MultiDexApplication { .. }

C. install it in application in attaching base context.

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

}

For more go through this link MultiDex.


Need Your Help

Close the keyboard on UITextField

uitextfield ios-4.2 iphone

I'm a newbie developing for iOS devices. I inserted an UITextField on InterfaceBuilder, and I assigned with the code:

When to use JavaScript template engines?

javascript html templates memory-leaks separation-of-concerns

Here is an example of JavaScript template from Ben Nadel's demo single page long-lived AJAX application taken from: [source]