Skip to content

Multidex Support

Harrison Saylor edited this page Sep 20, 2016 · 1 revision

Enabling Multidex Support

Occassionally when using many libriries or a extremely large library (such as the Watson Java SDK) in an Android project you will recieve an error such as

ClassNotFoundException: Didn't find class on path: DexPathList

or

Error converting bytecode to dex

This is likely because you exceeded the dex limit. Android is bulit on the Dalvik Executable specification, which by default has a 64k reference limit. Luckily, you can bypass the limit by enabling Multidex Support. This is a simple two step process.

First, modify the build.gradle file to include the multidex support library and enable multidex output:

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

Then, add the MultiDexApplication class to your manifest xml:

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

Proguard

Configuring ProGuard settings to enable shrinking removes unused code from APKs. This helps you stay under the 64k reference limit, however it should not be a replacement for writing consise code. To enable code shrinking add the following to your build.gradle:

android {
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile(‘proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
Clone this wiki locally