【发布时间】:2020-12-04 07:58:48
【问题描述】:
我正在尝试通过直接采用来自this gist 的代码来为 React Native 项目中的 websocket 连接实现Trustmanager。它需要okhttp-ws 模块才能运行。
但是,它在:app:checkDebugDuplicateClasses 失败
Execution failed for task ':app:checkDebugDuplicateClasses'.
> 1 exception was raised by workers:
java.lang.RuntimeException: java.lang.RuntimeException:
Duplicate class okhttp3.internal.ws.RealWebSocket found in modules okhttp-3.14.1.jar (com.squareup.okhttp3:okhttp:3.14.1) and okhttp-ws-3.4.2.jar (com.squareup.okhttp3:okhttp-ws:3.4.2)
Duplicate class okhttp3.internal.ws.RealWebSocket$1 found in modules okhttp-3.14.1.jar (com.squareup.okhttp3:okhttp:3.14.1) and okhttp-ws-3.4.2.jar (com.squareup.okhttp3:okhttp-ws:3.4.2)
Duplicate class okhttp3.internal.ws.WebSocketProtocol found in modules okhttp-3.14.1.jar (com.squareup.okhttp3:okhttp:3.14.1) and okhttp-ws-3.4.2.jar (com.squareup.okhttp3:okhttp-ws:3.4.2)
Duplicate class okhttp3.internal.ws.WebSocketReader found in modules okhttp-3.14.1.jar (com.squareup.okhttp3:okhttp:3.14.1) and okhttp-ws-3.4.2.jar (com.squareup.okhttp3:okhttp-ws:3.4.2)
Duplicate class okhttp3.internal.ws.WebSocketReader$FrameCallback found in modules okhttp-3.14.1.jar (com.squareup.okhttp3:okhttp:3.14.1) and okhttp-ws-3.4.2.jar (com.squareup.okhttp3:okhttp-ws:3.4.2)
Duplicate class okhttp3.internal.ws.WebSocketWriter found in modules okhttp-3.14.1.jar (com.squareup.okhttp3:okhttp:3.14.1) and okhttp-ws-3.4.2.jar (com.squareup.okhttp3:okhttp-ws:3.4.2)
Duplicate class okhttp3.internal.ws.WebSocketWriter$FrameSink found in modules okhttp-3.14.1.jar (com.squareup.okhttp3:okhttp:3.14.1) and okhttp-ws-3.4.2.jar (com.squareup.okhttp3:okhttp-ws:3.4.2)
所以,我从app/build.gradle 中排除了okhttp
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "com.facebook.react:react-native:+" // From node_modules
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"
// compile the ws module
implementation('com.squareup.okhttp3:okhttp-ws:+'){
// exclude the conflicting module
exclude group:'com.squareup.okhttp3', module:'okhttp'
}
debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}") {
exclude group:'com.facebook.fbjni'
}
debugImplementation("com.facebook.flipper:flipper-network-plugin:${FLIPPER_VERSION}") {
exclude group:'com.facebook.flipper'
// interestingly, it's been excluded here too
exclude group:'com.squareup.okhttp3', module:'okhttp'
}
debugImplementation("com.facebook.flipper:flipper-fresco-plugin:${FLIPPER_VERSION}") {
exclude group:'com.facebook.flipper'
}
if (enableHermes) {
def hermesPath = "../../node_modules/hermes-engine/android/";
debugImplementation files(hermesPath + "hermes-debug.aar")
releaseImplementation files(hermesPath + "hermes-release.aar")
} else {
implementation jscFlavor
}
}
仍然,我得到了同样的错误。因此,我访问了错误显示的开发人员文档link,并尝试在 Android Studio 中搜索 Class okhttp3.internal.ws.RealWebSocket。但它只显示缓存文件夹中包含麻烦类的一些文件,包括okhttp 和ws 模块,我无法删除这些文件。对于其他类,它只显示来自okhttp 模块的文件,我也无法删除这些文件。有解决方法吗?我错过了什么?
【问题讨论】:
-
我建议您在解决这个问题时使用精确版本而不是 +。您不能混合版本,因此应该全面使用一致的 okhttp 版本。 3.14.1 不再积极维护,因此请考虑 3.12.12(旧版 java impl)或 4.9.0(最新支持)。
标签: android gradle build.gradle okhttp