This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

构建电视 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.

需要本地项目更改

🌐 Native project changes required

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

🌐 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

先决条件

4 要求

1.

Node.js(长期支持版)

在 macOS 或 Linux 上安装 Node.js (LTS)

2.

Android Studio(Iguana 或更高版本)

安装 Android Studio Iguana 或更高版本。

3.

Android TV 系统镜像

在 Android Studio SDK 管理器中,选择你使用的 Android SDK 下拉菜单(API 版本 31 或更高),并确保选择了要安装的 Android TV 系统镜像。对于 Apple Silicon,选择 ARM 64 镜像。否则,选择 Intel x86_64 镜像。

4.

安卓电视模拟器

安装 Android TV 系统镜像后,使用该镜像创建一个 Android TV 模拟器(过程与创建 Android 手机模拟器相同)。

Apple TV

先决条件

3 要求

1.

Node.js(长期支持版)

在 macOS 上安装 Node.js (LTS)

2.

Xcode 16 或更高版本
安装 Xcode 16 或更高版本。

3.

tvOS SDK 17 或更高版本

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 仓库。

🌐 In package.json, modify the react-native dependency to use the TV repo.

package.json
{ %%placeholder-start%%... %%placeholder-end%% "dependencies": { %%placeholder-start%%... %%placeholder-end%% "react-native": "npm:react-native-tvos@0.85-stable" %%placeholder-start%%... %%placeholder-end%% } }

react-native-tvos 版本必须与你使用的 Expo SDK 匹配。例如,Expo SDK 56 使用 React Native 0.85,因此你应该使用 react-native-tvos@0.85-stable(最新的 0.85 版本),如上所示。有关正确使用的版本,请参阅 SDK 兼容性表

🌐 The react-native-tvos version must match the Expo SDK you are using. For example, Expo SDK 56 uses React Native 0.85, so you should use react-native-tvos@0.85-stable (the latest 0.85 version) as shown above. See the SDK compatibility table for the correct version to use.

在 SDK 56 及更高版本中,将你的项目升级到更新的 SDK 版本 也会升级 TV 仓库依赖。

🌐 In SDK 56 and later, upgrading your project to a newer SDK version also upgrades the TV repo dependency.

警告 如果你的 monorepo 中有多个 Expo 项目,其中一个项目已被修改为 TV,那么所有项目都应该按照此处描述的方式修改为使用 React Native TV 包,即使有些项目没有配置为针对 TV。这可以避免项目依赖之间可能发生的冲突,同时仍然完全支持所有项目的移动开发。

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": {} }

8

tvOS 应用商店构建的凭证

🌐 Credentials for tvOS App Store builds

tvOS 使用与 iOS 及其他 Apple 平台相同的应用包 ID 和分发证书。但是,tvOS 不使用与 iOS 相同的配置描述文件。

🌐 tvOS uses the same bundle IDs and distribution certificates as iOS and other Apple platforms. However, tvOS does not use the same provisioning profiles as iOS.

由于网络上的 EAS 工具和 EAS CLI 只创建 iOS 配置文件,因此 tvOS 项目需要一些额外的步骤。你有两种方式可以继续进行:

🌐 Since the EAS tooling on the web and in EAS CLI only creates iOS provisioning profiles, some extra steps are required for tvOS projects. There are two ways for you to proceed:

  1. 如果一个项目只针对 tvOS,你可以将凭证存储在 EAS 中。创建打包 ID,并在项目的网站上上传分发证书,就像你为 iOS 项目所做的那样。然后,你应该访问苹果开发者网站,并使用 EAS 创建的打包 ID 在那里创建一个 tvOS 配置描述文件。然后将该配置文件下载到你的电脑,并上传到项目的 你的 EAS 项目网站。参见 应用凭证指南
  2. 如果一个项目同时面向 tvOS 和 iOS,你应该为 tvOS 使用本地凭据,因为 EAS 存储的凭据将被你的 iOS 构建使用。

示例和演示项目

🌐 Examples and demonstration projects

点燃电视

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

SkiaMultiplatform

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

NativewindMultiplatform

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