集成到现有库中
了解如何将 Expo Modules API 集成到现有的 React Native 库中。
在某些情况下,你可能希望将 Expo 模块 API 集成到现有的 React Native 库中。例如,这可能有助于逐步重写你的库,或利用 Android 生命周期监听器 和 iOS AppDelegate 订阅者 来自动设置库。
🌐 There are cases where you may want to integrate the Expo Modules API into an existing React Native library. For example, it might be useful to incrementally rewrite your library or to take advantage of Android lifecycle listeners and iOS AppDelegate subscribers to automatically set up the library.
本指南将帮助你设置现有的 React Native 库以访问 Expo Modules API。
🌐 This guide will help you set up your existing React Native library to access Expo Modules API.
先决条件
🌐 Prerequisites
在项目根目录下创建 expo-module.config.json 文件,并在其中添加一个空对象 {}。稍后你会填写它以启用特定功能。
🌐 Create the expo-module.config.json file at the root of your project and add an empty object {} inside it. You will fill it in later to enable specific features.
创建此文件是必要的,以便 Expo Autolinking 将你的库识别为 Expo 模块并自动链接你的原生代码。
🌐 Creating this file is necessary for Expo Autolinking to recognize your library as an Expo module and automatically link your native code.
1
添加 expo-modules-core 原生依赖
🌐 Add the expo-modules-core native dependency
在你的 build.gradle 和 podspec 文件中添加 expo-modules-core 作为依赖:
🌐 Add expo-modules-core as a dependency in your build.gradle and podspec files:
// ... dependencies { // ... implementation project(':expo-modules-core') }
# ... Pod::Spec.new do |s| # ... s.dependency 'ExpoModulesCore' end
2
将 Expo 包添加到依赖中
🌐 Add Expo packages to dependencies
在你的 package.json 中将 expo 包添加为一个对等依赖 —— 我们建议使用 * 作为版本范围,以避免在用户的 node_modules 目录中出现重复的包。
🌐 Add expo package as a peer dependency in your package.json — we recommend using * as a version range so as not to cause any duplicated packages in user's node_modules directory.
你的库还需要依赖 expo-modules-core,但仅作为开发依赖——它已经由依赖你库的项目中的 expo 包提供,该包使用的核心版本与项目中使用的特定 SDK 兼容。
🌐 Your library also needs to depend on expo-modules-core but only as a dev dependency — it's already provided in the projects depending on your library by the expo package with the version of core that is compatible with the specific SDK used in the project.
{ %%placeholder-start%%... %%placeholder-end%% "devDependencies": { "expo-modules-core": "^X.Y.Z" }, "peerDependencies": { "expo": "*" }, "peerDependenciesMeta": { "expo": { "optional": true } } }
3
创建原生模块
🌐 Create a native module
根据以下模板创建 Kotlin 和 Swift 文件:
🌐 Create Kotlin and Swift files from the templates below:
package my.module.package import expo.modules.kotlin.modules.Module import expo.modules.kotlin.modules.ModuleDefinition class MyModule : Module() { override fun definition() = ModuleDefinition { // Definition components go here } }
import ExpoModulesCore public class MyModule: Module { public func definition() -> ModuleDefinition { // Definition components go here } }
然后,将你的类添加到 Android 和/或 iOS 的 modules,在 expo-module.config.json 文件中。Expo 自动链接会自动将这些类作为原生模块链接到用户的项目中。
🌐 Then, add your classes to Android and/or iOS modules in the expo-module.config.json file. Expo Autolinking will automatically link these classes as native modules in the user's project.
{ "ios": { "modules": ["MyModule"] }, "android": { "modules": ["my.module.package.MyModule"] } }
如果你的工作区中已经有示例应用,请确保模块已正确链接。
🌐 If you already have an example app in your workspace, ensure that the module is linked correctly.
- 在 Android 上,本地模块类将在构建之前自动链接,作为 Gradle 构建任务的一部分。
- 在 iOS 上,你需要运行
pod install来链接新类。
现在可以通过 expo-modules-core 包中的 requireNativeModule 函数从 JavaScript 代码访问这些模块类。我们建议创建一个单独的文件来导出本地模块,以简化操作。
🌐 These module classes are now accessible from the JavaScript code using the requireNativeModule function from the expo-modules-core package. We recommend creating a separate file that exports the native module for simplicity.
import { requireNativeModule } from 'expo-modules-core'; export default requireNativeModule('MyModule');
现在类已经设置并链接好,你可以开始实现其功能。请参阅native module API参考页面以及从简单到中等复杂的实际模块的示例链接,以了解如何使用该 API。
🌐 Now that the class is set up and linked, you can start to implement its functionality. See the native module API reference page and links to examples from simple to moderately complex real-world modules to understand how to use the API.