0

Error occurs only when I'm trying to take release or debug apk of my app. I can run or debug it on my device straight from Android Studio. From this question, I understood that this might be due to duplicate dependencies. So I tried to check whether there are any duplicate dependencies by running:

 ./gradlew -q :app:dependencies

I got the following dependency tree:

_releaseApk - ## Internal use, do not manually configure ##
+--- com.android.support:appcompat-v7:25.3.1
|    +--- com.android.support:support-annotations:25.3.1
|    +--- com.android.support:support-v4:25.3.1
|    |    +--- com.android.support:support-compat:25.3.1
|    |    |    \--- com.android.support:support-annotations:25.3.1
|    |    +--- com.android.support:support-media-compat:25.3.1
|    |    |    +--- com.android.support:support-annotations:25.3.1
|    |    |    \--- com.android.support:support-compat:25.3.1 (*)
|    |    +--- com.android.support:support-core-utils:25.3.1
|    |    |    +--- com.android.support:support-annotations:25.3.1
|    |    |    \--- com.android.support:support-compat:25.3.1 (*)
|    |    +--- com.android.support:support-core-ui:25.3.1
|    |    |    +--- com.android.support:support-annotations:25.3.1
|    |    |    \--- com.android.support:support-compat:25.3.1 (*)
|    |    \--- com.android.support:support-fragment:25.3.1
|    |         +--- com.android.support:support-compat:25.3.1 (*)
|    |         +--- com.android.support:support-media-compat:25.3.1 (*)
|    |         +--- com.android.support:support-core-ui:25.3.1 (*)
|    |         \--- com.android.support:support-core-utils:25.3.1 (*)
|    +--- com.android.support:support-vector-drawable:25.3.1
|    |    +--- com.android.support:support-annotations:25.3.1
|    |    \--- com.android.support:support-compat:25.3.1 (*)
|    \--- com.android.support:animated-vector-drawable:25.3.1
|         \--- com.android.support:support-vector-drawable:25.3.1 (*)
+--- com.android.support:cardview-v7:25.3.1
|    \--- com.android.support:support-annotations:25.3.1
+--- com.android.support:recyclerview-v7:25.3.1
|    +--- com.android.support:support-annotations:25.3.1
|    +--- com.android.support:support-compat:25.3.1 (*)
|    \--- com.android.support:support-core-ui:25.3.1 (*)
+--- com.android.support:design:25.3.1
|    +--- com.android.support:support-v4:25.3.1 (*)
|    +--- com.android.support:appcompat-v7:25.3.1 (*)
|    +--- com.android.support:recyclerview-v7:25.3.1 (*)
|    \--- com.android.support:transition:25.3.1
|         +--- com.android.support:support-annotations:25.3.1
|         \--- com.android.support:support-v4:25.3.1 (*)
+--- pl.droidsonroids.gif:android-gif-drawable:1.2.7
+--- com.google.android.gms:play-services-ads:10.2.6
|    +--- com.google.android.gms:play-services-ads-lite:10.2.6
|    |    \--- com.google.android.gms:play-services-basement:10.2.6
|    |         \--- com.android.support:support-v4:25.2.0 -> 25.3.1 (*)
|    +--- com.google.android.gms:play-services-base:10.2.6
|    |    +--- com.google.android.gms:play-services-basement:10.2.6 (*)
|    |    \--- com.google.android.gms:play-services-tasks:10.2.6
|    |         \--- com.google.android.gms:play-services-basement:10.2.6 (*)
|    +--- com.google.android.gms:play-services-clearcut:10.2.6
|    |    +--- com.google.android.gms:play-services-base:10.2.6 (*)
|    |    \--- com.google.android.gms:play-services-basement:10.2.6 (*)
|    +--- com.google.android.gms:play-services-basement:10.2.6 (*)
|    \--- com.google.android.gms:play-services-gass:10.2.6
|         \--- com.google.android.gms:play-services-basement:10.2.6 (*)
+--- com.google.android.gms:play-services-location:10.2.6
|    +--- com.google.android.gms:play-services-base:10.2.6 (*)
|    +--- com.google.android.gms:play-services-basement:10.2.6 (*)
|    \--- com.google.android.gms:play-services-tasks:10.2.6 (*)
\--- com.google.firebase:firebase-core:10.2.6
     \--- com.google.firebase:firebase-analytics:10.2.6
          +--- com.google.firebase:firebase-analytics-impl:10.2.6
          |    +--- com.google.android.gms:play-services-basement:10.2.6 (*)
          |    +--- com.google.firebase:firebase-iid:10.2.6
          |    |    +--- com.google.android.gms:play-services-basement:10.2.6 (*)
          |    |    \--- com.google.firebase:firebase-common:10.2.6
          |    |         +--- com.google.android.gms:play-services-basement:10.2.6 (*)
          |    |         \--- com.google.android.gms:play-services-tasks:10.2.6 (*)
          |    +--- com.google.firebase:firebase-common:10.2.6 (*)
          |    \--- com.google.android.gms:play-services-tasks:10.2.6 (*)
          +--- com.google.android.gms:play-services-basement:10.2.6 (*)
          \--- com.google.firebase:firebase-common:10.2.6 (*)

How to deduce which are the duplicate dependencies in my project from this tree? Are they the ones that end in asterisk?

Edit: My Gradle File is given below

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"
    defaultConfig {
        applicationId "com.mycompany.myapp"
        minSdkVersion 19
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:cardview-v7:25.3.1'
    compile 'com.android.support:recyclerview-v7:25.3.1'
    compile 'com.android.support:design:25.3.1'
    compile 'pl.droidsonroids.gif:android-gif-drawable:1.2.7'
    compile 'com.google.android.gms:play-services-ads:10.2.6'
    compile 'com.google.android.gms:play-services-location:10.2.6'
    compile 'com.google.firebase:firebase-core:10.2.6'
    testCompile 'junit:junit:4.12'
}


apply plugin: 'com.google.gms.google-services'  

Edit 2: Full gradle error:

Error:Execution failed for task ':app:transformClassesWithDexForDebug'.

com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexException: Multiple dex files define Landroid/support/v7/util/ThreadUtil$MainThreadCallback;

Edit 3: Since support/v7/util/ThreadUtil.java belongs to RecyclerView module, I tried to exclude the RecyclerView module under design library with the following:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:cardview-v7:25.3.1'
    compile 'com.android.support:recyclerview-v7:25.3.1'
    compile ('com.android.support:design:25.3.1') {
        exclude module: 'recyclerview-v7'
    }
    compile 'pl.droidsonroids.gif:android-gif-drawable:1.2.7'
    compile 'com.google.android.gms:play-services-ads:10.2.6'
    compile 'com.google.android.gms:play-services-location:10.2.6'
    compile 'com.google.firebase:firebase-core:10.2.6'
    testCompile 'junit:junit:4.12'
  }

Still doesn't work.

Community
  • 1
  • 1
DroidHeaven
  • 2,414
  • 3
  • 25
  • 31
  • post gradle file – jagapathi May 20 '17 at 10:32
  • try adding full gradle error – jagapathi May 20 '17 at 10:44
  • multiDexEnabled true inside default config – jagapathi May 20 '17 at 10:56
  • @jagapathi I tried that earlier. Didn't worked. – DroidHeaven May 20 '17 at 11:01
  • 1
    If I paste that list of dependencies into a brand-new Android Studio project (and get rid of the `ConstraintLayout` from the `activity_main` layout resource), I can build and run the project without a duplicate-dependency error. If you have not done so already, try cleaning & rebuilding the project. If that does not help, try copying your project directory to a separate location, delete all `build/`, `*.iml`, `.idea/` and `.gradle/` directories from the copy, import that copy, and do a build. If that does not help, I have to assume that you have the duplicate `ThreadUtil` in `src/` somewhere. – CommonsWare May 20 '17 at 13:01
  • @CommonsWare Thanks for the reply. That last line helped to solve the issue. I was using an ad-mediation network who provided their SDK in a zip file. This zip file contained a couple of jar files to be placed in the lib folder. Most of the jar files belonged to third party ad-networks but one of them was our recyclerview. I removed it and the issue was solved. Thanks once again for coming here and replying. – DroidHeaven May 20 '17 at 13:53

1 Answers1

1

Per your comment, apparently the issue was a third-party SDK that distributed a JAR of recyclerview-v7, that you had in libs/, which conflicted with your recyclerview-v7 dependency.

There are downsides to using plain JARs for dependencies.

One is that you tend to forget about them, particularly if you also are using dependencies from artifact repositories. For example, I didn't even consider that you might have JARs in libs/ in my comment.

Another is that Gradle cannot perform any sort of dependency resolution for them. Gradle relies on artifact metadata (POM files) for determining what dependencies are required. Gradle assumes that "you're doing the right thing" with respect to JAR files, and in this case, your ad network's one JAR isn't "the right thing".

Also, JARs are JARs, not AARs. In this case, the engineers at your ad network are very strange people. recyclerview-v7 has resources in its AAR that RecyclerView needs (e.g., custom attributes). Basically, by distributing a JAR containing RecyclerView code, they are dooming anyone using their SDK: nobody can use their RecyclerView because it is incomplete (and probably out of date), and nobody can use the official RecyclerView due to class conflicts.

At minimum, I recommend that you complain to the ad network, urging them to switch to using artifacts and a proper set of dependencies.

Community
  • 1
  • 1
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks for the informative and detailed answer. I've notified them about this issue and will certainly bring their attention to the information you've shared here. (FYI, the ad-network's name is [Appodeal](https://www.appodeal.com/) and [this](https://s3-us-west-1.amazonaws.com/appodeal-android/Appodeal-Android-SDK-160217-1.15.9.zip) is their SDK zip file). – DroidHeaven May 20 '17 at 15:13