Enable Android MultiDex

Apr 18, 2018
DexArchiveMergerException - The number of method references in a .dex file cannot exceed 64K

org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'. at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:100) ... Caused by: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: ... The number of method references in a .dex file cannot exceed 64K. Learn how to resolve this issue at https://developer.android.com/tools/building/multidex.html

You need to enable MultiDex due to exceeding 64K Methods limit (using after include large libraries like Google Play, Firebase, etc.)

Edit your module build.gradle.

android {
    defaultConfig {
        applicationId "..."
        multiDexEnabled true
    }
}

dependencies {
    // https://mvnrepository.com/artifact/com.android.support/multidex
    implementation 'com.android.support:multidex:1.0.3'
}

If you override Application, change it to extend MultiDexApplication.

public class MyApplication extends MultiDexApplication { ... }

If you do not override Application, edit your AndroidManifest.xml.

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

NOTE: You no longer need a dev mode to enable multi-dexing during development (e.g. productFlavors { dev { minSdkVersion 21 } })

You no longer need a dev mode to enable multi-dexing during development, and this can break API version checks less...

In the past, our documentation recommended creating a dev product flavor with has a minSdkVersion of 21, in order to enable multidexing to speed up builds significantly during development. That workaround is no longer necessary, and it has some serious downsides, such as breaking API access checking (since the true minSdkVersion is no longer known).

In recent versions of the IDE and the Gradle plugin, the IDE automatically passes the API level of the connected device used for deployment, and if that device is at least API 21, then multidexing is automatically turned on, meaning that you get the same speed benefits as the dev product flavor but without the downsides.

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.