使用 Hermes Engine
有关在 Expo 项目中为 Android 和 iOS 配置 Hermes 的指南。
Hermes 是一个为 React Native 优化的 JavaScript 引擎。通过提前将 JavaScript 编译为字节码,Hermes 可以提高应用的启动速度。Hermes 的二进制文件大小也比其他 JavaScript 引擎例如 JavaScriptCore (JSC) 更小。它在运行时还使用更少的内存,这在低端 Android 设备上尤其有价值。
支持
🌐 Support
Hermes 引擎是 Expo 使用的默认 JavaScript 引擎,所有 Expo 工具都完全支持它。
🌐 The Hermes engine is the default JavaScript engine used by Expo and it is fully supported across all Expo tooling.
在特定平台上切换 JavaScript 引擎
🌐 Switch JavaScript engine on a specific platform
你可能希望在一个平台上使用 Hermes,而在另一个平台上使用 JSC。一种方法是在应用配置的顶层将 "jsEngine" 设置为 "hermes",然后在 "ios" 键下用 "jsc" 覆盖它。或者在这种情况下,你可能更倾向于仅在 "android" 键上显式设置 "hermes"。
🌐 You may want to use Hermes on one platform and JSC on another. One way to do this is to set the "jsEngine" to "hermes" at the top level in app config and then override it with "jsc" under the "ios" key. You may alternatively prefer to explicitly set "hermes" on just the "android" key in this case.
{ "expo": { "jsEngine": "hermes", "ios": { "jsEngine": "jsc" } } }
发布更新
🌐 Publish updates
使用 eas update 和 npx expo export 发布更新将生成 Hermes 字节码包及其源映射。
🌐 Publishing updates with eas update and npx expo export will generate Hermes bytecode bundles and their source maps.
请注意,Hermes 字节码格式可能会在不同的 Hermes 版本之间发生变化 —— 为特定版本的 Hermes 生成的更新不能在不同版本的 Hermes 上运行。从 Expo SDK 46(React Native 0.69)开始,Hermes 已包含在 React Native 中。更新 React Native 版本或 Hermes 版本可以与更新其他原生模块的方式一样考虑。因此,如果你更新了 react-native 版本,也应该在 app.json 中更新 runtimeVersion。如果不进行此操作,你的应用可能会在启动时崩溃,因为更新可能会被使用旧 Hermes 版本的现有二进制加载,而旧版本与更新后的字节码格式不兼容。更多信息请参见 runtimeVersion。
🌐 Note that the Hermes bytecode format may change between different Hermes versions — an update produced for a specific version of Hermes will not run on a different version of Hermes. Starting from Expo SDK 46 (React Native 0.69), Hermes is bundled within React Native. Updating React Native version or Hermes version can be thought of in the same way as updating any other native module. So if you update the react-native version you should also update the runtimeVersion in app.json. If you don't do this, your app may crash on launch because the update may be loaded by an existing binary that uses an older Hermes version that is incompatible with the updated bytecode format. See runtimeVersion for more information.
JavaScript 调试器
🌐 JavaScript debugger
要调试使用 Hermes 运行的 JavaScript 代码,你可以使用 npx expo start 启动项目,然后按 j 在 Google Chrome 或 Microsoft Edge 中打开调试器。开发版本和 Expo Go 的开发者菜单中也有 打开开发者工具(原 打开 JS 调试器)选项来执行相同操作。
或者,你可以通过手动打开 Google Chrome 开发者工具 来使用 JavaScript 检查器
🌐 Alternatively, you can use the JavaScript inspector by opening Google Chrome DevTools manually
故障排除
🌐 Troubleshooting
警告 在打开调试器时出现
No compatible apps connected. JavaScript Debugging can only be used with the Hermes engine.。
-
确保你在
jsEngine字段中设置 Hermes。 -
如果你的应用是由
eas build、npx expo run:android或npx expo run:ios构建的,请确保它是调试版本。 -
在内部,应用将建立 WebSocket 连接,确保你的应用已连接到开发服务器。
- 尝试在 Expo CLI 终端界面中按 r 重新加载应用。
- 通过运行命令
curl http://127.0.0.1:8081/json/list测试调试可用性(将127.0.0.1:8081调整为匹配你的开发服务器 URL)。HTTP 响应应该是一个数组,如下所示。如果响应为空,请在npx expo start命令中添加--localhost或--tunnel标志。
[ { "id": "0-2", "description": "host.exp.Exponent", "title": "Hermes ABI47_0_0React Native", "faviconUrl": "https://react.nodejs.cn/favicon.ico", "devtoolsFrontendUrl": "devtools://devtools/bundled/js_app.html?experiments=true&v8only=true&ws=%5B%3A%3A1%5D%3A8081%2Finspector%2Fdebug%3Fdevice%3D0%26page%3D2", "type": "node", "webSocketDebuggerUrl": "ws://[::1]:8081/inspector/debug?device=0&page=2", "vm": "Hermes" }, { "id": "0--1", "description": "host.exp.Exponent", "title": "React Native Experimental (Improved Chrome Reloads)", "faviconUrl": "https://react.nodejs.cn/favicon.ico", "devtoolsFrontendUrl": "devtools://devtools/bundled/js_app.html?experiments=true&v8only=true&ws=%5B%3A%3A1%5D%3A8081%2Finspector%2Fdebug%3Fdevice%3D0%26page%3D-1", "type": "node", "webSocketDebuggerUrl": "ws://[::1]:8081/inspector/debug?device=0&page=-1", "vm": "don't use" } ]
我可以使用 Hermes 进行远程调试吗?
🌐 Can I use Remote Debugging with Hermes?
远程调试 的众多限制之一是它无法与基于 JSI 构建的模块一起使用,例如 react-native-reanimated 2 版或更高版本。
🌐 One of the many limitations of remote debugging is that it does not work with modules built on top of JSI, such as react-native-reanimated version 2 or higher.
Hermes 支持 Chrome 开发者工具协议,可以通过连接到设备上运行的引擎本地调试 JavaScript,而不是进行远程调试,远程调试是在桌面 Chrome 标签页中执行 JavaScript。Hermes 应用在你在 Expo Go 或开发版中打开调试器时会自动使用这种调试技术。
🌐 Hermes supports Chrome DevTools Protocol to debug JavaScript in place by connecting to the engine running on the device, as opposed to remote debugging, which executes JavaScript within a desktop Chrome tab. Hermes apps use this debugging technique automatically when you open the debugger in Expo Go or a development build.