Multiple dex files define Landroid/support/v7/util/ThreadUtil$MainThreadCallback Error

独自空忆成欢 提交于 2019-12-31 05:40:09

问题


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.


回答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.



来源:https://stackoverflow.com/questions/44084767/multiple-dex-files-define-landroid-support-v7-util-threadutilmainthreadcallback

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