Android 预置 Expo 模块

了解预构建的 Expo 模块如何将 Android 构建时间在你的设备上缩短高达 25%。


构建 React Native 应用时,较长的构建时间会减慢你的开发工作流程并降低生产力。每次更改代码时,你可能需要等待构建过程完成,这可能会造成严重的延迟。

¥When building React Native apps, longer build times can slow down your development workflow and reduce productivity. Each time you make changes to your code, you might need to wait for the build process to complete, which can add up to significant delays.

从 SDK 53 开始,Expo 引入了针对 Android 的预构建 Expo 模块来解决这一痛点。现在,你的项目可以使用这些模块的预编译版本,而不必在每次构建时从头开始编译 Expo 模块源代码。最终,这将缩短构建时间。

¥Starting from SDK 53, Expo introduces prebuilt Expo Modules for Android to address this pain point. Instead of compiling Expo Modules source code from scratch during each build, your project can now use pre-compiled versions of these modules. Ultimately, this results in faster build times.

优势

¥Benefits

  • 更快的本地开发:本地机器上的 Android 构建时间最多可缩短 25%

    ¥Faster local development: Up to 25% reduction in Android build times on local machines

  • 提升开发者体验:减少开发迭代期间的等待时间

    ¥Improved developer experience: Less waiting time during development iterations

  • 自动优化:适用于 SDK 53 及以上版本的新项目,开箱即用。

    ¥Automatic optimization: Works out of the box with new projects for SDK 53 and above

Android 预构建的 Expo 模块如何工作

¥How prebuilt Expo Modules for Android work

在项目的 Android 构建过程中,请在构建输出中查找软件包名称旁边的 [📦] 表情符号前缀。这表明这些软件包使用的是预构建版本,而不是从源代码编译的。

¥During your project's Android build process, look for the [📦] emoji prefix next to package names in the build output. This indicates that those packages are using prebuilt versions rather than being compiled from source.

例如,在使用 SDK 53 的默认模板创建项目并运行 npx expo run:android 命令后,你会注意到预编译包旁边的 [📦 package-name 前缀:

¥For example, after creating a project with SDK 53's default template, and running the npx expo run:android command, you will notice the [📦 package-name prefix next to packages that are precompiled:

配置

¥Configuration

对于 SDK 53 及以上版本,使用可用 Expo 模板 创建的项目无需任何配置步骤。

¥For SDK 53 and above, no configuration steps are required for projects that are created with one of the available Expo templates.

退出预置 Expo 模块

¥Opting out of prebuilt Expo Modules

你可以选择退出预构建模块。当你自己修改模块源代码时,可能需要这样做。在这种情况下,你可以通过将 buildFromSource 添加到 package.json 文件来配置 Expo Autolinking 配置:

¥You can opt out of prebuilt modules. This might be required when you are modifying the module source code yourself. In this scenario, you can configure the Expo Autolinking configuration by adding buildFromSource to the package.json file:

package.json
{ "name": "your-app-name", "expo": { "autolinking": { "android": { "buildFromSource": [ ".*" ] } } } }

选择性退出

¥Selectively opt out

你还可以通过指定单独的软件包名称(而不是通配符 ".*")来选择退出特定模块,同时保留其他预构建模块:

¥You can also opt out of specific modules while keeping others prebuilt by specifying individual package names instead of the wildcard ".*":

package.json
{ "name": "your-app-name", "expo": { "autolinking": { "android": { "buildFromSource": [ "expo-camera", "expo-web-browser", "expo-linking", ] } } } }

注意事项

¥Considerations

  • 现有项目升级到 SDK 53 及以上版本后可受益于此功能。

    ¥Existing projects can benefit from this feature when upgrading to SDK 53 and above

  • 性能提升可能因硬件配置而异

    ¥Performance improvements may vary based on your hardware configuration

  • EAS 构建的当前改进较为温和,但为未来的缓存机制奠定了基础。

    ¥Current improvements on EAS Builds are more modest but provide groundwork for future caching mechanisms