Expo Go安卓端引入react-native-webrtc报错,求助解决方法
问题:Expo Go安卓端引入react-native-webrtc后报错
当引入以下react-native-webrtc相关代码时:
import { RTCPeerConnection, RTCIceCandidate, RTCSessionDescription, RTCView, MediaStream, MediaStreamTrack, mediaDevices, registerGlobals } from 'react-native-webrtc';
安卓设备上的Expo Go出现两个错误:
ERROR TypeError: Cannot read property 'senderGetCapabilities' of null, js engine: hermesERROR Invariant Violation: 'main' has not been registered,提示可能是Metro运行目录错误或模块加载失败导致AppRegistry.registerComponent未调用。
项目依赖版本:
"dependencies": { "@config-plugins/react-native-webrtc": "^7.0.0", "@expo-google-fonts/dev": "^0.2.3", "@expo-google-fonts/heebo": "^0.2.3", "@expo-google-fonts/inter": "^0.2.3", "@expo-google-fonts/kanit": "^0.2.3", "@expo-google-fonts/merriweather-sans": "^0.2.3", "@expo-google-fonts/open-sans": "^0.2.3", "@expo-google-fonts/patua-one": "^0.2.3", "@expo-google-fonts/roboto": "^0.2.3", "@expo-google-fonts/roboto-slab": "^0.2.3", "@expo-google-fonts/rubik": "^0.2.3", "@expo/webpack-config": "^19.0.0", "@react-navigation/native": "^6.1.7", "@react-navigation/native-stack": "^6.9.13", "expo": "~49.0.8", "expo-camera": "^13.4.3", "expo-dev-client": "~2.4.8", "expo-splash-screen": "^0.20.5", "expo-status-bar": "~1.6.0", "react": "18.2.0", "react-dom": "18.2.0", "react-native": "0.72.4", "react-native-dotenv": "^3.4.9", "react-native-vector-icons": "^10.0.0", "react-native-web": "~0.19.6", "react-native-webrtc": "^111.0.3", "socket.io-client": "^4.7.2", "validator": "^13.11.0" }
平台信息:React Native 0.72.4,WebRTC模块^111.0.3,开发系统Windows 10,Expo Go 2.29.6,Chrome 116.0.5845.141。
不引入react-native-webrtc时项目运行正常,需解决该问题以实现WebRTC功能。
解决方案
改用Expo Dev Client而非Expo Go:react-native-webrtc属于原生模块,Expo Go不支持未预编译的原生依赖。你已安装
expo-dev-client,执行以下步骤:- 运行
expo prebuild生成原生项目文件 - 用
expo run:android编译并安装自定义开发客户端到安卓设备 - 通过自定义客户端运行项目,而非Expo Go
- 运行
检查config-plugin配置:确保
app.json或app.config.js中已添加@config-plugins/react-native-webrtc插件,示例配置:{ "expo": { "plugins": ["@config-plugins/react-native-webrtc"] } }插件会自动配置权限、原生依赖等,无需手动修改安卓Manifest文件。
移除
registerGlobals调用:registerGlobals会向全局注入WebRTC对象,在Expo环境中容易引发冲突,删除导入和调用该方法的代码,直接使用导入的模块即可。清理缓存:
- 清除Metro缓存:运行
npx react-native start --reset-cache - 清除Expo缓存:运行
expo r -c - 卸载设备上的Expo Go或自定义客户端,重新安装
- 清除Metro缓存:运行
验证依赖完整性:运行
npm install或yarn install重新安装所有依赖,检查是否有安装报错。同时确认Metro未修改默认运行目录,确保项目根目录正确。
内容的提问来源于stack exchange,提问作者Aryan Nayak
相关产品推荐
相关产品推荐

