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

标签 android gradle dex

仅当我尝试发布或调试我的应用程序的 apk 时才会发生错误。我可以直接从 Android Studio 在我的设备上运行或调试它。来自 this问题,我知道这可能是由于重复的依赖关系。所以我尝试通过运行检查是否有任何重复的依赖项:

 ./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 分发了 recyclerview-v7 的 JAR。 , 你在 libs/ ,与您的 recyclerview-v7 冲突依赖。

使用纯 JAR 进行依赖有缺点。

一是您倾向于忘记它们,特别是如果您还使用来自工件存储库的依赖项。例如,我什至没有考虑到 libs/ 中可能有 JAR。在 my comment .

另一个是 Gradle 无法为它们执行任何类型的依赖解析。 Gradle 依赖工件元数据(POM 文件)来确定需要哪些依赖项。对于 JAR 文件,Gradle 假定“您正在做正确的事情”,在这种情况下,您的广告网络的一个 JAR 不是“正确的事情”。

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

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

关于android - 多个 dex 文件定义 Landroid/support/v7/util/ThreadUtil$MainThreadCallback 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44084767/

相关文章:

android - 64K 方法计数超出错误,接近 62.5K 方法引用计数

android - 覆盖操作栏中的向上按钮

android - Android-如何从restartPackage()保存服务?

android - 为什么onSurfaceView onTouchEvent()返回true?

java - Gradle构建错误:任务 ':app:prepareDebugDependencies'的执行失败

java - 由于无法找到 Android 启动器类,无法在 Android 设备上运行 LibGDX 游戏

android - 如何以编程方式更改 Android 中连接的 BLE 设备名称?

gradle - 如何在Groovy项目中使用Gradle属性

android - Gradle离线同步问题

android - eclipse ,安卓 : Unexpected Unable to execute dex: Multiple dex files define