Android Gradle失败多个dex文件定义okhttp

时间:2017-07-14 20:57:28

标签: android gradle android-gradle build.gradle

我有以下依赖项:

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应该能够自动解决这个问题,所以我想知道这是什么问题。 我尝试通过从他们各自的第一类依赖项中排除okhttpokhttp-ws并单独编译它们来修复它,但这似乎没有帮助。

对于为何会出现此类错误的一些解释也值得赞赏。

1 个答案:

答案 0 :(得分:1)

正如@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'
}