我正在通过gradle使用以下设置:

compileSdkVersion 21
ANDROID_BUILD_MIN_SDK_VERSION=14
ANDROID_BUILD_TARGET_SDK_VERSION=21
ANDROID_BUILD_TOOLS_VERSION=21.0.2
ANDROID_BUILD_SDK_VERSION=21


我的gradle文件中也有以下设置:

compile 'com.android.support:support-annotations:21.0.0'
compile 'com.android.support:appcompat-v7:21.0.0'
compile 'com.android.support:support-v4:21.0.0'


我总是收到错误UNEXPECTED TOP LEVEL EXCEPTION
但是,当我将21.0.0设置为20.0.0时,它可以正常工作...但是我无法访问任何Android API 21选项。我在这里做错什么了吗?如何在没有此异常的情况下进行编译?除了其他年级项目(facebook等)之外,我在其他任何地方都没有支撑罐。

这是完整的堆栈跟踪:

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:283)
    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:302)
    at com.android.dx.command.dexer.Main.run(Main.java:245)
    at com.android.dx.command.dexer.Main.main(Main.java:214)
    at com.android.dx.command.Main.main(Main.java:106)

最佳答案

此消息听起来像您的项目太大。

您有太多方法。 dex只能有65536种方法。

由于gradle插件0.14.0和Build Tools 21.1.0,您可以使用multidex support

只需将这些行添加到build.gradle中:

android {

    defaultConfig {
        ...

        // Enabling multidex support.
        multiDexEnabled true
    }
    ...
}

dependencies {
  compile 'com.android.support:multidex:1.0.1'
}


同样在您的Manifest中,将multidex支持库中的MultiDexApplication类添加到application元素中

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.multidex.myapplication">
    <application
        ...
        android:name="android.support.multidex.MultiDexApplication">
        ...
    </application>
</manifest>


如果使用自己的Application类,请将父类从Application更改为MultiDexApplication

07-24 09:37
查看更多