Expo Brownfield
用于将 Expo 集成到现有原生应用中的工具包和 API。
For the complete documentation index, see llms.txt. Use this Use this file to discover all available pages.
expo-brownfield 是一个用于将 React Native 视图添加到现有原生 Android 和 iOS 应用中的工具包。它提供:
- 内置 API,用于原生应用和 React Native 应用之间的双向通信与导航
- 配置插件,用于在你的 Expo 项目中自动设置 brownfield 目标
- CLI,用于构建并发布到 Maven 仓库(Android)和 XCFrameworks(iOS)的制品
安装
- npx expo install expo-brownfieldIf you are installing this in an existing React Native app, make sure to install expo in your project.
用法
通信 API
通信 API 支持原生(宿主)应用与 React Native 之间基于消息的双向通信。
从 React Native 向原生发送消息
import * as Brownfield from 'expo-brownfield'; Brownfield.sendMessage({ type: 'MyMessage', data: { language: 'TypeScript', expo: true, platforms: ['android', 'ios'], }, });
在 React Native 中接收来自原生的消息
import * as Brownfield, { type MessageEvent } from 'expo-brownfield'; import { useEffect } from 'react'; function MyComponent() { useEffect(() => { const handleMessage = (event: MessageEvent) => { console.log('收到消息:', event); }; Brownfield.addMessageListener(handleMessage); return () => { Brownfield.removeMessageListener(handleMessage); }; }, []); // ... }
从原生向 React Native 发送消息
import expo.modules.brownfield.BrownfieldMessaging BrownfieldMessaging.sendMessage(mapOf( "type" to "MyAndroidMessage", "timestamp" to System.currentTimeMillis(), "data" to mapOf( "platform" to "android" ) ))
import ExpoBrownfield BrownfieldMessaging.sendMessage([ "type": "MyIOSMessage", "timestamp": Date().timeIntervalSince1970, "data": [ "platform": "ios" ] ])
在原生中接收来自 React Native 的消息
import expo.modules.brownfield.BrownfieldMessaging val listenerId = BrownfieldMessaging.addListener { event -> println("来自 React Native 的消息:$event") } // 之后,移除监听器: BrownfieldMessaging.removeListener(listenerId)
import ExpoBrownfield let listenerId = BrownfieldMessaging.addListener { message in print("来自 React Native 的消息:\(message)") } // 之后,移除监听器: BrownfieldMessaging.removeListener(id: listenerId)
app config 中的配置
expo-brownfield 包提供了一个 config plugin,可在使用 Continuous Native Generation (CNG) 时用于配置 brownfield 集成。此插件允许你自定义 Expo 项目的打包方式以及如何将其集成到现有原生应用中。
Example app.json with config plugin
{ "expo": { "plugins": [ [ "expo-brownfield", { "ios": { "targetName": "MyBrownfieldTarget", "bundleIdentifier": "com.example.brownfield" }, "android": { "group": "com.example", "libraryName": "brownfield", "package": "com.example.brownfield", "version": "1.0.0" } } ] ] } }
Configurable properties
| Name | Default | Description |
|---|---|---|
ios.targetName | "<scheme>brownfield" or "<slug>brownfield" | Only for: iOS brownfield 集成的 Xcode 目标名称。这用于在你的 Xcode 项目中为 React Native 代码创建一个独立目标。 |
ios.bundleIdentifier | "<ios.bundleIdentifier base>.<targetName>" or "com.example.<targetName>" | Only for: iOS brownfield 目标的 bundle 标识符。它应当是唯一的,并且与主应用的 bundle 标识符不同。 |
ios.buildReactNativeFromSource | true | Only for: iOS 从源代码构建 React Native,而不是使用预构建框架。开启此项会显著增加构建时间。 |
android.group | "<package without last segment>" | Only for: Android 生成的 Android 库的 Maven group ID。发布库到 Maven 仓库时会使用此项。 |
android.libraryName | "brownfield" | Only for: Android 生成的 Android 库模块名称。 |
android.package | "<android.package>.brownfield" or "com.example.brownfield" | Only for: Android 生成的 Android 库代码的 Java/Kotlin 包名。 |
android.version | "1.0.0" | Only for: Android 生成的 Android 库的版本字符串。发布到 Maven 仓库时会使用此项。 |
android.publishing | [{ type: "localMaven" }] | Only for: Android 生成的 Android 库的发布配置。支持 |
CLI
expo-brownfield 库包含一个 CLI,用于构建并发布到 Maven 仓库(Android)和 XCFrameworks(iOS)。
- npx expo-brownfield [command] [options]命令
build:android
构建并将 brownfield 库及其依赖发布到 Maven 仓库。
- npx expo-brownfield build:android [options]| 选项 | 描述 |
|---|---|
-d, --debug | 以调试模式构建 |
-r, --release | 以发布模式构建 |
-a, --all | 同时以调试和发布模式构建(默认) |
-l, --library | 指定 brownfield 库名称 |
--repo, --repository | 指定要发布到的 Maven 仓库 |
-t, --task | 指定要运行的 Gradle 发布任务 |
--verbose | 包含子进程的所有日志 |
build:ios
构建 brownfield XCFramework,并将 Hermes XCFramework 复制到制品目录。
- npx expo-brownfield build:ios [options]| 选项 | 描述 |
|---|---|
-d, --debug | 以调试模式构建 |
-r, --release | 以发布模式构建(默认) |
-a, --artifacts | 制品目录路径(默认:./artifacts) |
-s, --scheme | 要构建的 Xcode scheme |
-x, --xcworkspace | Xcode workspace 路径 |
-p, --package | 将制品作为 Swift Package 交付(接受可选名称) |
--verbose | 包含子进程的所有日志 |
tasks:android
列出所有可用的发布任务和 Maven 仓库。
- npx expo-brownfield tasks:androidAPI
import * as Brownfield from 'expo-brownfield';
Hooks
| Parameter | Type | Description |
|---|---|---|
| key | string | The key to get the value for. |
| initialValue(optional) | T | The initial value to be used if the shared state is not set. |
Hook to observe and set the value of shared state for a given key.
Provides a synchronous API similar to useState.
[T | undefined, (value: T | (prev: T | undefined) => T) => void]A tuple containing the value and a function to set the value.
Methods
| Parameter | Type | Description |
|---|---|---|
| key | string | The key to delete the shared state for. |
Deletes the shared state for a given key.
voidGets the number of registered message listeners.
numberThe number of active message listeners.
| Parameter | Type | Description |
|---|---|---|
| key | string | The key to get the value for. |
Gets the value of shared state for a given key.
T | undefined| Parameter | Type | Description |
|---|---|---|
| animated(optional) | boolean | Whether to animate the transition (iOS only). Defaults to Default: false |
Navigates back to the native part of the app, dismissing the React Native view.
void| Parameter | Type | Description |
|---|---|---|
| message | Record<string, any> | A dictionary containing the message payload to send to native. |
Sends a message to the native side of the app. The message can be received by setting up a listener in the native code.
void| Parameter | Type | Description |
|---|---|---|
| enabled | boolean | Whether to enable native back button handling. |
Enables or disables the native back button behavior. When enabled, pressing the back button will navigate back to the native part of the app instead of performing the default React Navigation back action.
void| Parameter | Type | Description |
|---|---|---|
| key | string | The key to set the value for. |
| value | T | The value to be set. |
Sets the value of shared state for a given key.
voidEvent Subscriptions
| Parameter | Type | Description |
|---|---|---|
| listener | Listener<MessageEvent> | A callback function that receives message events from native. |
Adds a listener for messages sent from the native side of the app.
EventSubscriptionA subscription object that can be used to remove the listener.
Example
const subscription = addMessageListener((event) => { console.log('Received message from native:', event); }); // Later, to remove the listener: subscription.remove();
| Parameter | Type | Description |
|---|---|---|
| key | string | The key to add the listener for. |
| callback | (value: T | undefined) => void | The callback to be called when the shared state changes. |
Adds a listener for changes to the shared state for a given key.
EventSubscriptionA subscription object that can be used to remove the listener.
| Parameter | Type | Description |
|---|---|---|
| listener | Listener<MessageEvent> | The listener function to remove. |
Removes a specific message listener.
voidInterfaces
A subscription object that allows to conveniently remove an event listener from the emitter.