我有以下依赖项:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-moshi:2.3.0'
compile('com.github.eoinsha:JavaPhoenixChannels:0.2') {
exclude module: 'groovy-all'
}
}
和下面的依赖树:
+--- 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.squareup.retrofit2:retrofit:2.3.0
| \--- com.squareup.okhttp3:okhttp:3.8.0
| \--- com.squareup.okio:okio:1.13.0
+--- com.squareup.retrofit2:converter-moshi:2.3.0
| +--- com.squareup.retrofit2:retrofit:2.3.0 (*)
| \--- com.squareup.moshi:moshi:1.4.0
| \--- com.squareup.okio:okio:1.11.0 -> 1.13.0
\--- com.github.eoinsha:JavaPhoenixChannels:0.2
+--- com.fasterxml.jackson.core:jackson-databind:2.8.3
| +--- com.fasterxml.jackson.core:jackson-annotations:2.8.0
| \--- com.fasterxml.jackson.core:jackson-core:2.8.3
\--- com.squareup.okhttp3:okhttp-ws:3.4.1
\--- com.squareup.okhttp3:okhttp:3.4.1 -> 3.8.0 (*)
我在尝试构建项目时遇到此错误:
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 Lokhttp3/internal/ws/WebSocketReader$FrameCallback;
这可能是由于类路径上存在两个不同版本的 okhttp 库造成的:
3.4.1 by
com.squareup.okhttp3:okhttp-ws
这是JavaPhoenixChannels
的依赖项
3.8.0 改造
Gradle 应该能够自动解决这个问题,所以我想知道问题是什么。
我试图通过从它们各自的一级依赖项中排除 okhttp
和 okhttp-ws
并分别编译它们来修复它,但这似乎没有帮助。
也欢迎解释为什么会弹出此类错误。
最佳答案
正如@Selvin 在评论中指出的那样,问题是由于 okhtt-ws
自 3.5 版以来已移入核心项目,因此指定了较新版本的okhttp
和旧版本的 okhttp-ws
将导致 okhttp-ws
库的两个不同副本最终出现在类路径中。对于 gradle,这些将是使用相同包层次结构的不同库,因此它不会自动排除一个。
TL;DR 通过排除 okhttp-ws
依赖项来修复错误,因为它已经在 okhttp
本身的 3.8 版中可用:
compile 'com.squareup.okhttp3:okhttp:3.8.0'
compile('com.github.eoinsha:JavaPhoenixChannels:0.2') {
exclude module: 'okhttp-ws'
}
关于Android Gradle 失败 多个 dex 文件定义 okhttp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45111497/