com.android.dex.DexException: 多个 dex 文件定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21735493/
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 04:59:12  来源:igfitidea点击:

com.android.dex.DexException: Multiple dex files define

androidandroid-studioandroid-gradle-plugin

提问by user3302639

I am using Android Studio 0.4.2. Opened project from a friend who is using 0.3.2. Tried to compile but got exception.

我正在使用 Android Studio 0.4.2。使用 0.3.2 的朋友打开的项目。试图编译但得到异常。

Execution failed for task ':JuiceTV:dexDebug'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
    C:\Program Files\Android\android-studio\sdk\build-tools.0.0\dx.bat --dex --output D:\Antik TV - Android\JuiceTV\build\libs\JuiceTV-debug.dex D:\Antik TV - Android\JuiceTV\build\classes\debug D:\Antik TV - Android\JuiceTV\build\dependency-cache\debug D:\Antik TV - Android\JuiceTV\build\pre-dexed\debug\classes-ffe9228b675e120536184b1056a59fcfc91e4006.jar D:\Antik TV - Android\JuiceTV\build\pre-dexed\debug\commons-io-2.4-27f1277ba9e42db4b52f3f658da01a26db29b896.jar D:\Antik TV - Android\JuiceTV\build\pre-dexed\debug\joda-time-2.2-4549e2440d188ee3fb4f85702e03eace13e8ad18.jar D:\Antik TV - Android\JuiceTV\build\pre-dexed\debug\mmlib-04a4fd100008bfbc84f0c25fd219e50eb7de9d0b.jar D:\Antik TV - Android\JuiceTV\build\pre-dexed\debug\support-v4-18.0.0-ba816fc3ae00ee0fdb20e5444c1d8bb88647d773.jar
Error Code:
    2
Output:
    UNEXPECTED TOP-LEVEL EXCEPTION:
    com.android.dex.DexException: Multiple dex files define Lcom/sevensoft/mmlib/AttachedOverlayWindow;
        at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:593)
        at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:551)
        at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:532)
        at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:169)
        at com.android.dx.merge.DexMerger.merge(DexMerger.java:187)
        at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:439)
        at com.android.dx.command.dexer.Main.runMonoDex(Main.java:287)
        at com.android.dx.command.dexer.Main.run(Main.java:230)
        at com.android.dx.command.dexer.Main.main(Main.java:199)
        at com.android.dx.command.Main.main(Main.java:103)  

Tried this things:

试过这样的事情:

  • deleting *.apk files
  • searching for dependencies with gradle -q dependenciesbut got nothing
  • 删除 *.apk 文件
  • 使用gradle -q 依赖项搜索依赖项但一无所获

JuiceTV Gradle:

JuiceTV Gradle:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.7.+'
    }
}
apply plugin: 'android'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 18
    buildToolsVersion "19.0.0"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 18
    }
}

dependencies {
    compile project(':TVbase')
}

BaseTv Gradle:

BaseTv Gradle:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.7.+'
    }
}
apply plugin: 'android-library'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 18
    buildToolsVersion '19.0.0'

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 18
    }
}

dependencies {
    compile 'com.android.support:support-v4:18.0.+'
    compile files('libs/joda-time-2.2.jar')
    compile files('libs/mmlib.jar')
    compile files('libs/commons-io-2.4.jar')
}

Any new tips?

有什么新提示吗?

回答by user01000101

I just had the same issue and I discovered that my application and a library were referring to 2 versions of the same jar.

我刚刚遇到了同样的问题,我发现我的应用程序和库指的是同一个 jar 的 2 个版本。

I did a search and my application.iml file clearly showed the duplicates.

我进行了搜索,我的 application.iml 文件清楚地显示了重复项。

<orderEntry type="library" exported="" name="crittercism_v3_0_11_sdkonly" level="project" />
<orderEntry type="library" exported="" name="crittercism_v4_4_0" level="project" />

I replaced the older v3 version with the v4 version and it worked after a clean rebuild.

我用 v4 版本替换了旧的 v3 版本,它在干净重建后工作。

回答by pzulw

This happens when a Module has a dependency on both another module and that same module's jar.

当一个模块同时依赖另一个模块和同一个模块的 jar 时,就会发生这种情况。

回答by karma

I had this issue on android studio 1.0, along with removing the duplicate entry for libraries you should also try deleting the bin folders and do a clean build.

我在 android studio 1.0 上遇到了这个问题,除了删除库的重复条目外,您还应该尝试删除 bin 文件夹并进行干净的构建。

回答by PaulR

As of Android Gradle 1.0 this can happen a few ways:

从 Android Gradle 1.0 开始,这可以通过以下几种方式发生:

  1. If you only use Maven repos you can run into this issue if two different artifacts include the same class file.
    The Mockito-alllibrary, for instance, includes Hamcrest and you would therefore cause this error if you included both dependencies in a build.
    Note, if you have minification turned on you won't see the error until you start using Hamcrest. The way to resolve this is to use a version of the library that isn't including other package's classes (Mockito-core in this case).

  2. If you declare a local jar/aardependency (compile 'com.some.library:some-artiface-version_number@aar') that is already being included by other maven-based dependencies.
    In this situation your local declaration doesn't get de-duped with the Maven one because the pom information isn't available. You would typically create a local, file-based maven repo for the aar to get around this.

  1. 如果您只使用 Maven 存储库,如果两个不同的工件包含相同的类文件,您可能会遇到此问题。
    的Mockito,所有的库,例如,包括Hamcrest因此您会导致这个错误,如果你提供的构建都依赖关系。
    请注意,如果您打开了缩小功能,则在开始使用 Hamcrest 之前不会看到错误。解决此问题的方法是使用不包含其他包类(在本例中为 Mockito-core)的库版本。

  2. 如果您声明了一个本地 jar/aar依赖项 ( compile 'com.some.library:some-artiface-version_number@aar'),该依赖项已经被其他基于 maven 的依赖项包含在内
    在这种情况下,您的本地声明不会被 Maven 删除,因为 pom 信息不可用。您通常会为 aar 创建一个本地的、基于文件的 maven 存储库来解决这个问题。

In either case, a project clean is required to pickup the changes.

在任何一种情况下,都需要一个项目清理来获取更改。

回答by Ammar

Having multiDex disabled and incremental in dexOptions enabled will also cause this issue.

禁用 multiDex 并启用 dexOptions 中的增量也会导致此问题。

defaultConfig {
    multiDexEnabled = false
 }
dexOptions {
    javaMaxHeapSize "4g"
    incremental true
}