开发构建简介

为什么使用开发版本以及如何开始使用。


开发构建 是我们用来指代包含 expo-dev-client 库的应用 “调试” 构建的术语。这个库增强了内置的 React Native 开发工具,提供了更多功能,例如支持检查网络请求以及一个“启动器”用户界面,允许你在不同的开发服务器之间切换(例如在你的电脑或队友的电脑上运行的服务器)以及应用的不同部署版本之间切换(例如通过 EAS Update 发布的更新)。

Expo Go 与开发构建之间的区别

Expo Go 是一个供学生和学习者快速入门的试玩应用。它内置了一套固定的原生库,因此你可以编写 JavaScript 代码并即时看到更改,而无需自己构建原生应用。开发构建是一个功能齐全的开发环境,用于开发你的生产级 Expo 应用。

原生应用和 JavaScript 包

原生应用是你安装在设备上的应用。Expo Go 是一个预先构建的原生应用,它就像一个在线运行——安装后无法更改。要添加新的原生库或更改应用名称和图标等内容,你需要构建自己的原生应用(开发版构建)。

🌐 The native app is what you install on your device. Expo Go is a pre-built native app that works like a playground — it can't be changed after you install it. To add new native libraries or change things like your app name and icon, you need to build your own native app (a development build).

JavaScript 打包文件(npx expo start 是存放你应用的 UI 代码和业务逻辑的地方。在生产环境的应用中,会有一个 main.js 打包文件随应用一起发布。在开发环境中,这个 JS 打包文件会从你的本地机器进行实时重载。React Native 的主要作用是为 JavaScript 代码提供访问原生 API(如图片、相机、通知等)的方式。然而,只有那些已被打包进 原生应用 的 API 和库才能被使用。

🌐 The JavaScript bundle (npx expo start) is where your app's UI code and business logic are. In production apps, there is one main.js bundle that is shipped with the app itself. In development, this JS bundle is live reloaded from your local machine. The main role of React Native is to provide a way for the JavaScript code to access the native APIs (Image, Camera, Notifications, and more). However, only APIs and libraries that were bundled in the native app can be used.

Expo Go 与开发构建:你应该使用哪一个?
Expo Go 与开发构建:你应该使用哪一个?

在本教程视频中,Beto 解释了它们各自的作用以及何时选择开发版本。

为什么使用开发版本(也就是在 Expo Go 中 不能 做什么以及原因)

🌐 Why use a development build (a.k.a what can't you do in Expo Go and why)

Expo Go 是一个供学生和学习者理解 React Native 基础的练习平台。它功能有限,不适合构建生产级项目,因此大多数应用会转换为使用开发构建版本。了解在 Expo Go 中哪些是 不可能 的以及 为什么 很有帮助,这样你就可以在何时以及为何进行此转换时做出明智的决定。

🌐 Expo Go is a playground for students and learners to understand the basics of React Native. It's limited and not useful for building production-grade projects, so most apps will convert to using development builds. It helps to know exactly what is impossible in Expo Go and why, so you can make an informed decision on when and why to make this move.

使用在 Expo Go 中没有的本地代码库

react-native-webview为例,这是一个包含原生代码的库,但已包含在 Expo Go 中。当你在项目中运行 npx expo install react-native-webview 命令时,它会在你的 node_modules 目录中安装该库,其中包含 JS 代码和原生代码。但是,你正在构建的 JS 包_只_使用 JS 代码。然后,你的 JS 包会上传到 Expo Go,并与应用中已经打包的原生代码进行交互。

🌐 Consider react-native-webview as an example, a library that contains native code, but is included in Expo Go. When you run npx expo install react-native-webview command in your project, it will install the library in your node_modules directory, which includes both the JS code and the native code. But the JS bundle you are building only uses the JS code. Then, your JS bundle gets uploaded to Expo Go, and it interacts with the native code that was already bundled with the app.

相反,当你尝试使用一个未包含的库时,例如 react-native-firebase,你可以使用 JS 代码并将新的 bundle 热加载到 Expo Go 中,但它会立即报错,因为 JS 代码尝试调用 React Native Firebase 包中的本地代码,而这些代码在 Expo Go 中不存在。除非本地代码已经包含在上传到应用商店的 bundle 中,否则无法将本地代码引入到 Expo Go 应用中。

🌐 Instead, when you try to use a library that is not included, for example, react-native-firebase, then you can use the JS code and hot reload the new bundle into Expo Go but it will immediately error because the JS code tries to call the native code from the React Native Firebase package that does not exist in Expo Go. There is no way to get the native code into the Expo Go app unless it was already included in the bundle that was uploaded to the app stores.

测试应用图标、名称和启动屏幕的更改

如果你只在 Expo Go 中开发你的应用,你可以构建一个商店版本,它将使用你提供的值和图片;只是不能在 Expo Go 中进行测试。

🌐 If you're developing your app in Expo Go only, you can build a store version that will use your provided values and images; it just won't be possible to test it in Expo Go.

这些本地资源随本地包一起提供,并且在应用安装后不可更改。Expo Go 应用会显示启动屏幕,即带有纯色背景的应用图标。这只是开发者专用的模拟功能,用于查看启动屏幕可能的显示效果。然而,这种模拟是有限的,例如,你无法测试 SplashScreen.setOptions 来为启动屏幕添加动画。

🌐 These native assets are shipped with the native bundle and are immutable once the app is installed. The Expo Go app does show a splash screen, which is your app icon on a solid color background. This is a dev-only emulation to view how the splash screen will probably look. However, it is limited, for example, you cannot test SplashScreen.setOptions to animate the splash screen.

远程推送通知

虽然 应用内通知 在 Expo Go 中可用,但远程推送通知(即从服务器向应用发送推送通知)不可用。这是因为推送通知服务应该绑定到你自己的推送通知证书上,虽然在 Expo Go 中可以使其工作,但通常会在生产版本中引起混淆。建议在开发版本中测试远程推送通知,以确保开发和生产环境中的行为一致。

🌐 While in-app notifications are available in Expo Go, remote push notifications (that is, sending a push notification from a server to the app) are not. This is because a push notification service should be tied to your own push notification certificates, and while it is possible to make it work in Expo Go, it often causes confusion for production builds. It is recommended to test remote push notifications in development builds so you can ensure parity in behavior between development and production.

实现应用/通用链接

无论是 Android 应用链接 还是 iOS 通用链接,都需要本地应用与网站之间的双向关联。尤其是,它要求本地应用包含所链接网站的 URL。由于前面提到的本地代码不可变性,这在 Expo Go 中是不可能实现的。

🌐 Both Android App Links and iOS Universal Links require a two-way association between the native app and the website. In particular, it requires the native app to include the linked website's URL. This is impossible with Expo Go due to the aforementioned native code immutability.

使用旧版 SDK 打开项目(仅限 iOS 设备)

Expo Go 一次只能支持一个 SDK 版本。当发布新版本的 SDK 时,Expo Go 会进行更新以支持较新的版本,而这是从应用商店安装的唯一可用版本。

🌐 Expo Go can only support one SDK version at a time. When a new SDK version is released, Expo Go is updated to support the newer version, and this will be the only version of Expo Go available to install from the stores.

如果你在 Android 设备、Android 模拟器或 iOS 模拟器上开发,可以下载并安装 兼容版本的 Expo Go。唯一无法做到这一点的平台是 iPhone,因为苹果不支持对老版本应用进行侧加载。

🌐 If you're developing on an Android Device, Android Emulator, or iOS Simulator, a compatible version of Expo Go can be downloaded and installed. The only platform where this is impossible is iPhone devices because Apple does not support side-loading older versions of apps.

Expo 转到开发版本

学习如何将现有的 Expo Go 项目迁移到使用开发构建

本地应用开发

如何在本地机器上构建开发客户端

基于 EAS 的开发版本

如何在 EAS 上构建开发客户端