【问题标题】:React Native :app:checkDebugDuplicateClasses FAILEDReact Native :app:checkDebugDuplicateClasses 失败
【发布时间】: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。但它只显示缓存文件夹中包含麻烦类的一些文件,包括okhttpws 模块,我无法删除这些文件。对于其他类,它只显示来自okhttp 模块的文件,我也无法删除这些文件。有解决方法吗?我错过了什么?

【问题讨论】:

  • 我建议您在解决这个问题时使用精确版本而不是 +。您不能混合版本,因此应该全面使用一致的 okhttp 版本。 3.14.1 不再积极维护,因此请考虑 3.12.12(旧版 java impl)或 4.9.0(最新支持)。

标签: android gradle build.gradle okhttp


【解决方案1】:

WebSocket 现在是核心库的一部分。 https://square.github.io/okhttp/4.x/okhttp/okhttp3/-web-socket/

所以你应该升级到 4.9.0 并停止依赖 okhttp-ws,或者恢复到 3.12.12(这是受支持的旧分支)。

这里有一个示例,说明如何在 OkHttp https://github.com/square/okhttp/blob/master/samples/slack/src/main/java/okhttp3/slack/SlackApi.java 中使用 WebSocket 支持

或者使用内置的 react-native 支持 https://reactnative.dev/docs/network

【讨论】:

  • 谢谢。我已经解决了。但是,您能否指出一些要点或具有为 websockets 实现的 Android TrustManager[] 的工作代码的东西,最好是轻松适应和使用 React Native?
  • 添加到我上面的答案中,您可以根据您的要求调整上面显示的 API 调用。但是由于 react-native 支持 websocket,你可能根本不需要这个。
  • 但是 React Native 本身并不支持基于证书的 SSL/TLS websocket 连接,对吧?我的意思是,wss 连接?
猜你喜欢
  • 1970-01-01
  • 2019-10-31
  • 2020-06-17
  • 1970-01-01
  • 1970-01-01
  • 2020-09-08
  • 2021-07-09
  • 2018-01-07
  • 1970-01-01
相关资源
最近更新 更多