反应本机:应用程序:checkDebugDuplicateClasses 失败

React Native :app:checkDebugDuplicateClasses FAILED

我正在尝试通过直接采用 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 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。但它只显示缓存文件夹中的一些文件,其中包含麻烦的 Class、okhttpws 模块,我无法删除这些文件。对于其他 类,它只显示来自 okhttp 模块的文件,我也无法删除。有解决方法吗?我错过了什么?

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

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

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

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