Build Tools 21.1.2 – UNEXPECTED TOP-LEVEL EXCEPTION

6.3k Views Asked by At

I'm suddenly running into this issue when building/running my project.

Error:Execution failed for task ':app:dexDebug'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
    /Users/aidanfollestad/Documents/android-sdk/build-tools/21.1.2/dx --dex --no-optimize --output /Users/aidanfollestad/Android Projects/Impression/app/build/intermediates/dex/debug --input-list=/Users/aidanfollestad/Android Projects/Impression/app/build/intermediates/tmp/dex/debug/inputList.txt
  Error Code:
    2
  Output:
    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)

My Gradle file contains this:

buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        classpath 'io.fabric.tools:gradle:1.14.+'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
apply plugin: 'versionPlugin'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.afollestad.impression"
        minSdkVersion 16
        targetSdkVersion 21
        versionCode 19
        versionName "0.7.0"
    }
}

repositories {
    mavenCentral()
    maven { url 'https://maven.fabric.io/public' }
}

dependencies {
    compile 'com.android.support:appcompat-v7:21.0.+'
    compile 'com.android.support:recyclerview-v7:21.0.+'
    compile 'com.koushikdutta.ion:ion:2.0.+'
    compile 'com.github.chrisbanes.photoview:library:1.2.+'
    compile 'com.afollestad:material-dialogs:0.4.5'
    compile 'com.google.android.gms:play-services:6.5.87'

    compile 'com.google.api-client:google-api-client:1.18.0-rc'
    compile 'com.google.api-client:google-api-client-android:1.18.0-rc'
    compile 'com.google.api-client:google-api-client-gson:1.18.0-rc'
    compile 'com.google.apis:google-api-services-drive:v2-rev152-1.19.0'

    compile('com.crashlytics.sdk.android:crashlytics:2.1.0@aar') {
        transitive = true;
    }
}

versionPlugin{
    buildTypesMatcher = 'release'
    supportBuildNumber = false
    fileNameFormat = '$appPkg-v$versionName-$versionCode'
}

My material-dialogs library references only AppCompat-v7, the same version this Gradle file is referencing. I don't have any JARs in my libs folder that I'm referencing. I have no idea what libraries are interfering with each other (other than the possibility of Play Services and AppCompat?). Any ideas or solutions?

I noticed Ion references v4 of the support library (https://github.com/koush/ion/blob/master/ion/build.gradle#L17), maybe that could be interfering with AppCompat?

3

There are 3 best solutions below

3
On BEST ANSWER

Try to enable multidex build.gradle :

android {
   defaultConfig {
      ...
      multiDexEnabled = true
   }
}

Ref : Unable to execute dex: method ID not in [0, 0xffff]: 65536

0
On

This exception shows that your project have reached to maximum number of methods 65536. you just do the below work.It worked for me

Step 1:
inside build.gradle

      defaultConfig {  
       multiDexEnabled = true 
      }

Step 2:

inside manifest application tag

  android:name="android.support.multidex.MultiDexApplication" 

   or

Step 3: if You use Class which extends Application then do:

 public class MyApplication extends Application { 
   @Override 
   protected void attachBaseContext(Context base) { 
    super.attachBaseContext(base); 
    MultiDex.install(this); 
    } 
    }

Step 4:

inside build.gradle

  android{
    dexOptions { 
    incremental true 
    javaMaxHeapSize "4g" 
    }
   }
1
On

You may hitted the 65k methods limit. Instead of using multiDex, try to use Google Play Services with more granularity. Follow this guide, you can use only parts that you want. Probably this will fix your problem.