1

I know there are many questions on this, but I tried the solutions of most of them and neither of them worked. I have an app in which I have imported a module. When I try to run the app, I am getting Unable to merge dex exception.

build.gradle (app module)

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 (imported module)

 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 (project level)

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
}

I have tried deleting the .gradle folder, cleaning the project and rebuilding it. Nothing works. I also tried using implementation instead of compile in the build.gradle of the imported module. Any help will be appreciated. Thanks in advance.

  • post the logcat of stacktrace from gradle tabs, also tell me what is commonutils project that you are adding in app build.gradle – Umar Ata Mar 05 '18 at 08:07

3 Answers3

0

I'm not 100 percent sure but I think you need to enable multidex in your app. Check this link : Enable Multidex

Just trying to help.

Vicky
  • 1,807
  • 5
  • 23
  • 34
0

In my previous project which has multiple modules If I enable Proguard I was getting the same error. That error frustrating and In my case I solved problem after inspecting the project dependency graph(Using gradle to find dependency tree may help you while finding dependencies) Play service's different version was causing in my case

gokhan
  • 627
  • 1
  • 6
  • 16
0

It seems that 2 libraries you're declaring a dependency on are causing duplicate entries.

The problem is that you can't have the exact same class or annotation declared by 2 different libraries. If they were different versions of the same library probably the one with the highest version would be picked, but if they are completely different libraries, there is no way to tell which implementation should be included in the dex file.

The first one is:

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

Why would you use this? You have already defined a dependency on RxJava ('io.reactivex:rxjava:1.1.1') and they probably have many classes in common, the first that comes to my mind and that I checked is rx.Observable. The last update for that repo is Novermber 2014, more than 3 years ago. Besides conflicts, this fact alone should be a very good reason not to use it.

The second problem is:

'org.jetbrains:annotations-java5:15.0'

I imagine that you're importing this in order to get org.jetbrains.annotations.Nullable, this is already available in your project because Kotlin stdlib has a dependncy on org.jetbrains:annotations that also declares it. Hence the conflict.

Remove these 2 dependencies, you most likely don't actually need them anyway, and the project compiles.

lelloman
  • 13,883
  • 5
  • 63
  • 85
  • Thanks, removing this 'org.jetbrains:annotations-java5:15.0' solved my problem. –  Mar 06 '18 at 07:19