构建工具21.1.2 - 意外的顶级异常

时间:2014-12-16 04:30:56

标签: android gradle android-studio build-tools

在构建/运行我的项目时,我突然遇到了这个问题。

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)

我的Gradle文件包含:

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'
}

我的材质对话框库仅引用AppCompat-v7,与此Gradle文件引用的版本相同。我正在引用的libs文件夹中没有任何JAR。我不知道哪些库互相干扰(除了Play Services和AppCompat的可能性?)。任何想法或解决方案?

我注意到Ion引用了支持库的v4(https://github.com/koush/ion/blob/master/ion/build.gradle#L17),也许这可能会干扰AppCompat?

3 个答案:

答案 0 :(得分:12)

尝试启用multidex build.gradle:

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

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

答案 1 :(得分:11)

你可以达到65k方法限制。不要使用multiDex,而是尝试更精细地使用Google Play服务。 Follow this guide,您只能使用自己想要的部分。这可能会解决你的问题。

答案 2 :(得分:2)

此异常显示您的项目已达到最大方法数65536.您只需执行以下工作。它对我有用

步骤1:
      在build.gradle内部

      defaultConfig {  
       multiDexEnabled = true 
      }

第2步:

在清单申请标签

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

   or

步骤3:如果您使用扩展应用程序的类,则执行:

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

第4步:

在build.gradle内部

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