Expo 棕地

用于将 Expo 集成到现有原生应用中的工具包和 API。

Android
iOS
Bundled version:
~55.0.14

expo-brownfield 是一个用于向现有原生 Android 和 iOS 应用添加 React Native 视图的工具包。它提供:

  • 内置 API 用于原生应用与 React Native 应用之间的双向通信和导航
  • 配置插件 用于在你的 Expo 项目中自动设置棕地目标
  • CLI 用于构建并发布到 Maven 仓库(Android)和 XCFrameworks(iOS)的工件

安装

🌐 Installation

Terminal
npx expo install expo-brownfield

If you are installing this in an existing React Native app, make sure to install expo in your project.

用法

🌐 Usage

通信 API

🌐 Communication API

通信 API 允许本地(宿主)应用与 React Native 之间进行双向、基于消息的通信。

🌐 The Communication API enables bi-directional, message-based communication between the native (host) app and React Native.

从 React Native 发送消息到原生

🌐 Sending messages from React Native to native

import * as Brownfield from 'expo-brownfield'; Brownfield.sendMessage({ type: 'MyMessage', data: { language: 'TypeScript', expo: true, platforms: ['android', 'ios'], }, });

在 React Native 中接收来自本地用户的消息

🌐 Receiving messages from native in React Native

import * as Brownfield, { type MessageEvent } from 'expo-brownfield'; import { useEffect } from 'react'; function MyComponent() { useEffect(() => { const handleMessage = (event: MessageEvent) => { console.log('Received message:', event); }; Brownfield.addMessageListener(handleMessage); return () => { Brownfield.removeMessageListener(handleMessage); }; }, []); // ... }

从原生发送消息到 React Native

🌐 Sending messages from native to 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 的消息

🌐 Receiving messages from React Native in native

import expo.modules.brownfield.BrownfieldMessaging val listenerId = BrownfieldMessaging.addListener { event -> println("Message from React Native: $event") } // Later, to remove the listener: BrownfieldMessaging.removeListener(listenerId)
import ExpoBrownfield let listenerId = BrownfieldMessaging.addListener { message in print("Message from React Native: \(message)") } // Later, to remove the listener: BrownfieldMessaging.removeListener(id: listenerId)

应用配置中的配置

🌐 Configuration in app config

expo-brownfield 包提供了一个 配置插件,可用于在使用 连续原生生成 (CNG) 时配置棕地集成。这个插件允许你自定义 Expo 项目如何打包并集成到你现有的原生应用中。

🌐 The expo-brownfield package provides a config plugin that can be used to configure the brownfield integration when using Continuous Native Generation (CNG). This plugin allows you to customize how your Expo project is packaged and integrated into your existing native app.

Example app.json with config plugin

app.json
{ "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

NameDefaultDescription
ios.targetName"<scheme>brownfield" or "<slug>brownfield"
Only for:
iOS

Name of the Xcode target for the brownfield integration. This is used to create a separate target in your Xcode project for the React Native code.

ios.bundleIdentifier"<ios.bundleIdentifier base>.<targetName>" or "com.example.<targetName>"
Only for:
iOS

Bundle identifier for the brownfield target. This should be unique and different from your main app bundle identifier.

ios.buildReactNativeFromSourcetrue
Only for:
iOS

Build React Native from source instead of using prebuilt frameworks. Turning this on significantly increases the build times.

android.group"<package without last segment>"
Only for:
Android

Maven group ID for the generated Android library. This is used when publishing the library to a Maven repository.

android.libraryName"brownfield"
Only for:
Android

Name of the generated Android library module.

android.package"<android.package>.brownfield" or "com.example.brownfield"
Only for:
Android

Java/Kotlin package name for the generated Android library code.

android.version"1.0.0"
Only for:
Android

Version string for the generated Android library. This is used when publishing to a Maven repository.

android.publishing[{ type: "localMaven" }]
Only for:
Android

Publishing configuration for the generated Android library. Supports localMaven, localDirectory, remotePublic, and remotePrivate publication types. Each type has different configuration options for specifying where and how the library is published.

命令行接口

🌐 CLI

expo-brownfield 库包含一个用于构建和发布到 Maven 仓库(Android)以及 XCFrameworks(iOS)的命令行工具(CLI)。

🌐 The expo-brownfield library includes a CLI for building and publishing to Maven repositories (Android) and XCFrameworks (iOS).

Terminal
npx expo-brownfield [command] [options]

命令

🌐 Commands

build:android

构建并发布棕地库及其依赖到 Maven 仓库。

🌐 Builds and publishes the brownfield library and its dependencies to Maven repositories.

Terminal
npx expo-brownfield build:android [options]
选项描述
-d, --debug以调试模式构建
-r, --release以发布模式构建
-a, --all同时以调试和发布模式构建(默认)
-l, --library指定 brownfield 库名称
--repo, --repository指定要发布到的 Maven 仓库
-t, --task指定要运行的 Gradle 发布任务
--verbose包含子进程的所有日志

build:ios

构建棕地 XCFramework 并将 Hermes XCFramework 复制到 artifacts 目录。

🌐 Builds the brownfield XCFramework and copies the Hermes XCFramework to the artifacts directory.

Terminal
npx expo-brownfield build:ios [options]
选项描述
-d, --debug以调试模式构建
-r, --release以发布模式构建(默认)
-a, --artifacts构建产物目录路径(默认:./artifacts
-s, --scheme要构建的 Xcode 方案
-x, --xcworkspaceXcode 工作区路径
-p, --package将构建产物作为 Swift 包发布(可选择名称)
--verbose包含来自子进程的所有日志

tasks:android

列出所有可用的发布任务和 Maven 仓库。

🌐 Lists all available publish tasks and Maven repositories.

Terminal
npx expo-brownfield tasks:android

应用接口

🌐 API

import * as Brownfield from 'expo-brownfield';

Hooks

useSharedState(key, initialValue)

Android
iOS
ParameterTypeDescription
keystring

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.

Returns:
[T | undefined, (value: T | (prev: T | undefined) => T) => void]

A tuple containing the value and a function to set the value.

Methods

Brownfield.deleteSharedState(key)

Android
iOS
ParameterTypeDescription
keystring

The key to delete the shared state for.


Deletes the shared state for a given key.

Returns:
void

Brownfield.getMessageListenerCount()

Android
iOS

Gets the number of registered message listeners.

Returns:
number

The number of active message listeners.

Brownfield.getSharedStateValue(key)

Android
iOS
ParameterTypeDescription
keystring

The key to get the value for.


Gets the value of shared state for a given key.

Returns:
T | undefined

Brownfield.popToNative(animated)

Android
iOS
ParameterTypeDescription
animated(optional)boolean

Whether to animate the transition (iOS only). Defaults to false.

Default:false

Navigates back to the native part of the app, dismissing the React Native view.

Returns:
void

Brownfield.sendMessage(message)

Android
iOS
ParameterTypeDescription
messageRecord<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.

Returns:
void

Brownfield.setNativeBackEnabled(enabled)

Android
iOS
ParameterTypeDescription
enabledboolean

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.

Returns:
void

Brownfield.setSharedStateValue(key, value)

Android
iOS
ParameterTypeDescription
keystring

The key to set the value for.

valueT

The value to be set.


Sets the value of shared state for a given key.

Returns:
void

Event Subscriptions

Brownfield.addMessageListener(listener)

Android
iOS
ParameterTypeDescription
listenerListener<MessageEvent>

A callback function that receives message events from native.


Adds a listener for messages sent from the native side of the app.

Returns:
EventSubscription

A 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();

Brownfield.addSharedStateListener(key, callback)

Android
iOS
ParameterTypeDescription
keystring

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.

Returns:
EventSubscription

A subscription object that can be used to remove the listener.

Brownfield.removeAllMessageListeners()

Android
iOS

Removes all message listeners.

Returns:
void

Brownfield.removeMessageListener(listener)

Android
iOS
ParameterTypeDescription
listenerListener<MessageEvent>

The listener function to remove.


Removes a specific message listener.

Returns:
void

Interfaces

EventSubscription

Android
iOS

A subscription object that allows to conveniently remove an event listener from the emitter.

EventSubscription Methods

remove()

Android
iOS

Removes an event listener for which the subscription has been created. After calling this function, the listener will no longer receive any events from the emitter.

Returns:
void

Types

MessageEvent

Android
iOS

Type: Record<string, any>