react native 出现 FATAL EXCEPTION: OkHttp Dispatcher

react native 出现 FATAL EXCEPTION: OkHttp Dispatcher

报错信息

复制代码
 FATAL EXCEPTION: OkHttp Dispatcher
  Process: , PID: 8868
   java.lang.NoSuchMethodError: No virtual method toString(Z)Ljava/lang/String; in class Lokhttp3/Cookie; or its 
   super classes (declaration of 'okhttp3.Cookie' appears in /data/app/~~jJHvA-
   QIVyqleQgWxDDo2A==/com.bestwond.bigapp-BKzRyAI_ofmJB2eguJfYAA==/base.apk)

错误原因

复制代码
你的项目中至少有两个不同的库分别依赖了不同大版本的 OkHttp:
1.React Native 本身:新版本的 RN (>= 0.63) 通常依赖 OkHttp 4.x。
2.某个第三方库:你安装的某个第三方库(如 react-native-webview, @react-native-community/netinfo, 或其他网络相关的库)依赖了旧的 OkHttp 3.x。
编译时一切正常,但运行时手机只加载了一个版本的 OkHttp(通常是旧版本),导致 RN 网络模块找不到它需要的新版本中的方法(这里是 Cookie.toString(Z) 方法),从而引发 NoSuchMethodError 致命异常。

解决方案

解决方案:强制统一 OkHttp 版本

你需要在你项目的 android/app/build.gradle 文件中,强制所有依赖都使用同一个版本的 OkHttp。

请按照以下步骤操作:

1.打开文件:找到你 React Native 项目中的 android/app/build.gradle 文件。

2.添加配置:在 dependencies 块中,在最前面添加以下强制依赖解析策略:

复制代码
dependencies {
    // !!! 强制统一 OkHttp 版本,解决 FATAL EXCEPTION !!! 
    implementation(platform("com.squareup.okhttp3:okhttp-bom:4.10.0")) // 使用BOM统一管理版本
    implementation("com.squareup.okhttp3:okhttp")
    implementation("com.squareup.okhttp3:okhttp-urlconnection")
    implementation("com.squareup.okhttp3:logging-interceptor")
    // !!! 结束 !!!

    // ... 你原有的其他依赖项
    implementation("com.facebook.react:react-android")
    implementation("org.webkit:android-jsc:+")
    // ... 其他 project(...) 依赖
}
相关推荐
znsoft10 小时前
MaClaw(码卡龙)技术拆解:一个 Go + React 的可自进化智能体平台
开发语言·react.js·golang
濮水大叔14 小时前
React/Next.js 生态里,有没有 CabloyJS 这样的全栈框架?
react.js·全栈·next.js
前端小D15 小时前
React Native Android 项目搭建
android·react native·react.js
Highcharts.js2 天前
Highcharts 集成到 React 或 Vue 项目中时,如何高效地使用 TypeScript技巧?
vue.js·人工智能·react.js·typescript·前端框架·highcharts
hhhjhl3 天前
React Native 三方库「react-native-sha256」的鸿蒙化适配指南
react native·华为·harmonyos
2601_949970693 天前
React Native 三方库「react-native-version-check」的鸿蒙化适配指南
react native·华为·harmonyos
hhhjhl3 天前
React Native 三方库「react-native-proximity」的鸿蒙化适配指南
react native·华为·harmonyos
2601_949970693 天前
React Native 三方库「react-native-media-notification」的鸿蒙化适配指南
react native·华为·harmonyos
liuchangng3 天前
React 19 + Vite 6 实战:从教材 PDF 到 866 词间隔重复单词 App
前端·react.js·pdf·vite·开源项目
GitCode官方3 天前
从大语言模型到具身智能:百度百舸 LoongForge 入驻 AtomGit
人工智能·react native·百度·harmonyos·atomgit