libevent 2.1.13 补丁(RNOH 源码依赖模式)

背景

二进制成分扫描要求 libevent >= 2.1.13,但官方预编译 @rnoh/react-native-openharmony 0.72.143 release har(react_native_openharmony_release.har)内嵌的是 2.1.12-stable,库这一层无依赖声明可升, 只能切到 RNOH 源码依赖模式自己编译,编译前把源码里的 libevent 换成官方 2.1.13-stable。

用什么、不用什么

  • 用:官方 @react-native-oh/react-native-harmony@0.72.143 npm 包内自带的 源码版 har (react_native_openharmony.har,非 _release)——npm install 就能拿到,不需要额外提交任何大文件。
  • 不用:不要提交"已经打好补丁、编译好的" RNOH 源码 har(那份东西完整源码树在 140MB 左右, 远超 GitCode 单文件 100MiB 限制)。本仓 ohos/vendor/libevent-2.1.13-src/ 里只放 libevent 2.1.13-stable 的上游源码本身(约 3.4MB),配合 patch-libevent-2113.sh 在装完依赖后现场替换。

用法

cd ohos/example/harmony   # 或 ohos/example_auto/harmony
ohpm install --all
node ../../scripts/patch-libevent-2113.cjs .
node ../../scripts/patch-libevent-2113.cjs . --check
hvigorw --mode module -p module=entry@default -p product=default -p buildMode=release assembleHap

Git Bash 下也可继续使用:

bash ../../scripts/patch-libevent-2113.sh .

example/harmony/entry/oh-package.json5 与 example/harmony/oh-package.json5(example_auto 同构)需要:

  1. @rnoh/react-native-openharmony 指向源码版 har (file:../../node_modules/@react-native-oh/react-native-harmony/react_native_openharmony.har, entry 下多一层 ../),不是 _release.har;
  2. entry 的 dependencies 里显式加 "@ppd/ffrt": "1.1.11"(源码构建需要 hvigor 为 entry 生成 ffrt::ffrt_cpp 导入目标,只有模块直接依赖才会进 summary.cmake,传递依赖不够);
  3. entry/src/main/cpp/CMakeLists.txt 切源码依赖模式:RNOH_CPP_DIR 指向 oh_modules/@rnoh/react-native-openharmony/src/main/cpp,删除预编译 rnoh_semi 链接段, 改成 add_subdirectory("${RNOH_CPP_DIR}" ./rn);
  4. entry/src/main/cpp/CMakeLists.txt 引入 ohos/scripts/verify-libevent-2113.cmake。 DevEco/CMake 配置阶段会执行只读 --check,漏跑补丁或 ohpm install 后补丁被还原时直接失败。

产物差异:HAP 内不再有 librnoh_semi.so,取而代之的是源码编出的 librnoh.so,内嵌 libevent 2.1.13-stable,全包无 2.1.12 残留(已用 python3 -c "import zipfile,re;..." 逐 .so 扫描验证,命中 0)。

已知坑

  • 源码模式路径深度容易超 Windows 260 上限,必须在短路径下构建(如 D:\rn\<slug>)。
  • hvigor 首次为新工程哈希装依赖常报 pnpm.cmd install execute failed (No stdout):进 ~/.hvigor/project_caches/<新哈希>/workspace 手动跑一次 pnpm install,重跑 hvigor 即可。
  • ohpm install --all 对 140MB 级 file: har 依赖偶尔解包不稳定(装成空目录),装完建议校验 oh_modules/@rnoh/react-native-openharmony/src/main/cpp/CMakeLists.txt 是否存在。