AGENT SKILL

Expo 原生模块开发

expo-module

使用 Expo Modules API(Swift、Kotlin、TypeScript)编写 Expo 原生模块和视图的指南。涵盖模块定义 DSL、原生视图、共享对象、配置插件、生命周期钩子、自动链接(autolinking)和类型系统。在构建或修改 Expo 原生模块时使用。

使用场景代码实现
License 许可
MIT
所属仓库
openai/plugins
最近更新
2026-11-14
Stars
★ 7.9k
Forks
1.0k

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 资源文件 19

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 as modules/my-module/ because the slug is derived from customTargetPath which is undefined for --local modules — the --name flag 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 run cd ios && pod install so CocoaPods picks up the correct path. Skipping the rename is fine functionally, but skipping pod install after 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:

  1. 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.
  2. Remove unnecessary boilerplate: The scaffold includes example code (hello() function, PI constant, onChange event, WebView-based view with url prop). Strip all of this and replace with your actual implementation.
  3. Remove web files if not needed: The scaffold generates *.web.ts/*.web.tsx files for web platform support. Remove these if the module is native-only. Also remove "web" from the platforms array in expo-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.ts and view export from index.ts

What to remove for a view-only (no module functions):

  • Remove Function, AsyncFunction, Constant, Events blocks from the module definition (keep Name and View)
  • 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

openai
openai/plugins

Postgres 性能优化最佳实践

supabase-postgres-best-practices

来自 Supabase 的 Postgres 性能优化与最佳实践。在编写、审查或优化 Postgres 查询、schema 设计或数据库配置时使用此技能。

数据库管理员数据库管理
★ 7.9k
openai
openai/plugins

极简技能示例

minimal-skill

用于 plugin-eval 测试的极简示例技能。当用户想要一个结构良好的技能的紧凑演示时使用。

软件开发工程师示例与演示技能
★ 7.9k
openai
openai/plugins

NVIDIA 物理 AI 神经重建

physical-ai-neural-reconstruction

NVIDIA NuRec/NRE 路由技能:USDZ 渲染、NCore 转换、3DGS、gRPC 传感器模拟、PhysicalAI Hugging Face(HF)数据集。不用于 SimReady 或基础设施搭建。

AI 工程师XR 与空间计算开发
★ 7.9k
openai
openai/plugins

NVIDIA 物理 AI 基础设施搭建与扩展

physical-ai-infrastructure-setup-and-resilient-scaling

当用户需要为跨本地 MicroK8s 或 Azure AKS 的合成数据生成工作流搭建、扩展、验证或加固 NVIDIA 物理 AI 基础设施时使用,包括 Kubernetes 集群、推理端点部署、OSMO 部署、工作负载提交准备以及基础设施故障恢复。触发关键词:physical ai infrastructure、resilient scaling、SDG infrastructure、microk8s、azure aks、NVCF deployment、NIM Operator、OSMO deploy、workflow scaling。不触发场景:OSMO 日志摘要或仅涉及工作负载的操作,除非明确要求基础设施搭建、扩展、验证或恢复。

AI 工程师云原生计算与容器编排
★ 7.9k
openai
openai/plugins

Omniverse USD 性能调优

omniverse-usd-performance-tuning

用于 USD 性能诊断与优化的顶层工作流技能。适用于加载缓慢、内存占用高、帧率(FPS)低或'优化我的场景'等请求;将认证/运行时设置委托给 Phase 0 负责人处理。

多媒体工程师XR 与空间计算开发
★ 7.9k
openai
openai/plugins

Omniverse 实时查看器 USD 应用路由

omniverse-realtime-viewer

作为 Omniverse Realtime Viewer(实时查看器)USD 应用请求及专项查看器参考文档的顶层路由使用。

多媒体工程师XR 与空间计算开发
★ 7.9k