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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26515378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:39:48  来源:igfitidea点击:

DexIndexOverflowException issue after updating to latest appcompat and support library

androidgradleandroid-5.0-lollipopandroid-support-librarybuild.gradle

提问by KVISH

I'm using the below settings through gradle:

我正在通过以下设置使用以下设置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

I also have the following settings in my gradle file:

我的 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'

I always get the error UNEXPECTED TOP LEVEL EXCEPTION.
But when I make the 21.0.0to 20.0.0it works fine...but i'm not able to access any of the Android API 21 options. Is there something i'm doing wrong here? How do I get it to compile without this exception? I do not have the support jars anywhere else outside of other grade projects (facebook etc.).

我总是得到错误UNEXPECTED TOP LEVEL EXCEPTION
但是,当我做21.0.020.0.0它工作正常...但我不能访问任何的Android API 21个选项。我在这里做错了什么吗?如何在没有此异常的情况下编译它?我在其他年级项目(facebook 等)之外的任何其他地方都没有支持 jars。

Here is the full stack trace:

这是完整的堆栈跟踪:

UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536
    at com.android.dx.merge.DexMerger.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)

回答by Gabriele Mariotti

This message sounds like your project is too large.

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

You have too many methods. There can only be 65536 methods for dex.

你的方法太多了。dex只能有65536 个方法

Since the gradle plugin 0.14.0 and the Build Tools 21.1.0 you can use the multidex support.

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

Just add these lines in the build.gradle:

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

android {

    defaultConfig {
        ...

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

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

Also in your Manifestadd the MultiDexApplicationclass from the multidex support library to the application element

同样在您ManifestMultiDexApplicationmultidex 支持库中的类添加到应用程序元素中

<?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>

If you are using a own Applicationclass, change the parent class from Applicationto MultiDexApplication.

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

回答by georgehardcore

1) add dependency to application build.gradle file

1) 添加对应用程序 build.gradle 文件的依赖

compile 'com.android.support:multidex:1.0.2'

2) If you do not override the Application class, edit your manifest file to set android:name in the tag as follows:

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>

If you do override the Application class, change it to extend MultiDexApplication (if possible) as follows:

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

public class MyApplication extends MultiDexApplication { ... }

Or if you do override the Application class but it's not possible to change the base class, then you can instead override the attachBaseContext() method and call MultiDex.install(this) to enable multidex:

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

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

3) Add multidex support to defaultConfig at build.gradle

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

defaultConfig {
        ...
        minSdkVersion 16
        targetSdkVersion 26
        ...
        // Enabling multidex support.
        multiDexEnabled true
    }
...

回答by Andrei

Before messing around with MultiDex

在使用 MultiDex 之前

It either solution is too big (use MultiDex) or you compile and include in your project some not needed stuff. I got this issue on my tiny project. Here is what I did to fix it:

它要么解决方案太大(使用 MultiDex),要么你编译并在你的项目中包含一些不需要的东西。我在我的小项目中遇到了这个问题。这是我为修复它所做的:

I removed this from build.gradle:

我从build.gradle

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

And I manually removed my build folder from application root. Problem fixed.

我从应用程序根目录手动删除了我的构建文件夹。问题已解决。

回答by I?aqui

Follow thisguide to workaround 65K limit.

按照指南解决 65K 限制。

I solved this issue in gradle by setting multiDexEnabled trueand extending my application with class MyApp extends MultiDexApplication

我通过设置multiDexEnabled true和扩展我的应用程序在 gradle 中解决了这个问题class MyApp extends MultiDexApplication

回答by Earl Allen

I simply commented out the compile filetree(include: ['*.jar'], dir: 'libs')in the build.gradlefile (as suggested above) and rebuilt the project. Compiled without any errors. I did not need to move any folders.

我只是filetree(include: ['*.jar'], dir: 'libs')build.gradle文件中的编译注释掉(如上所述)并重建项目。编译没有任何错误。我不需要移动任何文件夹。

回答by atomsymbol

First make sure that minification is enabled in app/build.cradle:

首先确保在app/build.cradle以下位置启用了缩小:

buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        signingConfig signingConfigs.config
    }
    debug {
        signingConfig signingConfigs.config
    }
}

If this does not solve the issue, enable multidex buildfor your app.

如果这不能解决问题,为您的应用启用multidex 构建

回答by Gaurav Gupta

I faced this issue when i had compiled the entire package of Google Play Services APIs into my app. I solved the issue by using selective API.

当我将整个 Google Play 服务 API 包编译到我的应用程序中时,我遇到了这个问题。我通过使用选择性 API 解决了这个问题。

For example, to include only the Google Fit and Android Wear APIs, replace the following line in your build.gradle file:

例如,要仅包含 Google Fit 和 Android Wear API,请替换 build.gradle 文件中的以下行:

compile 'com.google.android.gms:play-services:11.0.2'

with these lines:

用这些行:

compile 'com.google.android.gms:play-services-fitness:11.0.2'
compile 'com.google.android.gms:play-services-wearable:11.0.2'

Issue is explained in following link.

问题在以下链接中进行了解释。