多个 dex 文件定义 Landroid/support/v7/util/ThreadUtil$MainThreadCallback 错误

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

仅当我尝试发布或调试我的应用程序的 apk 时才会发生错误。我可以 运行 或直接从 Android Studio 在我的设备上调试它。从 this 问题,我了解到这可能是由于重复的依赖关系。所以我尝试通过 运行ning:

检查是否有重复的依赖
 ./gradlew -q :app:dependencies

我得到了以下依赖关系树:

_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 (*)

如何从这棵树中推断出我的项目中哪些是重复的依赖项?他们是以星号结尾的吗?

编辑:我的Gradle文件如下

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'  

编辑 2:完整 gradle 错误:

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;

编辑 3:由于 support/v7/util/ThreadUtil.java 属于 RecyclerView 模块,我尝试使用以下代码排除设计库下的 RecyclerView 模块:

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

还是不行。

根据您的评论,显然问题是第三方 SDK 分发了您在 libs/ 中的 recyclerview-v7 的 JAR,这与您的 recyclerview-v7 依赖项冲突。

使用纯 JAR 作为依赖项有缺点。

一个是您往往会忘记它们,特别是如果您正在使用来自工件存储库的依赖项。例如,我什至没有考虑到 libs/ 中可能有 JAR。

另一个是 Gradle 无法为它们执行任何类型的依赖项解析。 Gradle 依赖工件元数据(POM 文件)来确定需要哪些依赖项。 Gradle 假设 "you're doing the right thing" 关于 JAR 文件,在这种情况下,您的广告网络的一个 JAR 不是 "the right thing"。

另外,JAR 是 JAR,不是 AAR。在这种情况下,您广告网络的工程师是非常奇怪的人。 recyclerview-v7 在其 AAR 中有 RecyclerView 需要的资源(例如,自定义属性)。基本上,通过分发包含 RecyclerView 代码的 JAR,他们注定了任何使用他们的 SDK 的人:没有人可以使用他们的 RecyclerView 因为它不完整(并且可能已过时),并且没有人可以使用官方 RecyclerView 由于 class 冲突。

至少,我建议您向广告网络投诉,敦促他们改用工件和一组适当的依赖项。