Expo 原生模块开发
expo-module
使用 Expo Modules API(Swift、Kotlin、TypeScript)编写 Expo 原生模块和视图的指南。涵盖模块定义 DSL、原生视图、共享对象、配置插件、生命周期钩子、自动链接(autolinking)和类型系统。适用于构建或修改 Expo 原生模块时使用。
- License 许可
- MIT
- 所属仓库
- stigmer/stigmer
- 最近更新
- 2026-11-15
- Stars
- ★ 9
- Forks
- 6
01 前言 Front Matter
- name
- expo-module
- license
- MIT
- version
- 1.0.0
- description
- Guide for writing Expo native modules and views using the Expo Modules API (Swift, Kotlin, TypeScript). Covers module definition DSL, native views, shared objects, config plugins, lifecycle hooks, autolinking, and type system. Use when building or modifying native modules for Expo.
02 资源文件 24
- 参考文档agents/openai.yaml367 B
- 资产文件data/domain-complexity.csv7.2 KB
- 参考文档data/prd-purpose.md7.1 KB
- 资产文件data/project-types.csv3.1 KB
- 参考文档references/config-plugin.md2.0 KB
- 参考文档references/lifecycle.md3.7 KB
- 参考文档references/module-config.md1.1 KB
- 参考文档references/native-module.md5.7 KB
- 参考文档references/native-view.md3.5 KB
- 参考文档steps-v/step-v-01-discovery.md7.0 KB
- 参考文档steps-v/step-v-02-format-detection.md6.4 KB
- 参考文档steps-v/step-v-02b-parity-check.md6.3 KB
- 参考文档steps-v/step-v-03-density-validation.md5.6 KB
- 参考文档steps-v/step-v-04-brief-coverage-validation.md7.0 KB
- 参考文档steps-v/step-v-05-measurability-validation.md6.9 KB
- 参考文档steps-v/step-v-06-traceability-validation.md7.1 KB
- 参考文档steps-v/step-v-07-implementation-leakage-validation.md6.8 KB
- 参考文档steps-v/step-v-08-domain-compliance-validation.md7.5 KB
- 参考文档steps-v/step-v-09-project-type-validation.md8.4 KB
- 参考文档steps-v/step-v-10-smart-validation.md6.6 KB
- 参考文档steps-v/step-v-11-holistic-quality-validation.md8.5 KB
- 参考文档steps-v/step-v-12-completeness-validation.md8.0 KB
- 参考文档steps-v/step-v-13-report-complete.md7.9 KB
- 参考文档workflow.md2.8 KB
03 SKILL.md 正文
Writing Expo Modules
Complete reference for building native modules and views using the Expo Modules API. Covers Swift (iOS), Kotlin (Android), and TypeScript.
When to Use
- Creating a new Expo native module or native view
- Adding native functionality (camera, sensors, system APIs) to an Expo app
- Wrapping platform SDKs for React Native consumption
- Building config plugins that modify native project files
References
Consult these resources as needed:
references/
native-module.md Module definition DSL: Name, Function, AsyncFunction, Property, Constant, Events, type system, shared objects
native-view.md Native view components: View, Prop, EventDispatcher, view lifecycle, ref-based functions
lifecycle.md Lifecycle hooks: module, iOS app/AppDelegate, Android activity/application listeners
config-plugin.md Config plugins: modifying Info.plist, AndroidManifest.xml, reading values in native code
module-config.md expo-module.config.json fields and autolinking configuration
Quick Start
Create a Local Module (in existing app)
Always scaffold with create-expo-module first, then modify the generated code. This ensures correct podspec, build.gradle, and module config — avoiding common build errors.
CI=1 npx create-expo-module@latest --local \
--name MyModule \
--description "My Expo module" \
--package expo.modules.mymodule
CI=1 skips interactive prompts and uses the provided flags.
Important: In
CI=1(non-interactive) mode, the scaffold always creates the directory asmodules/my-module/because the slug is derived fromcustomTargetPathwhich isundefinedfor--localmodules — the--nameflag only sets the native class name, not the directory. After scaffolding, rename it to a kebab-case name matching your module (e.g.,KeyValueStore→modules/key-value-store/), then runcd ios && pod installso CocoaPods picks up the correct path. Skipping the rename is fine functionally, but skippingpod installafter any rename causes iOS build failures ("Build input file cannot be found").
Available flags:
| Flag | Description | Example |
|---|---|---|
--name |
Native module name (PascalCase) | --name KeyValueStore |
--description |
Module description | --description "Native key-value storage" |
--package |
Android package name | --package expo.modules.keyvaluestore |
--author-name |
Author name | --author-name "dev" |
--author-email |
Author email | --author-email "dev@example.com" |
--author-url |
Author profile URL | --author-url "https://github.com/dev" |
--repo |
Repository URL | --repo "https://github.com/dev/repo" |
The scaffold generates both a native module (functions, events, constants) and a native view component (WebView example with props and events). After scaffolding:
- Decide what you need: If you only need a native module (no UI), remove the view files. If you only need a native view, remove the module function boilerplate. If you need both, keep both and replace the implementations.
- Remove unnecessary boilerplate: The scaffold includes example code (
hello()function,PIconstant,onChangeevent, WebView-based view withurlprop). Strip all of this and replace with your actual implementation. - Remove web files if not needed: The scaffold generates
*.web.ts/*.web.tsxfiles for web platform support. Remove these if the module is native-only. Also remove"web"from theplatformsarray inexpo-module.config.json.
What to remove for a module-only (no native view):
- Delete
ios/MyModuleView.swift,android/.../MyModuleView.kt - Delete
src/MyModuleView.tsx,src/MyModuleView.web.tsx - Remove the
View(...)block from the module definition in both Swift and Kotlin - Remove view-related types from
MyModule.types.tsand view export fromindex.ts
What to remove for a view-only (no module functions):
- Remove
Function,AsyncFunction,Constant,Eventsblocks from the module definition (keepNameandView) - Simplify the TypeScript module file to only export the view
Generated structure (after renaming from my-module to your module's kebab-case name):
modules/
my-module/ # Rename to kebab-case, e.g. key-value-store/
android/
build.gradle
src/main/java/expo/modules/mymodule/
MyModule.kt # Module definition (functions, events, view registration)
MyModuleView.kt # Native view (ExpoView subclass)
ios/
MyModule.podspec
MyModule.swift # Module definition
MyModuleView.swift # Native view (ExpoView subclass)
src/
MyModule.ts # Native module binding
MyModule.web.ts # Web implementation
MyModule.types.ts # Shared types
MyModuleView.tsx # Native view component
MyModuleView.web.tsx # Web view component
expo-module.config.json
index.ts # Re-exports module + view
Create a Standalone Module (for publishing)
npx create-expo-module@latest my-module
Module Structure Reference
The Swift and Kotlin DSL share the same structure. Both platforms are shown here for reference — in other reference files, Swift is shown as the primary language unless the Kotlin pattern meaningfully differs.
Swift (iOS):
import ExpoModulesCore
public class MyModule: Module {
public func definition() -> ModuleDefinition {
Name("MyModule")
Function("hello") { (name: String) -> String in
return "Hello \(name)!"
}
}
}
Kotlin (Android):
package expo.modules.mymodule
import expo.modules.kotlin.modules.Module
import expo.modules.kotlin.modules.ModuleDefinition
class MyModule : Module() {
override fun definition() = ModuleDefinition {
Name("MyModule")
Function("hello") { name: String ->
"Hello $name!"
}
}
}
TypeScript:
import { requireNativeModule } from "expo";
const MyModule = requireNativeModule("MyModule");
export function hello(name: string): string {
return MyModule.hello(name);
}
expo-module.config.json
{
"platforms": ["android", "apple"],
"apple": {
"modules": ["MyModule"]
},
"android": {
"modules": ["expo.modules.mymodule.MyModule"]
}
}
Note: iOS uses just the class name; Android uses the fully-qualified class name (package + class). See references/module-config.md for all fields.
04 同仓库更多 Skills
spreadsheet-export
Use whenever your work makes a table, to save it as a .csv too.
stigmer
Sets up Stigmer for the project in front of you and keeps working with it. Stigmer runs AI agents for the users of the person's own app. Use when the person asks to set up Stigmer, to add an AI agent for their app's users, to put an agent's chat in their app, or to make, run, change or read Stigmer agents.
Hookify 规则编写
writing-hookify-rules
当用户要求“创建 hookify 规则”“编写 hook 规则”“配置 hookify”“添加 hookify 规则”,或需要有关 hookify 规则语法和模式的指导时,应使用此技能。
SQL 数据库迁移
sql-migrations
编写安全的数据库模式迁移——采用可逆步骤、在线索引构建和分批数据回填。
Go 仓库代码规范
repo-conventions
在本仓库中修改 Go 代码的规范——错误包装、表驱动测试、文档注释、不引入新依赖,以及完成前运行 go test。
事故复盘报告撰写
incident-postmortem
撰写无指责的事故复盘报告——包括时间线、影响范围、根本原因、促成因素以及明确负责人的后续行动项。