更新到最新的appcompat和支持库后出现DexIndexOverflowException问题

KVI*_*ISH 73 android gradle android-support-library build.gradle android-5.0-lollipop

我通过以下设置使用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
Run Code Online (Sandbox Code Playgroud)

我的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'
Run Code Online (Sandbox Code Playgroud)

我总是得到错误UNEXPECTED TOP LEVEL EXCEPTION.
但是,当我做21.0.020.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)
Run Code Online (Sandbox Code Playgroud)

Gab*_*tti 148

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

你有太多的方法.dex只能有65536种方法.

从gradle插件0.14.0和Build Tools 21.1.0开始,您可以使用multidex支持.

只需在以下位置添加以下行build.gradle:

android {

    defaultConfig {
        ...

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

dependencies {
  compile 'com.android.support:multidex:1.0.1'
}
Run Code Online (Sandbox Code Playgroud)

此外,您还可以ManifestMultiDexApplicationmultidex支持库中的类添加到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>
Run Code Online (Sandbox Code Playgroud)

如果您使用自己的Application类,请将父类更改ApplicationMultiDexApplication.

  • 将multiDexEnabled true添加到build.gradle文件的defaultConfig (6认同)

geo*_*ore 45

1)为应用程序build.gradle文件添加依赖项

compile 'com.android.support:multidex:1.0.2'
Run Code Online (Sandbox Code Playgroud)

2)如果不覆盖Application类,请编辑清单文件以在标记中设置android:name,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp">
    <application
            android:name="android.support.multidex.MultiDexApplication" >
        ...
    </application>
</manifest>
Run Code Online (Sandbox Code Playgroud)

如果您覆盖Application类,请将其更改为扩展MultiDexApplication(如果可能),如下所示:

public class MyApplication extends MultiDexApplication { ... }
Run Code Online (Sandbox Code Playgroud)

或者,如果您覆盖Application类但无法更改基类,则可以改为覆盖attachBaseContext()方法并调用MultiDex.install(this)以启用multidex:

public class MyApplication extends SomeOtherApplication {
  @Override
  protected void attachBaseContext(Context base) {
     super.attachBaseContext(base);
     MultiDex.install(this);
  }
}
Run Code Online (Sandbox Code Playgroud)

3)在build.gradle中向defaultConfig添加multidex支持

defaultConfig {
        ...
        minSdkVersion 16
        targetSdkVersion 26
        ...
        // Enabling multidex support.
        multiDexEnabled true
    }
...
Run Code Online (Sandbox Code Playgroud)