You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native中inappbrowser-reborn Android真机崩溃问题求助

解决React Native中inappbrowser-reborn导致的Android真机崩溃问题

问题分析

从Android Studio日志的核心错误信息:

x6.e: Subscriber class h5.c and its super classes have no public methods with the @Subscribe annotation

可以确定崩溃是由EventBus订阅者异常引发的,这是inappbrowser-reborn库内部依赖的EventBus框架出现版本不兼容或缓存冲突导致的。

解决方案

1. 匹配库与React Native版本

不同RN版本对第三方库兼容性要求不同,确保inappbrowser-reborn版本适配当前RN环境:

  • React Native 0.70+:使用^4.0.0及以上版本
  • React Native 0.60-0.69:使用^3.6.0版本

执行命令更新/降级库:

# 适配RN 0.70+
npm install react-native-inappbrowser-reborn@latest --save
# 适配低版本RN
npm install react-native-inappbrowser-reborn@3.6.0 --save

2. 清理构建缓存

缓存残留是常见的依赖冲突诱因,执行以下步骤清理:

# 删除node_modules并清除npm缓存
rm -rf node_modules && npm cache clean --force
# 清理Android构建缓存
cd android && ./gradlew clean && cd ..
# 重新安装依赖
npm install

3. 解决EventBus版本冲突(可选)

如果项目中其他库也依赖EventBus,可能出现版本冲突,在android/app/build.gradle中添加排除规则:

dependencies {
  // 排除inappbrowser-reborn自带的EventBus,使用统一版本
  implementation ('com.proyecto26:inappbrowser:4.0.0') {
    exclude group: 'org.greenrobot', module: 'eventbus'
  }
  // 指定统一的EventBus版本
  implementation 'org.greenrobot:eventbus:3.3.1'
}

4. 补全代码逻辑

确保WebView的拦截逻辑完整,补充白名单URL的加载允许逻辑:

onShouldStartLoadWithRequest={(event) => {
  const { url } = event;
  if (!url.startsWith('https://portal.bsu')) {
    console.log('Blocked navigation to URL:', url);
    InAppBrowser.open(url, {
      toolbarColor: '#03a9f3',
      readerMode: false,
      animated: true,
      modalPresentationStyle: 'overFullScreen',
      modalTransitionStyle: 'coverVertical',
      modalEnabled: true,
      enableBarCollapsing: false,
      // Android配置
      showTitle: true,
      enableUrlBarHiding: true,
      enableDefaultShare: false,
      forceCloseOnRedirection: false,
      // iOS配置
      usewkwebview: true
    })
    .then((result) => {
      console.log('InAppBrowser opened successfully:', result);
    })
    .catch((error) => {
      console.error('Error opening InAppBrowser:', error.message);
    });
    return false; // 拦截非白名单URL
  }
  return true; // 允许白名单URL在WebView中加载
}

验证修复

重新构建并运行应用:

npm run android

触发跳转链接后,检查应用是否正常打开InAppBrowser,无崩溃现象。

内容的提问来源于stack exchange,提问作者jaba

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 22:30:12