【问题标题】:Unable to merge dex when a module is imported导入模块时无法合并dex
【发布时间】:2018-02-28 08:40:41
【问题描述】:

我知道对此有很多问题,但我尝试了其中大多数的解决方案,但都没有奏效。 我有一个应用程序,我在其中导入了一个模块。当我尝试运行应用程序时,我收到 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 中使用实现而不是编译。 任何帮助将不胜感激。 提前致谢。

【问题讨论】:

  • 从 gradle 选项卡发布 stacktrace 的 logcat,同时告诉我你在 app build.gradle 中添加的 commonutils 项目是什么

标签: android android-gradle-plugin kotlin kotlin-android-extensions


【解决方案1】:

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

问题是您不能拥有由 2 个不同库声明的完全相同的类或注解。如果它们是同一个库的不同版本,可能会选择版本最高的库,但如果它们是完全不同的库,则无法确定应该在 dex 文件中包含哪个实现。

第一个是:

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

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

第二个问题是:

'org.jetbrains:annotations-java5:15.0'

我想您导入它是为了获得org.jetbrains.annotations.Nullable,这已经在您的项目中可用,因为 Kotlin 标准库依赖于也声明了它的 org.jetbrains:annotations。于是就有了冲突。

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

【讨论】:

  • 谢谢,删除这个 'org.jetbrains:annotations-java5:15.0' 解决了我的问题。
【解决方案2】:

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

只是想帮忙。

【讨论】:

  • 我也尝试过启用 mulitdex,但没有帮助。
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 2018-02-26
    • 2018-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-05
    • 2018-04-28
    相关资源
    最近更新 更多