为 Android TV 或 Apple TV 目标构建 Expo 应用的指南。
警告 TV 开发仅在 SDK 50 及更高版本中可用。并非所有 Expo 功能和 SDK 模块均可在电视上使用。详细信息请参见 局限性 部分。
¥TV development is available only in SDK 50 and later. Not all Expo features and SDK modules are available on TV. See the Limitations section for details.
Android TV 和 Apple TV 通过 React Native 电视项目 支持 React Native。这项技术超越了电视,提供了全面的核心存储库分支,支持手机和电视目标,包括 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.
使用 React Native TV 库作为 Expo 项目中的 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,则可以使用 配置插件 实现自动化。以下是配置插件所做的更改列表,你也可以手动应用这些更改:
¥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 修改为:
¥AndroidManifest.xml is modified:
删除默认手机纵向方向
¥The default phone portrait orientation is removed
添加了电视应用所需的意图
¥The required intent for TV apps is added
修改 MainApplication.kt 以删除不支持的 Flipper 调用
¥MainApplication.kt is modified to remove unsupported Flipper invocations
¥iOS
ios/Podfile 已修改为目标 tvOS 而不是 iOS
¥ios/Podfile is modified to target tvOS instead of iOS
Xcode 项目已修改为目标 tvOS 而不是 iOS
¥The Xcode project is modified to target tvOS instead of iOS
启动画面 (SplashScreen.storyboard) 已修改以在 tvOS 上运行
¥The splash screen (SplashScreen.storyboard) is modified to work on tvOS
¥System requirements for TV development
macOS 或 Linux 上的 Node.js (LTS)。
¥Node.js (LTS) on macOS or Linux.
Android Studio(Iguana 或更高版本)。
¥Android Studio (Iguana or later).
在 Android Studio SDK 管理器中,选择你正在使用的 Android SDK(API 版本 31 或更高版本)的下拉列表,并确保选择安装 Android TV 系统映像。(对于 Apple 芯片,请选择 ARM 64 映像。否则,请选择 Intel x86_64 映像)。
¥In the Android Studio SDK manager, select the dropdown for the Android SDK you are using (API version 31 or later), and make sure an Android TV system image is selected for installation. (For Apple silicon, choose the ARM 64 image. Otherwise, choose the Intel x86_64 image).
安装 Android TV 系统映像后,使用该映像创建 Android TV 模拟器(该过程与创建 Android 手机模拟器相同)。
¥After installing the Android TV system image, create an Android TV emulator using that image (the process is the same as creating an Android phone emulator).
macOS 上的 Node.js (LTS)。
¥Node.js (LTS) on macOS.
Xcode 15 或更高版本。
¥Xcode 15 or later.
tvOS SDK 17 或更高版本。(这不会随 Xcode 自动安装。你可以稍后使用 xcodebuild -downloadAllPlatforms
安装它)。
¥tvOS SDK 17 or later. (This is not installed automatically with Xcode. You can install it later with xcodebuild -downloadAllPlatforms
).
¥Quick start
Expo 示例存储库中的 电视示例 描述了生成新项目的最快方法:
¥The fastest way to generate a new project is described in the TV example within the Expo examples repository:
-
npx create-expo-app MyTVProject -e with-tv
对于 SDK 51 及更高版本,你可以从 电视路由示例 开始:
¥For SDK 51 and later, you can start with the TV Router example:
-
npx create-expo-app MyTVProject -e with-router-tv
这将创建一个新项目,该项目使用 Expo 路由 进行基于文件的导航,以 create-expo-app 默认模板 为模型。
¥This creates a new project that uses Expo Router for file-based navigation, modeled after the create-expo-app default template.
¥Supported modules
目前,电视应用可使用我们的 Expo API 文档中列出的以下软件包和 API。
¥At this time, TV applications work with the following packages and APIs listed in our Expo API docs.
¥Font
TV 还可以与 react-navigation、react-native-video 和许多其他常用的第三方 React Native 库配合使用。
¥TV also works with react-navigation, react-native-video, and many other commonly used third-party React Native libraries.
¥Limitations
目前不支持 Expo 开发客户端 包。
¥The Expo DevClient package is not supported at this time.
目前不支持 Expo 视频 包。电视开发者应继续使用 Expo AV 视频包。
¥The Expo Video package is not supported at this time. TV developers should continue to use the Expo AV package for video.
仅 SDK 51 及更高版本支持 Expo 路由 包。
¥The Expo Router package is only supported in SDK 51 and later.
¥Integration with an existing Expo project
如果你有使用 SDK 50 或更高版本的现有 Expo 项目,以下演练介绍了修改 TV Expo 项目所需的步骤。
¥If you have an existing Expo project using SDK 50 or later, the following walkthrough describes the steps required to modify an Expo project for TV.
信息 如果你从 SDK 49 项目开始,则需要 升级到 SDK 50 或更高版本,然后才能继续执行以下步骤。
¥If you are starting with an SDK 49 project, you will need to upgrade to SDK 50 or higher before proceeding through the following steps.
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.
{
%%placeholder-start%%... %%placeholder-end%%
"dependencies": {
%%placeholder-start%%... %%placeholder-end%%
"react-native": "npm:react-native-tvos",
%%placeholder-start%%... %%placeholder-end%%
},
"expo": {
"install": {
"exclude": [
"react-native"
]
}
}
}
2
¥Add the TV config plugin
-
npx expo install --dev @react-native-tvos/config-tv
安装后,该插件将在以下情况下修改电视项目:
¥When installed, the plugin will modify the project for TV when either:
环境变量 EXPO_TV
设置为 1
¥The environment variable EXPO_TV
is set to 1
插件参数 isTV
设置为 true
¥The plugin parameter isTV
is set to true
验证此插件是否出现在 app.json 中:
¥Verify that this plugin appears in 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.)
# 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.
-
export EXPO_TV=1
-
npx expo prebuild --clean
注意:建议使用
--clean
参数,如果项目中已有 Android 和 iOS 目录,则该参数是必需的。¥Note: The
--clean
argument is recommended, and is required if you have existing Android and iOS directories in the project.
4
¥Build for Android TV
启动 Android TV 模拟器并使用以下命令在模拟器上启动应用:
¥Start an Android TV emulator and use the following command to start the app on the emulator:
-
npx expo run:android
5
¥Build for Apple TV
运行以下命令在 Apple TV 模拟器上构建并运行应用:
¥Run the following command to build and run the app on an Apple TV simulator:
-
npx expo run:ios
6
¥Revert TV changes and build for phone
你可以通过取消设置 EXPO_TV
并再次运行预构建来恢复电视的更改并返回手机开发:
¥You can revert the changes for TV and go back to phone development by unsetting EXPO_TV
and running prebuild again:
-
unset EXPO_TV
-
npx expo prebuild --clean
7
¥Create EAS Build profiles for both TV and phone
由于电视构建是由环境变量的值驱动的,因此很容易设置 EAS 构建配置文件,这些配置文件从相同的源构建,但目标是电视而不是手机。
¥Since the TV build is 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 显示如何扩展现有配置文件(development
和 preview
)以创建电视配置文件(development_tv
和 preview_tv
)。
¥The following example eas.json shows how to extend existing profiles (development
and preview
) to create TV profiles (development_tv
and preview_tv
).
{
"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": {}
}
¥More
GitHub 上提供了完整的示例。