android - 多个 Dex : in Build Config Files 的问题

标签 android android-studio android-gradle-plugin

项目中有3个模块,A、B、C。 A 依赖于 B,B 依赖于 C。

错误输出:

Error:Execution failed for task ':app:dexDebug'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
    C:\Users\user\AppData\Local\Android\sdk\build-tools\21.1.2\dx.bat --dex --no-optimize --output C:\Testing\MyApplication\app\build\intermediates\dex\debug --input-list=C:\Testing\MyApplication\a\build\intermediates\tmp\dex\debug\inputList.txt
Error Code:
    2
Output:
    UNEXPECTED TOP-LEVEL EXCEPTION:
    com.android.dex.DexException: Multiple dex files define Lcom/test/test2/BuildConfig;
        at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596)
        at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554)
        at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535)
        at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)
        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)

主项目[settings.gradle]:

include ':A', ':B', ':C'

主项目[build.gradle]:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.0.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

A.gradle:

apply plugin: 'com.android.application'

dependencies {

    compile 'com.google.android.gms:play-services:6.5.87'
    compile 'com.android.support:support-v4:21.0.3'
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile project(':B')
}

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    /*defaultConfig {
        minSdkVersion 9
        targetSdkVersion 21
    }*/

    lintOptions {
        abortOnError false
    }
}

B.gradle:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile('de.keyboardsurfer.android.widget:crouton:1.8.5') {
        exclude group: 'com.android.support', module: 'support-v4'
    }
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'de.greenrobot:eventbus:2.2.1'
    compile 'com.mobsandgeeks:android-saripaar:1.0.3'
    compile 'com.loopj.android:android-async-http:1.4.6'
    compile project(':C')
    compile 'com.android.support:appcompat-v7:21.0.3'
}

C.gradle:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 21
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile files('libs/classes.jar')
}

如果我删除 A 和模块 B 中 appcpmpat-v7 的重复项,则会收到错误。

模块 C 包含已编译的 .class 文件。

我无法理解 build.config 文件的重复。有什么办法可以解决这个问题?

最佳答案

支持库被包含多次(可能是因为您正在使用的多个库将其声明为依赖项),请在构建脚本中查找它。

resolution remove appcompat library from Gradle A.


依赖解析

当一个项目引用两个都需要相同 jar 文件的库项目时,构建系统必须检测并解决重复问题。

完整的依赖系统会将每个 jar 文件与完全限定名称和版本号关联起来,以确定要使用的版本。

不幸的是,Android 构建系统还没有完整的依赖解析系统。与此同时,我们已经实现了一个遵循以下规则的非常基本的系统:

Jar 文件严格通过文件名来标识。

这意味着 mylib.jar 与 mylib-v2.jar 不同,并且两者都将被打包,如果它们实际上是不同版本中的相同库,则可能会导致“已添加”dx 错误。

对于具有相同文件名的 jar,“相同版本”意味着完全相同的文件。

目前我们的检测非常基础,仅检查文件的大小和 sha1 是否相同。 如果两个库各自在其 libs 文件夹中包含一个名为 mylib.jar 的文件,但这两个文件不同,则构建系统将失败,指示依赖错误。

检查Android Dex: UNEXPECTED TOP-LEVEL EXCEPTION了解更多信息。

关于android - 多个 Dex : in Build Config Files 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28825397/

相关文章:

android - 在 android 8.0+ 上不显示通知抽屉

android - 带有 api 16 和 Webview 的 MotionEvent (Android)

java - Android rtl 支持无需重新加载 Activity

java - 如何确定在 EditText 中选择了哪些文本?

android - 从服务器 : Proxy Authentication Required 收到状态代码 407

Android: 错误:(464) 属性 "dividerPadding"已经被定义

android - 在设计时在 android 中更改 textview 字体类型

android - ~/Library/Android/sdk/ndk-bundle 中的 NDK 没有 source.properties 文件

安卓 : RecyclerView row Visibility not working properly

android - 使用 gradle maven-publish 插件导出的 Kotlin Android 库模块不添加依赖项