跳到主要内容
新架构实战课 实操 + 基建 + 原理全维度包揽,抢先掌握 React Native 新架构精髓 立即查看 >Version: Next

创建启用新架构的应用

注意

这个文档仍然是实验性的,随着我们的迭代,细节会有变化。欢迎在工作小组内的讨论中分享你的反馈。

此外,它还包含几个手动步骤。请注意新架构尚未稳定下来,最终的开发者体验会继续迭代改善。我们正在努力开发工具、模板和库,以帮助你在新架构上快速入门,而不需要经历整个设置过程。

本文档将帮助你从头创建一个启用了新架构的 React Native 应用。

开发环境

Before continuing, make sure you've followed all the steps in Setting up the development environment, under the React Native CLI Quickstart tab.

If following the setup guide, stop when you reach the section Running your React Native Application, and resume following this guide.

注意

If you're using Expo, you can't enable the New Architecture at the moment, and will have to wait for a future release of the Expo SDK.

创建新应用

如果你之前全局安装过旧的react-native-cli命令行工具,请使用npm uninstall -g react-native-cli卸载掉它以避免一些冲突:

npm uninstall -g react-native-cli @react-native-community/cli

If you already have your development environment set up, create a new React Native project from the template:

npx react-native init AwesomeProject
注意

The New Architecture is available in React Native version 0.68 or later.

配置

Follow the steps below to enable the New Architecture and build the app.

启用 Hermes

Hermes is an open-source JavaScript engine optimized for React Native. Hermes will be the default engine in the future, and we highly recommend you use it.

Please follow the instructions on the React Native website in order to enable Hermes in your application.

启用新架构

Target OS

Set the newArchEnabled property to true by either:

  • Changing the corresponding line in android/gradle.properties
  • Setting the environment variable ORG_GRADLE_PROJECT_newArchEnabled=true

Then build and run the app as usual:

yarn android
备注

You may notice longer build times with the New Architecture, due to additional step of C++ compilation with the Android NDK. To improve your build time, see Speeding Up Your Build Phase.

Confirming the New Architecture is in use

After you build and run the app, when Metro serves the JavaScript bundle, you should see "fabric": true in the Metro logs:

Metro shows fabric: true

Want to know more?

If you'd like to view the code changes relevant for the New Architecture, take a look at the upgrade helper from version 0.67.4 to 0.68.0. Files that were added for the New Architecture are marked with a yellow banner.

For further explanations of what each file is doing, check out these guides to walk through the changes step-by-step:

Android

iOS