导入模块时无法合并 dex

Unable to merge dex when a module is imported

我知道这方面有很多问题,但我尝试了其中大部分的解决方案,但都没有奏效。 我有一个应用程序,我在其中导入了一个模块。当我尝试 运行 应用程序时,出现 Unable to merge dex 异常。

build.gradle(应用模块)

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 27
defaultConfig {
    applicationId "com.meditab.imspatient"
    minSdkVersion 21
    targetSdkVersion 27
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'

implementation 'com.android.support.constraint:constraint-layout:1.0.2' // Constraint layout
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" // Kotlin

// Support libraries
implementation "com.android.support:appcompat-v7:$appcompat_version"
implementation "com.android.support:support-v4:$appcompat_version"

compile project(path: ':commonutils')
 }

build.gradel(导入模块)

 apply plugin: 'com.android.library'

android {
compileSdkVersion 27
buildToolsVersion '27.0.2'

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

packagingOptions {
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/NOTICE.txt'
}
}

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

compile 'org.jetbrains:annotations-java5:15.0'
compile 'com.jakewharton:butterknife:8.8.1'

// Support libraries
compile "com.android.support:appcompat-v7:$appcompat_version"
compile "com.android.support:design:$appcompat_version"

// Rx related libraries
compile 'io.reactivex:rxjava:1.1.1'
compile 'io.reactivex:rxandroid:1.1.0'
//compile 'com.netflix.rxjava:rxjava-core:0.+'
// Networking related libraries
compile 'com.squareup.okhttp3:logging-interceptor:3.8.1'
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-jackson:2.0.2'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.2'
compile 'com.fasterxml.jackson.core:jackson-databind:2.7.2'

compile 'com.google.firebase:firebase-core:11.8.0'
compile 'com.google.firebase:firebase-messaging:11.8.0'
compile 'com.google.android.gms:play-services-base:11.8.0'
compile 'com.google.android.gms:play-services-location:11.8.0'
compile 'com.estimote:sdk:0.10.+@aar'
}

build.gradle(项目级别)

buildscript {
ext.kotlin_version = '1.2.21'
ext.appcompat_version = '27.0.2'
repositories {
    google()
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:3.0.1'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

    // Needed for the common utils module
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}

allprojects {
repositories {
    google()
    jcenter()
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}

我试过删除 .gradle 文件夹,清理项目并重建它。什么都不管用。我还尝试在导入模块的 build.gradle 中使用实现而不是编译。 任何帮助将不胜感激。 提前致谢。

我不是 100% 确定,但我认为您需要在您的应用中启用 multidex。检查此 link :Enable Multidex

只是想帮忙。

在我之前有多个模块的项目中,如果我启用 Proguard,我会遇到同样的错误。 这个错误令人沮丧,就我而言,我在检查后解决了问题 项目依赖关系图(Using gradle to find dependency tree 可以帮助你找到依赖关系) 播放服务的不同版本导致了我的情况

您声明依赖的 2 个库似乎导致了重复条目。

问题是您不能让 2 个不同的库声明完全相同的 class 或注解。如果它们是同一库的不同版本,可能会选择版本最高的那个,但如果它们是完全不同的库,则无法判断 dex 文件中应包含哪个实现。

第一个是:

'com.netflix.rxjava:rxjava-core:0.+'

你为什么要用这个?您已经定义了对 RxJava ('io.reactivex:rxjava:1.1.1') 的依赖,它们可能有许多共同点 class,我第一个想到并检查过的是 rx.Observable。该回购的最后一次更新是 2014 年 11 月,也就是 3 年多以前。除了冲突之外,仅此一个事实就应该是不使用它的一个很好的理由。

第二个问题是:

'org.jetbrains:annotations-java5:15.0'

我想您导入它是为了获得 org.jetbrains.annotations.Nullable,它已经在您的项目中可用,因为 Kotlin stdlib 对 org.jetbrains:annotations 也有依赖性,它也声明了它。因此冲突。

删除这 2 个依赖项,您很可能实际上并不需要它们,并且项目会编译。