构建电视 Expo 应用

为 Android TV 或 Apple TV 目标构建 Expo 应用的指南。


警告 并非所有 Expo 功能和 SDK 库都可在电视上使用。更多详情,请查看 查看支持的库

React Native 通过 React Native TV 项目 支持 Android TV 和 Apple TV。这项技术不仅限于电视,还提供了一个全面的核心仓库分支,支持手机和电视平台,包括 Hermes 和 Fabric。

🌐 React Native is supported on Android TV and Apple TV through the React Native TV project. This technology extends beyond TV, offering a comprehensive core repo fork with support for phone and TV targets, including Hermes and Fabric.

在 Expo 项目中使用 React Native TV 库作为 react-native 依赖时,它就能够同时面向移动设备(Android、iOS)和电视设备(Android TV、Apple TV)。

🌐 Using the React Native TV library as the react-native dependency in an Expo project, it becomes capable of targeting both mobile (Android, iOS) and TV (Android TV, Apple TV) devices.

先决条件

🌐 Prerequisites

对本地 Android 和 iOS 文件的必要更改很少,如果使用 prebuild,可以通过 config 插件 自动补齐。以下是 config 插件所做更改的列表,你也可以选择手动应用这些更改:

🌐 The necessary changes to the native Android and iOS files are minimal and can be automated with a config plugin if you use prebuild. Below is a list of changes made by the config plugins, which you can alternatively apply manually:

安卓

🌐 Android

  • AndroidManifest.xml 已被修改:
    • 删除默认手机纵向方向
    • 添加了电视应用所需的意图
  • MainApplication.kt 已被修改,以移除不支持的 Flipper 调用

iOS

  • ios/Podfile 已修改为面向 tvOS,而非 iOS
  • Xcode 项目已修改为目标 tvOS 而不是 iOS
  • 启动画面(SplashScreen.storyboard)已修改以适用于 tvOS

TV 开发的系统要求

🌐 System requirements for TV development

Android TV

  • Node.js(LTS) 在 macOS 或 Linux 上。
  • Android Studio(Iguana 或更高版本)。
  • 在 Android Studio 的 SDK 管理器中,选择你正在使用的 Android SDK 下拉菜单(API 版本 31 或更高版本),并确保选择了要安装的 Android TV 系统镜像。(对于 Apple Silicon,请选择 ARM 64 镜像;否则,请选择 Intel x86_64 镜像。)
  • 安装 Android TV 系统映像后,使用该映像创建 Android TV 模拟器(该过程与创建 Android 手机模拟器相同)。

Apple TV

  • Node.js(LTS) 在 macOS 上。
  • Xcode 16 或更高版本。
  • tvOS SDK 17 或更高版本。(这个不会随 Xcode 自动安装。你可以稍后使用 xcodebuild -downloadAllPlatforms 安装它)。

快速开始

🌐 Quick start

在 Expo 示例仓库中的 TV 示例 中描述了生成新项目的最快方法:

🌐 The fastest way to generate a new project is described in the TV example within the Expo examples repository:

Terminal
npx create-expo-app MyTVProject -e with-tv

你可以从 电视路由示例 开始:

🌐 You can start with the TV Router example:

Terminal
npx create-expo-app MyTVProject -e with-router-tv

这将创建一个新项目,该项目使用 Expo Router 进行基于文件的导航,参考 create-expo-app 默认模板 模式。

🌐 This creates a new project that uses Expo Router for file-based navigation, modeled after the create-expo-app default template.

查看支持哪些库

目前,电视应用可以使用以下列出的库和 API:

🌐 At this time, TV applications work with the following libraries and APIs listed below:

电视也可以与 React NavigationReact Native Skia 以及许多其他常用的第三方 React Native 库一起使用。请参阅 React Native 目录 以了解更多支持的第三方库信息。

🌐 TV also works with React Navigation, React Native Skia, and many other commonly used third-party React Native libraries. See React Native directory to learn more about supported third-party libraries.

局限性

🌐 Limitations

  • Expo DevClient 库仅支持 SDK 54 及更高版本:
    • Android TV:支持所有操作,类似于安卓手机。
    • Apple TV:支持使用本地或隧道打包程序进行基本操作。目前尚不支持 EAS 的身份验证以及 EAS 构建和更新的列出功能。

与现有的 Expo 项目集成

🌐 Integration with an existing Expo project

以下指南描述了修改适用于电视的 Expo 项目所需的步骤。

🌐 The following walkthrough describes the steps required to modify an Expo project for TV.

1

修改电视的依赖

🌐 Modify dependencies for TV

package.json 中,将 react-native 依赖修改为使用 TV 仓库,并将此依赖排除在 npx expo install 版本验证 之外。

🌐 In package.json, modify the react-native dependency to use the TV repo, and exclude this dependency from npx expo install version validation.

警告 react-native-tvos 版本应与你使用的 Expo SDK 相匹配。例如,Expo SDK 54 使用 React Native 0.81,因此你应使用 react-native-tvos@0.81-stable(最新的 0.81 版本),如下所示。有关旧版本 SDK 的正确使用版本,请参阅SDK 兼容性表

package.json
{ %%placeholder-start%%... %%placeholder-end%% "dependencies": { %%placeholder-start%%... %%placeholder-end%% "react-native": "npm:react-native-tvos@0.81-stable", %%placeholder-start%%... %%placeholder-end%% }, "expo": { "install": { "exclude": [ "react-native" ] } } }

2

添加电视配置插件

🌐 Add the TV config plugin

Terminal
npx expo install @react-native-tvos/config-tv -- --dev

安装后,该插件将在以下任一情况下修改电视项目:

🌐 When installed, the plugin will modify the project for TV when either:

  • 环境变量 EXPO_TV 已设置为 1
  • 插件参数 isTV 设置为 true

确认此插件出现在 app.json 中:

🌐 Verify that this plugin appears in app.json:

app.json
{ "plugins": ["@react-native-tvos/config-tv"] }

要查看插件在预构建期间的更多操作信息,你可以在运行预构建之前设置调试环境变量。(另请参阅我们关于Expo CLI 环境变量的文档。)

🌐 To see additional information on the plugin's actions during prebuild, you can set debug environment variables before running prebuild. (See also our documentation on Expo CLI environment variables.)

Terminal
# See all Expo CLI and config plugin debug information
export DEBUG=expo:*

# See only debug information for the TV plugin
export DEBUG=expo:react-native-tvos:config-tv

3

运行预构建

🌐 Run prebuild

设置 EXPO_TV 环境变量,然后运行 prebuild 对项目进行 TV 修改。

🌐 Set the EXPO_TV environment variable, and run prebuild to make the TV modifications to the project.

Terminal
export EXPO_TV=1
npx expo prebuild --clean

注意:建议使用 --clean 参数,如果项目中已有 Android 和 iOS 目录,则该参数是必需的。

4

为 Android TV 构建

🌐 Build for Android TV

启动 Android TV 模拟器,并使用以下命令在模拟器上启动应用:

🌐 Start an Android TV emulator and use the following command to start the app on the emulator:

Terminal
npx expo run:android

5

为 Apple TV 构建

🌐 Build for Apple TV

运行以下命令在 Apple TV 模拟器上构建并运行应用:

🌐 Run the following command to build and run the app on an Apple TV simulator:

Terminal
npx expo run:ios

6

还原电视更改并为手机构建

🌐 Revert TV changes and build for phone

你可以通过取消设置 EXPO_TV 并再次运行预构建,将 TV 的更改恢复回手机开发:

🌐 You can revert the changes for TV and go back to phone development by unsetting EXPO_TV and running prebuild again:

Terminal
unset EXPO_TV
npx expo prebuild --clean

7

为电视和手机创建 EAS 构建配置文件

🌐 Create EAS Build profiles for both TV and phone

由于 TV 构建可以由环境变量的值驱动,因此很容易设置 EAS Build 配置文件,从相同的源代码构建,但目标是 TV 而不是手机。

🌐 Since the TV build can be driven by the value of an environment variable, it is easy to set up EAS Build profiles that build from the same source but target TV instead of phone.

下面的示例 eas.json 展示了如何扩展现有配置文件(developmentpreview)以创建电视配置文件(development_tvpreview_tv)。

🌐 The following example eas.json shows how to extend existing profiles (development and preview) to create TV profiles (development_tv and preview_tv).

eas.json
{ "cli": { "version": ">= 5.2.0" }, "build": { "base": { "distribution": "internal", "ios": { "simulator": true }, "android": { "buildType": "apk", "withoutCredentials": true }, "channel": "base" }, "development": { "extends": "base", "android": { "gradleCommand": ":app:assembleDebug" }, "ios": { "buildConfiguration": "Debug" }, "channel": "development" }, "development_tv": { "extends": "development", "env": { "EXPO_TV": "1" }, "channel": "development" }, "preview": { "extends": "base", "channel": "preview" }, "preview_tv": { "extends": "preview", "env": { "EXPO_TV": "1" }, "channel": "preview" } }, "submit": {} }

示例和演示项目

🌐 Examples and demonstration projects

点燃电视

使用 Ignite CLI 生成的项目,可用于移动设备或电视平台。

SkiaMultiplatform

展示在移动设备、电视和网页上使用 React Native Skia。

NativewindMultiplatform

演示在手机、电视和网页上使用 TailwindCSS 进行样式设计。