首页指南参考教程

配置 JS 引擎

有关在 Expo 项目中为 Android 和 iOS 配置 JS 引擎的指南。


JavaScript 引擎执行你的应用代码并提供各种功能,例如内存管理、优化和错误处理。默认情况下,Expo 项目使用 Hermes 作为 JavaScript 引擎(在 SDK 47 及更低版本中,默认为 JavaScriptCore)。可以切换到适用于 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 (in SDK 47 and lower, the default was JavaScriptCore). 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

我们推荐 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:

app.json
{
  "expo": {
    "jsEngine": "jsc"
  }
}
Usage in bare React Native projects

要更改裸 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:

Terminal
npx expo install react-native-v8 v8-android-jit

确保从应用配置中删除 jsEngine 字段。

¥Make sure to remove the jsEngine field from the app config.

Usage with Expo Prebuild

安装后运行 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.

app.json
{
  "expo": {
    "jsEngine": "hermes",
    "ios": {
      "jsEngine": "jsc"
    }
  }
}
Expo 中文网 - 粤ICP备13048890号