首页指南参考教程

配置插件:介绍

Expo 项目的配置插件简介。


可以自动设置将原生模块添加到你的项目中。有时,模块需要更复杂的设置。配置插件可用于自动配置模块的原生项目,并通过避免与原生项目交互来降低复杂性。

¥An automatic setup for adding a native module to your project is possible. Sometimes, a module requires a more complex setup. A config plugin can be used to automatically configure your native project for a module and reduce the complexity by avoiding interaction with the native project.

什么是配置插件

¥What is a config plugin

配置插件是一个用于扩展 应用配置 并为你的应用自定义预构建过程的系统。它们可用于添加默认情况下未包含的原生模块,或添加需要进一步配置的任何原生代码。

¥Config plugin is a system for extending the app config and customizing the prebuild process for your app. They can be used to add native modules that aren't included, by default, or to add any native code that needs to be configured further.

在内部,Expo CLI 使用配置插件来生成和配置托管项目的所有原生代码。插件执行诸如生成应用图标、设置应用名称以及配置 AndroidManifest.xml、Info.plist 等操作。

¥Internally Expo CLI uses config plugins to generate and configure all the native code for a managed project. Plugins do things such as generate app icons, set the app name, and configure the AndroidManifest.xml, Info.plist, and so on.

你可以将插件视为原生项目的打包程序,并运行 npx expo prebuild 作为通过评估所有项目插件来打包项目的方式。这样做将生成 android 和 ios 目录。这些目录在生成后可以手动修改,但是如果不潜在地覆盖手动修改,就无法再安全地重新生成它们。

¥You can think of plugins like a bundler for native projects, and running npx expo prebuild as a way to bundle the projects by evaluating all the project plugins. Doing so will generate android and ios directories. These directories can be modified manually after being generated, but then they can no longer be safely regenerated without potentially overwriting manual modifications.

使用配置插件

¥Use a config plugin

Expo 配置插件主要来自 Node.js 模块。你可以像项目中的其他包一样安装它们。

¥Expo config plugins mostly come from Node.js modules. You can install them just like other packages in your project.

例如,expo-camera 有一个插件,可以将相机权限添加到 AndroidManifest.xml 和 Info.plist 中。要将其安装到你的项目中,请运行以下命令:

¥For example, expo-camera has a plugin that adds camera permissions to the AndroidManifest.xml and Info.plist. To install it in your project, run the following command:

Terminal
npx expo install expo-camera

在你的 应用的配置 中,你可以将 expo-camera 添加到插件列表中:

¥In your app's config, you can add expo-camera to the list of plugins:

app.json
{
  "expo": {
    "plugins": ["expo-camera"]
  }
}

某些配置插件通过允许你传递选项来自定义其配置,从而提供了灵活性。为此,你可以传递一个数组,其中 Expo 库名称作为第一个参数,包含选项的对象作为第二个参数。例如,expo-camera 插件允许你自定义相机权限消息:

¥Some config plugins offer flexibility by allowing you to pass options to customize their configuration. To do this, you can pass an array with the Expo library name as the first argument, and an object containing the options as the second argument. For example, the expo-camera plugin allows you to customize the camera permission message:

app.json
{
  "expo": {
    "plugins": [
      [
        "expo-camera",
        {
          "cameraPermission": "Allow $(PRODUCT_NAME) to access your camera."
        }
      ]
    ]
  }
}
提示:对于每个具有配置插件的 Expo 库,你都可以在库的 API 参考中找到有关它的更多信息。例如,expo-camera 库有一个配置插件部分

运行 npx expo prebuild 时,会编译 mods,并且原生文件会发生变化。

¥On running the npx expo prebuild, the mods are compiled, and the native files change.

直到你重建原生项目(例如使用 Xcode)后,更改才会生效。如果你在托管应用中使用配置插件,它们将在 eas build 上的预构建阶段应用。

¥The changes don't take effect until you rebuild the native project, for example, with Xcode. If you're using config plugins in a managed app, they will be applied during the prebuild phase on eas build.

Expo 中文网 - 粤ICP备13048890号