android – 64k limit exceeded on older APIs than lollipop, but not newer-ThrowExceptions

Exception or error:

So I am wondering why I encounter the 64k dex method limit when trying to run my app on android versions older than lollipop, when it runs just fine on the more recent versions.

Could it be, because the support libraries are actually being referenced when running on the older versions?

This is my gradle:

    apply plugin: ''

    android {
        compileSdkVersion 23
        buildToolsVersion '23.0.2'
    lintOptions {
        checkReleaseBuilds true
        // Or, if you prefer, you can continue to check for errors in release builds,
        // but continue the build even when errors are found:
        abortOnError false

    defaultConfig {
        applicationId "com.domain.myapp"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 27
        versionName "1.2"

        // Vector compat
        vectorDrawables.useSupportLibrary = true 

    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), ''

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7

dependencies {

    compile files('libs/commons-io-2.4.jar')
    compile files('libs/activation.jar')
    compile files('libs/additionnal.jar')
    compile files('libs/mail.jar')
    compile project(':libraries:preferencefragment')

    // Gmail API
    compile('') {
        exclude group: 'org.apache.httpcomponents'
    compile('') {
        exclude group: 'org.apache.httpcomponents'

    // Play Services
    compile ''
    compile ''
    compile ''
    compile ''
    compile ''

    // Support libraries
    compile ''
    compile ''
    compile ''
    compile ''

    compile 'com.github.bumptech.glide:glide:3.6.1'
    compile ''
    compile 'com.sothree.slidinguppanel:library:2.0.3'
    compile 'com.commit451:PhotoView:1.2.5'

    compile('com.github.afollestad.material-dialogs:core:') {
        transitive = true


To clarify: This happens when I try to run the app on an emulator running e.g. KitKat API 19

Crash logs from the gradle console:

AGPBI: {"kind":"error","text":"The number of method references in a .dex file cannot exceed 64K.\nLearn how to resolve this issue at","sources":[{}],"original":"UNEXPECTED TOP-LEVEL EXCEPTION:\ method ID not in [0, 0xffff]: 65536\n\tat$6.updateIndex(\n\tat$IdMerger.mergeSorted(\n\tat\n\tat\n\tat\n\tat\n\tat\n\tat\n\tat\n\tat\n","tool":"Dex"}


FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':App:transformClassesWithDexForDebug'.
> java.util.concurrent.ExecutionException: org.gradle.process.internal.ExecException: Process 'command '/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/bin/java'' finished with non-zero exit value 2

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.


Crash message from the Message window:

Error:The number of method references in a .dex file cannot exceed 64K.
Learn how to resolve this issue at
Error:Execution failed for task ':App:transformClassesWithDexForDebug'.
> java.util.concurrent.ExecutionException: org.gradle.process.internal.ExecException: Process 'command '/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/bin/java'' finished with non-zero exit value 2
How to solve:

To answer your specific question is:

  • This method count limitation is on the DEX (Dalvik Executable) file.
  • A common workaround for this limitation is to have multiple DEX files.
  • Older versions of Android does not natively support multiple DEX files.
  • Starting from Lollipop the system supports it natively.

So that’s why it fails on older devices but it works on newer devices. It has nothing to do with support libraries.

To add more useful content to the my answer:

But maybe what some might want to know is how to workaround it on older devices, please note that I used the word natively on my bullet points above. That means, that there’re workarounds to make older platforms support it, but you have to code those workarounds into your app.

The workaround (and even the issue itself) is detailed explained on this Google guide:

The base of it is:

  • add multidex to your dependencies compile ''
  • enable multiDex on your build script inside android->default config multiDexEnabled true
  • on the manifest make your application extends from MultidexApplication android:name="" or, if you need to sub-class Application for your own app, make your Application extends from it.


Completely agree with @Budius. You must mention an application class in manifest. And have your application class extend MultiDexApplication.

There is one more thing needed. Calling attach base context in your MultiDexApplication class

public class MyApplicationClass extends MultiDexApplication... {

    protected void attachBaseContext(Context base) {

This worked fine for me on pre and post lollipop devices.


The issue is occuring when using Instant Run and pre-lollipop devices!


you should add multiDexEnabled = true in to your defaultConfig in gradle file.

Leave a Reply

Your email address will not be published. Required fields are marked *