0

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
}
Thanasis1101
  • 1,614
  • 4
  • 17
  • 28
  • Have you instantiated Multidex Application ? – ADM Feb 28 '18 at 03:14
  • @ADM What exactly do you mean? What part of the code should I change? – Thanasis1101 Feb 28 '18 at 14:29
  • If multidex is the problem Then follow [This](https://stackoverflow.com/questions/26609734/how-to-enable-multidexing-with-the-new-android-multidex-support-library) . Get back if not worked. – ADM Feb 28 '18 at 14:31
  • I have tried all solutions suggested but nothing works. I get the same error. – Thanasis1101 Feb 28 '18 at 14:39
  • Are you sure you have `Multidex .installl()` in application class and added Application class to manifest ? And try Clean Project and the Rebuild Project. – ADM Feb 28 '18 at 14:41
  • The problem is I don't have an Application class. But I did add the `android:name="android.support.multidex.MultiDexApplication"` in my manifest file. I only have an Activity class. – Thanasis1101 Feb 28 '18 at 14:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/165977/discussion-between-thanasis-and-adm). – Thanasis1101 Feb 28 '18 at 14:46

2 Answers2

0

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

Akshay
  • 318
  • 1
  • 15
  • Thank you for replying. I replaced in my build.gradle file the part of the `android{}` with your code but I have the same problem. `Error:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'. > com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex` – Thanasis1101 Feb 28 '18 at 14:06
  • Please check this dependency, may be conflicting with each other implementation 'org.springframework.security.oauth:spring-security-oauth2:2.2.0.RELEASE' implementation 'org.springframework.android:spring-android-rest-template:1.0.1.RELEASE' – Akshay Mar 01 '18 at 08:57
  • Yes that is the problem but I dont know how to solve it – Thanasis1101 Mar 01 '18 at 11:49
0

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'
}
Thanasis1101
  • 1,614
  • 4
  • 17
  • 28