REST Android Client using Spring and OAuth2 Error DexArchiveMergerException: Unable to merge dex

邮差的信 提交于 2019-12-11 14:48:32

问题


I have a server running and I want to connect an android client to the server with spring's oauth2. I use Android Studio. My problem is related to the gradle configurations, which I don't know how it should be.

Problem: When I run the app, I get this error:

Error:Execution failed for task':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.> com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

I have seen many other questions with the same problem, I tried the solutions (like adding multiDexEnabled true, clean and rebuild the project etc.) but none of them worked. I think that the problem is caused because these two dependencies:

implementation 'org.springframework.security.oauth:spring-security-oauth2:2.2.0.RELEASE'
implementation 'org.springframework.android:spring-android-rest-template:1.0.1.RELEASE'

might have some common jars or something. Any help would be appreciated.


My build.gradle (Module: app)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "readinghood.restclient"
        minSdkVersion 14
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/ASL2.0'
        exclude 'META-INF/spring.tooling'
        exclude 'META-INF/spring.handlers'
        exclude 'META-INF/spring.schemas'
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    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.fasterxml.jackson.core:jackson-databind:2.3.2'
    implementation 'org.springframework.security.oauth:spring-security-oauth2:2.2.0.RELEASE'
    implementation 'org.springframework.android:spring-android-rest-template:1.0.1.RELEASE'
}

My build.gradle (Project: RestClient)

buildscript {

    repositories {
        google()
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.10.RELEASE")
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()
        maven { url 'http://repo.spring.io/libs-release' }
    }
}

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

回答1:


android {

dexOptions {
    preDexLibraries = false
}
compileSdkVersion 27
buildToolsVersion '27.0.3'
packagingOptions
        {
            exclude 'AndroidManifest.xml'
            exclude 'META-INF/LICENSE'
            exclude 'META-IN F/NOTICE'
            exclude 'META-INF/DEPENDENCIES'
        }
defaultConfig {
    vectorDrawables.useSupportLibrary = true
    applicationId "com.support.project"
    minSdkVersion 14
    targetSdkVersion 27
    versionCode 1
    versionName "1.0"
    multiDexEnabled true
}
buildTypes {
    release {
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
productFlavors {
}

}

Try this it should work for your cause. Go through this link to understand why we should add this line of code multiDexEnabled true




回答2:


I finally solved the problem. There were two modules that were duplicate, so I had to exclude them from the Module build.gradle file. What I had to do was:

compile('org.springframework.android:spring-android-rest-template:2.0.0.M3') {
    exclude module: 'spring-android-core'
}

compile('org.springframework.security.oauth:spring-security-oauth2:2.3.0.RC1'){
    exclude module: 'spring-web'
}


来源:https://stackoverflow.com/questions/49020955/rest-android-client-using-spring-and-oauth2-error-dexarchivemergerexception-una

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!