配置 JS 引擎
有关在 Expo 项目中为 Android 和 iOS 配置 JS 引擎的指南。
JavaScript 引擎执行你的应用代码,并提供诸如内存管理、优化和错误处理等各种功能。默认情况下,Expo 项目使用 Hermes 作为 JavaScript 引擎。可以将其切换到其他引擎,例如 Android 和 iOS 平台的 JSC 或 V8。但对于 web 平台则不行,因为 JavaScript 引擎包含在 web 浏览器中。
🌐 JavaScript engines execute your application code and provide various features such as memory management, optimization, and error handling. By default, Expo projects use Hermes as the JavaScript engine. Switching to other engines, such as JSC or V8, for Android and iOS platforms is possible. However, not for the web because the JavaScript engine is included in the web browser.
通过应用配置配置 jsEngine
🌐 Configuring the jsEngine through app config
警告 从 SDK 52 开始,Expo Go 中无法更改 JS 引擎(只能使用 Hermes 引擎)。需要使用开发构建才能进行此自定义。
我们推荐 Hermes,因为它是专为 React Native 应用构建和优化的,并且具有最佳的调试体验。如果你熟悉不同 JavaScript 引擎的权衡,并且希望更换 Hermes,应用配置 中的 jsEngine 字段允许你为应用指定 JavaScript 引擎。默认值是 hermes。
🌐 We recommend Hermes because it is purpose built and optimized for React Native apps, and it has the best debugging experience. If you are familiar with the tradeoffs of different JavaScript engines and would like to change away from Hermes, the jsEngine field inside app config allows you to specify the JavaScript engine for your app. The default value is hermes.
如果你想改用 JSC,请在应用配置中设置 jsEngine 字段:
🌐 If you want to use JSC instead, set the jsEngine field in the app config:
{ "expo": { "jsEngine": "jsc" } }
在原生 React Native 项目中的使用
要在纯 React Native 项目中更改 JavaScript 引擎,请更新 android/gradle.properties 和 ios/Podfile.properties.json 中的 expo.jsEngine 值。
🌐 To change the JavaScript engine in a bare React Native project, update the expo.jsEngine value in android/gradle.properties and ios/Podfile.properties.json.
需要强调的是,更改 JS 引擎将需要你重新使用 eas build 重新编译开发版本才能正常工作。
🌐 It's important to highlight that changing the JS engine will require you to recompile development builds with eas build to work properly.
使用 V8 引擎
🌐 Using the V8 engine
要使用 V8 引擎,你需要安装 react-native-v8,这是一个可选安装的包,为 React Native 添加 V8 运行时支持。你可以通过运行以下命令来安装它:
🌐 To use the V8 engine, you need to install react-native-v8, an opt-in package that adds V8 runtime support for React Native. You can install it by running the following command:
- npx expo install react-native-v8 v8-android-jit确保从应用配置中删除 jsEngine 字段。
🌐 Make sure to remove the jsEngine field from the app config.
在 Expo 预构建中使用
安装完成后运行 npx expo prebuild -p android --clean 以重新预构建。
🌐 Run npx expo prebuild -p android --clean after the installation to prebuild again.
在特定平台上切换 JavaScript 引擎
🌐 Switch JavaScript engine on a specific platform
要在某一个特定平台上使用不同的引擎,你可以在顶层设置 "jsEngine" 值,然后在 "android" 或 "ios" 键下用不同的值覆盖它。为该平台指定的值将优先于通用字段。
🌐 To use a different engine on one specific platform, you can set the "jsEngine" value at the top level and then override it with a different value under the "android" or "ios" key. The value specified for the platform will take precedence over the common field.
{ "expo": { "jsEngine": "hermes", "ios": { "jsEngine": "jsc" } } }