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(...) 依赖
}
相关推荐
程序员老刘43 分钟前
Flutter你看看人家RN
flutter·react native·ai编程
Java后端的Ai之路7 小时前
07React_Props验证器
人工智能·python·react.js·架构·fastapi
Aolith8 小时前
从收藏到错题本,我在 React 全栈项目里实现了一套复习系统
mongodb·react.js·全栈
是你的小橘呀8 小时前
告别各写各的!从 0 到 1 打造 React Native 客户端公共动效基础设施 🏗️
react native
墨狂之逸才10 小时前
# React Native iOS 排查记录:从 Yoga 编译报错,到模拟器反复白屏
react native
一木 之林10 小时前
Agent智能体学习总结:从一次调用到模型自主规划、工具调用与 ReAct 循环的项目实战-6/6
前端·人工智能·学习·react.js
牧瀬クリスだ10 小时前
Ajax入门:从原生XHR到axios实战
java·spring boot·ajax·okhttp
song5011 天前
React Native for OpenHarmony 实战:三方库 react-native-parallax-scroll-view 的鸿蒙化适配指南
分布式·react native·react.js·transformer·harmonyos
song5011 天前
React Native for OpenHarmony 实战:三方库 react-native-ratings 的鸿蒙化适配指南
javascript·react native·react.js·分类·harmonyos
晓得迷路了1 天前
栗子前端技术周刊第 149 期 - Vite+ 1.0、Preact 11、Next.js 16.4...
前端·javascript·react.js