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 模块的源代码。最终,这将带来更快的构建时间。

优势

🌐 Benefits

  • 更快的本地开发:本地机器上的 Android 构建时间最多减少 25%
  • 改进的开发者体验:减少开发迭代过程中的等待时间
  • 自动优化:适用于 SDK 53 及更高版本的新项目,开箱即用

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 模板 创建的项目无需进行任何配置步骤。

退出预置 Expo 模块

🌐 Opting out of prebuilt Expo Modules

你可以选择不使用预构建模块。当你自己修改模块源代码时,可能需要这样做。在这种情况下,你可以通过在 package.json 文件中添加 buildFromSource 来配置 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 及更高版本时可以从此功能中受益
  • 性能提升可能因硬件配置而异
  • EAS 构建的当前改进较为温和,但为未来的缓存机制奠定了基础。