DexIndexOverflowException Only When Running Tests

com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536 

Android application (APK) files contain executable bytecode files in
the form of Dalvik Executable (DEX) files, which contain the compiled
code used to run your app. The Dalvik Executable specification limits
the total number of methods that can be referenced within a single DEX
file to 65,536, including Android framework methods, library methods,
and methods in your own code. Getting past this limit requires that
you configure your app build process to generate more than one DEX
file, known as a multidex configuration.

The Android plugin for Gradle available in Android SDK Build Tools 21.1 and higher supports multidex as part of your build configuration. Make sure you update the Android SDK Build Tools tools and the Android Support Repository to the latest version using the SDK Manager before attempting to configure your app for multidex.

Setting up your app development project to use a multidex configuration requires that you make a few modifications to your app development project. In particular you need to perform the following steps:

  1. Change your Gradle build configuration to enable multidex
  2. Modify your manifest to reference the MultiDexApplication class

Modify your app Gradle build file configuration to include the support library and enable multidex output .

    android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"

    defaultConfig {
        ...
        minSdkVersion 14
        targetSdkVersion 25
        ...

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

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

In your manifest add the MultiDexApplication class from the multidex support library to the application element.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.multidex.myapplication">
    <application
     android:name="android.support.multidex.MultiDexApplication">

    </application>
</manifest>

Read Official Document about
MultiDex

If your Application class is extending some other class and you don’t want to or can’t change it, override attachBaseContext() as shown below:

public class MyApplication extends MultiDexApplication { 
   @Override 
   protected void attachBaseContext(Context base) { 
      super.attachBaseContext(base); 
      MultiDex.install(this); 
   } 
}

Then

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.multidex.myapplication">
    <application
     android:name=".MyApplication">
    </application>
</manifest>

Conclusion

While the library fixes the DEX 64K problem in most cases, it should
be treated as a last resort. Before attempting to use it, you should
audit your project for unwanted dependencies and remove as much unused
code as possible using ProGuard.

Leave a Comment