MultiDex issue - Execution failed for task ':a

2019-02-28 06:45发布

I face a problem while adding a line for Google Calendar API to build.gradle. Here is the error:

UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]:65536
    at com.android.dx.merge.DexMerger$6.updateIndex(DexMerger.java:502)
at com.android.dx.merge.DexMerger$IdMerger.mergeSorted(DexMerger.java:277)
at com.android.dx.merge.DexMerger.mergeMethodIds(DexMerger.java:491)
at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:168)
at com.android.dx.merge.DexMerger.merge(DexMerger.java:189)
at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:303)
at com.android.dx.command.dexer.Main.run(Main.java:246)
at com.android.dx.command.dexer.Main.main(Main.java:215)
at com.android.dx.command.Main.main(Main.java:106)
Error:Execution failed for task ':app:dexDebug'.
> com.android.ide.common.process.ProcessException: 
 org.gradle.process.internal.ExecException: Process 'command 
'/usr/java/jdk1.7.0_79/bin/java''

finished with non-zero exit value 2
Information:BUILD FAILED
Information:Total time: 14.802 secs
Information:1 error
Information:2 warnings
Information:See complete output in console

I have tried many options but nothing is working. When I add "multiDex true" to defaultConfig, appcompat library doesn't connect to my project, and application crashes with java.lang.NoClassDefFoundError: android.support.v7.appcompat.R$layout.

Here's my build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

defaultConfig {
    applicationId "ua.appp.appp"
    minSdkVersion 14
    targetSdkVersion 21
    versionCode 15
    versionName "2.1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
   }
 }

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

    compile 'com.google.android.gms:play-services:6.5.87'
    compile 'com.google.apis:google-api-services-calendar:v3-rev119-1.19.1'
    compile 'com.google.api-client:google-api-client:1.19.1'
    compile 'com.google.api-client:google-api-client-android:1.19.1'
    compile 'com.google.api-client:google-api-client-gson:1.19.1'

    compile 'com.github.navasmdc:MaterialDesign:1.+@aar'
    compile 'com.afollestad:material-dialogs:0.6.5.2'
    compile 'com.nhaarman.listviewanimations:lib-core:3.1.0@aar'
    compile 'com.nhaarman.listviewanimations:lib-manipulation:3.1.0@aar'
    compile 'com.nhaarman.listviewanimations:lib-core-slh:3.1.0@aar'
}

PS: Some interesting fact: When I add all these libraries to a test project, code compiles and works fine without an error.

1条回答
做个烂人
2楼-- · 2019-02-28 07:18

When you add the multidex:true don't forget to make an Application class in your project that extends from the MultiDexApplication or a lot of bad stuff will happen

public class YourApplication extends MultiDexApplication {
    ..........
}

And don't forget this either (the version may change over time, if you read this, make sure you use the latest version)

dependencies {
    'com.android.support:multidex:1.0.1'
}
查看更多
登录 后发表回答