>我有以下依赖关系:
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
关系com.squareup.okhttp3:okhttp-ws
这是JavaPhoenixChannels
的依赖3.8.0 通过改造
Gradle 应该能够自动解决此问题,所以我想知道问题出在哪里。 我试图通过将okhttp
和okhttp-ws
从它们各自的第一类依赖项中排除并单独编译它们来修复它,但这似乎没有帮助。
关于为什么会弹出此类错误的一些解释也值得赞赏。
正如@Selvin在评论中指出的那样,该问题是由于自 3.5 版以来okhtt-ws
已移至核心项目内部,因此指定较新版本的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'
}