Multidex and the Dex Method Limit

suggest change

Versions

This topic was documented against Android 5.0 through 7.1.

Introduction

A DEX file is the Dalvik Executable bytecode inside an APK. It contains the compiled code used to run the app.

The Dalvik Executable specification limits the total number of methods that can be referenced within a single DEX file to 65,536 (64K), including Android framework methods, library methods, and methods in your own code.

To get past this limit, configure the app build to generate more than one DEX file. That setup is called multidex.

Remarks

What is dex?

Dex is the name of the file format and encoding to which Android Java code is compiled. Early versions of Android would load and execute dex binaries directly in a virtual machine named Dalvik. More recent versions of Android use the Android Runtime (ART), which treats dex files as an intermediate representation and performs further compilations on it prior to running the application.

Dex is a very old file format, in terms of the lifespan of smartphones, and was designed for devices whose main memory was measured in tens of megabytes. The design limitations of those days have remained with us to this day.

The problem:

The dex file format encodes a limit to the number of methods that can be referenced in a single binary. The bytecode method index is 16 bits, so one DEX file can reference at most 65,536 methods (indices 0 through 0xFFFF). If an application references more than that, the DEX merge fails.

What to do about it:

Google has provided a way around this problem, called Multidex. It has compile-time and run-time components. As its name implies, at compile-time it will divide code between one or more dex files. At runtime, it will teach the default ClassLoader how to look up classes from these files.

This approach works well on newer devices, but has some substantial drawbacks. It can increase application startup time dramatically, and on older devices can cause Application Not Responding failures.

Multidex, while effective, should be avoided if possible.

How to avoid the limit:

Before configuring your app to enable use of 64K or more method references, you should take steps to reduce the total number of references called by your app code, including methods defined by your app code or included libraries. The following strategies can help you avoid hitting the dex reference limit:

The first point requires diligence and discipline on the part of the developer. When incorporating third-party libraries, one must consider the size of the library. For example, two popular JSON libraries are Jackson and Gson. Functionally they are quite similar, but Gson tends to see greater use in Android. One reason is that Jackson weighs in around 9,000 methods, whereas Gson contributes 1,900.

There are several tools available to help developers keep track of the size of their application:

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents