Search code examples
javaandroidspringspring-security-oauth2resttemplate

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


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
}

Solution

  • 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'
    }