sadelbrid sadelbrid - 3 months ago 64
Android Question

"Method references in a .dex file cannot exceed 64K" after changing build.gradle

I'm getting that error that says I have too many method references:

Error:The number of method references in a .dex file cannot exceed 64K.


It all started when I tried to change my targetSdkVersion to 24 and minSdkVersion to a lower sdk (19) via the gradle app file. That led me to having to change some values throughout the file like

compile 'com.android.support:appcompat-v7:23.0.0'


to

compile 'com.android.support:appcompat-v7:24.1.1'


I've read that people get this error when they have the wrong values in build.gradle. Here was my original build.gradle before the changes:

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"

defaultConfig {
applicationId "com.delbridge.seth.alarm"
minSdkVersion 23
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
debug {
debuggable true
}
}
}


Any thoughts as to what's causing this?

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'

compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.mcxiaoke.volley:library-aar:1.0.0'
compile 'org.jsoup:jsoup:1.9.2'
compile 'org.jdeferred:jdeferred-android-aar:1.2.4'
compile 'com.google.android.gms:play-services-appindexing:9.0.2'
compile 'com.google.android.gms:play-services:9.0.2'
compile 'com.android.support:support-v4:23.0.0'
compile 'com.android.support:design:23.0.0'
compile 'com.android.support:cardview-v7:23.0.+'
compile 'com.android.support:recyclerview-v7:23.0.+'
compile 'com.google.firebase:firebase-ads:9.0.2'

}

apply plugin: 'com.google.gms.google-services'


And here is my build.gradle currently:

apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion "23.0.3"

defaultConfig {
applicationId "com.delbridge.seth.alarm"
minSdkVersion 19
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
debuggable true
}
}
}



dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.1.1'

compile 'com.mcxiaoke.volley:library-aar:1.0.0'
compile 'org.jsoup:jsoup:1.9.2'
compile 'org.jdeferred:jdeferred-android-aar:1.2.4'
compile 'com.google.android.gms:play-services-appindexing:9.4.0'
compile 'com.google.android.gms:play-services:9.4.0'

compile 'com.google.firebase:firebase-ads:9.4.0'
compile 'com.google.android.gms:play-services-ads:9.4.0'
compile 'com.google.android.gms:play-services-auth:9.4.0'
compile 'com.google.android.gms:play-services-gcm:9.4.0'

compile 'com.android.support:support-v4:24.1.1'
compile 'com.android.support:design:24.1.1'
compile 'com.android.support:cardview-v7:24.1.1'
compile 'com.android.support:recyclerview-v7:24.1.1'


}

apply plugin: 'com.google.gms.google-services'

Answer

You're unnecessarily including all of Google Play Services with this line:

compile 'com.google.android.gms:play-services:9.4.0'

So, remove that line and leave the ones that include the individual components:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.1.1'

    compile 'com.mcxiaoke.volley:library-aar:1.0.0'
    compile 'org.jsoup:jsoup:1.9.2'
    compile 'org.jdeferred:jdeferred-android-aar:1.2.4'
    compile 'com.google.android.gms:play-services-appindexing:9.4.0'

    //remove this line:
    //compile 'com.google.android.gms:play-services:9.4.0'

    compile 'com.google.firebase:firebase-ads:9.4.0'
    compile 'com.google.android.gms:play-services-ads:9.4.0'
    compile 'com.google.android.gms:play-services-auth:9.4.0'
    compile 'com.google.android.gms:play-services-gcm:9.4.0'

    compile 'com.android.support:support-v4:24.1.1'
    compile 'com.android.support:design:24.1.1'
    compile 'com.android.support:cardview-v7:24.1.1'
    compile 'com.android.support:recyclerview-v7:24.1.1'
}

Note:

If the above solution does not solve your issue, then you will need to enable multidex:

Enabling Multidex