# Expo Documentation > Expo is an open-source React Native framework for apps that run natively on Android, iOS, and the web. Expo brings together the best of mobile and the web and enables many important features for building and scaling an app such as live updates, instantly sharing your app, and web support. The company behind Expo also offers Expo Application Services (EAS), which are deeply integrated cloud services for Expo and React Native apps. --- title: 创建一个项目 description: 了解如何创建一个新的 Expo 项目。 --- ## Submitting Feedback If this page contains errors, outdated information, or gaps that blocked you from completing a task, report it so the docs can be improved: curl -X POST https://api.expo.dev/v2/feedback/docs-send -H 'Content-Type: application/json' -d '{"url":"/get-started/create-a-project/","feedback":"🤖 Agent feedback: "}' Only submit when you have something specific and actionable to report. # 创建一个项目 了解如何创建一个新的 Expo 项目。 > For the complete documentation index, see [llms.txt](/llms.txt). Use this Use this file to discover all available pages. Expo 是一个 React Native 框架,它使开发 Android 和 iOS 应用变得更容易。我们的框架提供基于文件的路由、原生模块标准库等更多功能。Expo 是开源的,在 [GitHub](https://github.com/expo/expo) 和 [Discord](https://chat.expo.dev) 上拥有活跃的社区。 我们还提供 [Expo Application Services (EAS)](https://expo.dev/eas),这是一组在开发过程的每个步骤中补充 Expo 框架的服务。 ## 系统要求 - [Node.js (LTS)](https://nodejs.org/en/)。 - 支持 macOS、Windows(Powershell 和 [WSL 2](https://expo.fyi/wsl))以及 Linux。 我们建议从 `create-expo-app` 创建的默认项目开始。默认项目包含示例代码,可帮助你快速上手。 要创建新项目,请运行以下命令: ```sh npx create-expo-app@latest --template default@sdk-55 ``` > **注意:** 在 SDK 55 过渡期间,不带 `--template` 标志的 `create-expo-app@latest` 会创建一个 SDK 54 项目。如果你计划在实体设备上使用 Expo Go,请使用 SDK 54 项目。否则,请使用 `--template default@sdk-55` 创建 SDK 55 项目。你也可以通过添加 [`--template` 选项](/more/create-expo#--template) 选择其他模板。 ## 下一步 你已经有了一个项目。现在是时候设置你的开发环境,这样你就可以开始开发了。 --- --- title: 设置你的环境 description: 了解如何设置你的开发环境,以开始使用 Expo 构建。 --- ## Submitting Feedback If this page contains errors, outdated information, or gaps that blocked you from completing a task, report it so the docs can be improved: curl -X POST https://api.expo.dev/v2/feedback/docs-send -H 'Content-Type: application/json' -d '{"url":"/get-started/set-up-your-environment/","feedback":"🤖 Agent feedback: "}' Only submit when you have something specific and actionable to report. # 设置你的环境 了解如何设置你的开发环境,以开始使用 Expo 构建。 > For the complete documentation index, see [llms.txt](/llms.txt). Use this Use this file to discover all available pages. 让我们为在 Android 和 iOS 上运行你的项目设置一个本地开发环境。 ## 你想在哪里进行开发? 我们建议使用真机进行开发,因为这样你可以准确看到用户将看到的内容。 ## 你想如何进行开发? Expo Go 是为学生和学习者准备的一个试验场,可用于快速体验 Expo。开发构建是你自己的应用的构建版本,其中包含 Expo 的开发者工具。 ## Android device with Expo Go ### Set up an Android device with Expo Go Scan the QR code to download the app from the Google Play Store, or visit the Expo Go page on the [Google Play Store](https://play.google.com/store/apps/details?id=host.exp.exponent&referrer=docs). Download link: [https://play.google.com/store/apps/details?id=host.exp.exponent&referrer=docs](https://play.google.com/store/apps/details?id=host.exp.exponent&referrer=docs) --- ## Android device with a development build (EAS) ### Set up an Android device with a development build #### Install EAS CLI To build your app, you will need to install EAS CLI. You can do this by running the following command in your terminal: ```sh npm install -g eas-cli ``` #### Create an Expo account and login To build your app, you will need to create an Expo account and login to the EAS CLI. 1. [Sign up](https://expo.dev/signup) for an Expo account. 2. Run the following command in your terminal to log in to the EAS CLI: ```sh eas login ``` #### Configure your project Run the following command to create an EAS config in your project: ```sh eas build:configure ``` #### Create a build Run the following command to create a development build: ```sh eas build --platform android --profile development ``` #### Install the development build on your device After the build is complete, scan the QR code in your terminal or open the link on your device. Tap **Install** to download the build on your device, then tap **Open** to install it. --- ## Android device with a development build (local) ### Set up an Android device with a development build ### Install Watchman and JDK ##### macOS ##### Prerequisites Use a package manager such as [Homebrew](https://brew.sh/) to install the following dependency. ##### Install dependencies [Install Watchman](https://facebook.github.io/watchman/docs/install#macos) using a tool such as Homebrew: ```sh brew install watchman ``` Install OpenJDK distribution called Azul Zulu using Homebrew. This distribution offers JDKs for both Apple Silicon and Intel Macs. Run the following commands in a terminal: ```sh brew install --cask zulu@17 ``` After you install the JDK, add the `JAVA_HOME` environment variable in **~/.bash_profile** (or **~/.zshrc** if you use Zsh): ```bash export JAVA_HOME=/Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home ``` ##### Windows ##### Prerequisites Use a package manager such as [Chocolatey](https://chocolatey.org/) to install the following dependencies. ##### Install dependencies Install [Java SE Development Kit (JDK)](https://openjdk.org/): ```sh choco install -y microsoft-openjdk17 ``` ##### Linux ##### Install dependencies Follow [instructions from the Watchman documentation](https://facebook.github.io/watchman/docs/install#linux) to compile and install it from the source. Install [Java SE Development Kit (JDK)](https://openjdk.org/): You can download and install [OpenJDK@17](http://openjdk.java.net/) from [AdoptOpenJDK](https://adoptopenjdk.net/) or your system packager. ### Set up Android Studio ##### macOS Download and install [Android Studio](https://developer.android.com/studio). Open the **Android Studio** app, you will see the **SDK Components setup** screen. Click **Next** to continue to install the Android SDK and Android SDK Platform. Click **Next** again to verify the settings and install. By default, Android Studio will install the latest version of the Android SDK. However, Android 15 (`VanillaIceCream`) SDK is required to compile a React Native app. Open Android Studio, go to **Settings** > **Languages & Frameworks** > **Android SDK**. From the **SDK Platforms** tab, and under **Android 15 (`VanillaIceCream`)**, select **Android SDK Platform 35** and **Sources for Android 35**. Then, click on the **SDK Tools** tab and make sure you have at least one version of the **Android SDK Build-Tools** and **Android Emulator** installed. Copy or remember the path listed in the box that says **Android SDK Location**. Add the following lines to your **/.zprofile** or **~/.zshrc** (if you are using bash, then **~/.bash_profile** or **~/.bashrc**) config file: ```sh export ANDROID_HOME=$HOME/Library/Android/sdk export PATH=$PATH:$ANDROID_HOME/emulator export PATH=$PATH:$ANDROID_HOME/platform-tools ``` Reload the path environment variables in your current shell: ```sh source $HOME/.zshrc source $HOME/.bashrc ``` Finally, make sure that you can run `adb` from your terminal. **Troubleshooting: Android Studio not recognizing JDK** If Android Studio doesn't recognize your homebrew installed JDK, you can create a Gradle configuration file to explicitly set the Java path: 1. Create a Gradle properties file in your home directory: ```sh touch ~/.gradle/gradle.properties ``` 2. Add the following line to the **gradle.properties** file, replacing the path with your actual Java installation path: ```bash gradle.properties java.home=/Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home ``` 3. If you have an existing `.gradle` folder in your project directory, delete it and reopen your project in Android Studio: ```sh rm -rf .gradle ``` This should resolve issues with Android Studio not detecting your JDK installation. ##### Windows Download [Android Studio](https://developer.android.com/studio). Open **Android Studio Setup**. Under **Select components to install**, select Android Studio and Android Virtual Device. Then, click **Next**. In the Android Studio Setup Wizard, under **Install Type**, select **Standard** and click **Next**. The Android Studio Setup Wizard will ask you to verify the settings, such as the version of Android SDK, platform-tools, and so on. Click **Next** after you have verified. In the next window, accept licenses for all available components. By default, Android Studio will install the latest version of the Android SDK. However, Android 15 (`VanillaIceCream`) SDK is required to compile a React Native app. Open Android Studio, go to **Settings** > **Languages & Frameworks** > **Android SDK**. From the **SDK Platforms** tab, and under **Android 15 (`VanillaIceCream`)**, select **Android SDK Platform 35** and **Sources for Android 35**. Then, click on the **SDK Tools** tab and make sure you have at least one version of the **Android SDK Build-Tools** and **Android Emulator** installed. After the tools installation is complete, configure the `ANDROID_HOME` environment variable. Go to **Windows Control Panel** > **User Accounts** > **User Accounts** (again) > **Change my environment variables** and click **New** to create a new `ANDROID_HOME` user variable. The value of this variable will point to the path to your Android SDK: **How to find installed SDK location?** By default, the Android SDK is installed at the following location: ```bash %LOCALAPPDATA%\Android\Sdk ``` To find the location of the SDK in Android Studio manually, go to **Settings** > **Languages & Frameworks** > **Android SDK**. See the location next to **Android SDK Location**. To verify that the new environment variable is loaded, open **PowerShell**, and copy and paste the following command: ```sh Get-ChildItem -Path Env: ``` The command will output all user environment variables. In this list, see if `ANDROID_HOME` has been added. To add platform-tools to the Path, go to **Windows Control Panel** > **User Accounts** > **User Accounts** (again) > **Change my environment variables** > **Path** > **Edit** > **New** and add the path to the platform-tools to the list as shown below: **How to find installed platform-tools location** By default, the platform-tools are installed at the following location: ```bash %LOCALAPPDATA%\Android\Sdk\platform-tools ``` Finally, make sure that you can run `adb` from the PowerShell. For example, run the `adb --version` to see which version of the `adb` your system is running. ### Running your app on an Android device #### Install expo-dev-client Run the following command in your project's root directory: ```sh npx expo install expo-dev-client ``` #### Enable debugging over USB Most Android devices can only install and run apps downloaded from Google Play, by default. You will need to enable USB Debugging on your device to install your app during development. To enable USB debugging on your device, you will first need to enable the "Developer options" menu by going to **Settings** > **About phone** > **Software information** and then tapping the `Build number` row at the bottom seven times. You can then go back to **Settings** > **Developer options** to enable "USB debugging". #### Plug in your device via USB Plug in your Android device via USB to your computer. Check that your device is properly connecting to ADB, the Android Debug Bridge, by running `adb devices` in your terminal. You should see your device listed with `device` listed next to it. For example: ```sh adb devices List of devices attached 8AHX0T32K device ``` #### Run your app Run the following from your terminal: ```sh npx expo run:android ``` > This command runs a development server after building your app. You can skip running `npx expo start` on the next page. --- ## Android Emulator with Expo Go ### Set up an Android Emulator with Expo Go ### Set up Android Studio ##### macOS Download and install [Android Studio](https://developer.android.com/studio). Open the **Android Studio** app, you will see the **SDK Components setup** screen. Click **Next** to continue to install the Android SDK and Android SDK Platform. Click **Next** again to verify the settings and install. By default, Android Studio will install the latest version of the Android SDK. However, Android 15 (`VanillaIceCream`) SDK is required to compile a React Native app. Open Android Studio, go to **Settings** > **Languages & Frameworks** > **Android SDK**. From the **SDK Platforms** tab, and under **Android 15 (`VanillaIceCream`)**, select **Android SDK Platform 35** and **Sources for Android 35**. Then, click on the **SDK Tools** tab and make sure you have at least one version of the **Android SDK Build-Tools** and **Android Emulator** installed. Copy or remember the path listed in the box that says **Android SDK Location**. Add the following lines to your **/.zprofile** or **~/.zshrc** (if you are using bash, then **~/.bash_profile** or **~/.bashrc**) config file: ```sh export ANDROID_HOME=$HOME/Library/Android/sdk export PATH=$PATH:$ANDROID_HOME/emulator export PATH=$PATH:$ANDROID_HOME/platform-tools ``` Reload the path environment variables in your current shell: ```sh source $HOME/.zshrc source $HOME/.bashrc ``` Finally, make sure that you can run `adb` from your terminal. **Troubleshooting: Android Studio not recognizing JDK** If Android Studio doesn't recognize your homebrew installed JDK, you can create a Gradle configuration file to explicitly set the Java path: 1. Create a Gradle properties file in your home directory: ```sh touch ~/.gradle/gradle.properties ``` 2. Add the following line to the **gradle.properties** file, replacing the path with your actual Java installation path: ```bash gradle.properties java.home=/Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home ``` 3. If you have an existing `.gradle` folder in your project directory, delete it and reopen your project in Android Studio: ```sh rm -rf .gradle ``` This should resolve issues with Android Studio not detecting your JDK installation. ##### Windows Download [Android Studio](https://developer.android.com/studio). Open **Android Studio Setup**. Under **Select components to install**, select Android Studio and Android Virtual Device. Then, click **Next**. In the Android Studio Setup Wizard, under **Install Type**, select **Standard** and click **Next**. The Android Studio Setup Wizard will ask you to verify the settings, such as the version of Android SDK, platform-tools, and so on. Click **Next** after you have verified. In the next window, accept licenses for all available components. By default, Android Studio will install the latest version of the Android SDK. However, Android 15 (`VanillaIceCream`) SDK is required to compile a React Native app. Open Android Studio, go to **Settings** > **Languages & Frameworks** > **Android SDK**. From the **SDK Platforms** tab, and under **Android 15 (`VanillaIceCream`)**, select **Android SDK Platform 35** and **Sources for Android 35**. Then, click on the **SDK Tools** tab and make sure you have at least one version of the **Android SDK Build-Tools** and **Android Emulator** installed. After the tools installation is complete, configure the `ANDROID_HOME` environment variable. Go to **Windows Control Panel** > **User Accounts** > **User Accounts** (again) > **Change my environment variables** and click **New** to create a new `ANDROID_HOME` user variable. The value of this variable will point to the path to your Android SDK: **How to find installed SDK location?** By default, the Android SDK is installed at the following location: ```bash %LOCALAPPDATA%\Android\Sdk ``` To find the location of the SDK in Android Studio manually, go to **Settings** > **Languages & Frameworks** > **Android SDK**. See the location next to **Android SDK Location**. To verify that the new environment variable is loaded, open **PowerShell**, and copy and paste the following command: ```sh Get-ChildItem -Path Env: ``` The command will output all user environment variables. In this list, see if `ANDROID_HOME` has been added. To add platform-tools to the Path, go to **Windows Control Panel** > **User Accounts** > **User Accounts** (again) > **Change my environment variables** > **Path** > **Edit** > **New** and add the path to the platform-tools to the list as shown below: **How to find installed platform-tools location** By default, the platform-tools are installed at the following location: ```bash %LOCALAPPDATA%\Android\Sdk\platform-tools ``` Finally, make sure that you can run `adb` from the PowerShell. For example, run the `adb --version` to see which version of the `adb` your system is running. ### Set up an emulator On the Android Studio main screen, click **More Actions**, then **Virtual Device Manager** in the dropdown. Click the **Create device** button. Under **Add device**, choose the type of hardware you'd like to emulate. We recommend testing against a variety of devices, but if you're unsure where to start, the newest device in the Pixel line could be a good choice. Select an OS version to load on the emulator (probably one of the system images), and download the image (if required). Change any other settings you'd like, and press **Finish** to create the emulator. You can now run this emulator anytime by pressing the Play button in the AVD Manager window. ### Install Expo Go When you start a development server with `npx expo start` on the [start developing](/get-started/start-developing) page, press a to open the Android Emulator. Expo CLI will install Expo Go automatically. --- ## Android Emulator with a development build (EAS) ### Set up an Android Emulator with a development build ### Set up Android Studio ##### macOS Download and install [Android Studio](https://developer.android.com/studio). Open the **Android Studio** app, you will see the **SDK Components setup** screen. Click **Next** to continue to install the Android SDK and Android SDK Platform. Click **Next** again to verify the settings and install. By default, Android Studio will install the latest version of the Android SDK. However, Android 15 (`VanillaIceCream`) SDK is required to compile a React Native app. Open Android Studio, go to **Settings** > **Languages & Frameworks** > **Android SDK**. From the **SDK Platforms** tab, and under **Android 15 (`VanillaIceCream`)**, select **Android SDK Platform 35** and **Sources for Android 35**. Then, click on the **SDK Tools** tab and make sure you have at least one version of the **Android SDK Build-Tools** and **Android Emulator** installed. Copy or remember the path listed in the box that says **Android SDK Location**. Add the following lines to your **/.zprofile** or **~/.zshrc** (if you are using bash, then **~/.bash_profile** or **~/.bashrc**) config file: ```sh export ANDROID_HOME=$HOME/Library/Android/sdk export PATH=$PATH:$ANDROID_HOME/emulator export PATH=$PATH:$ANDROID_HOME/platform-tools ``` Reload the path environment variables in your current shell: ```sh source $HOME/.zshrc source $HOME/.bashrc ``` Finally, make sure that you can run `adb` from your terminal. **Troubleshooting: Android Studio not recognizing JDK** If Android Studio doesn't recognize your homebrew installed JDK, you can create a Gradle configuration file to explicitly set the Java path: 1. Create a Gradle properties file in your home directory: ```sh touch ~/.gradle/gradle.properties ``` 2. Add the following line to the **gradle.properties** file, replacing the path with your actual Java installation path: ```bash gradle.properties java.home=/Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home ``` 3. If you have an existing `.gradle` folder in your project directory, delete it and reopen your project in Android Studio: ```sh rm -rf .gradle ``` This should resolve issues with Android Studio not detecting your JDK installation. ##### Windows Download [Android Studio](https://developer.android.com/studio). Open **Android Studio Setup**. Under **Select components to install**, select Android Studio and Android Virtual Device. Then, click **Next**. In the Android Studio Setup Wizard, under **Install Type**, select **Standard** and click **Next**. The Android Studio Setup Wizard will ask you to verify the settings, such as the version of Android SDK, platform-tools, and so on. Click **Next** after you have verified. In the next window, accept licenses for all available components. By default, Android Studio will install the latest version of the Android SDK. However, Android 15 (`VanillaIceCream`) SDK is required to compile a React Native app. Open Android Studio, go to **Settings** > **Languages & Frameworks** > **Android SDK**. From the **SDK Platforms** tab, and under **Android 15 (`VanillaIceCream`)**, select **Android SDK Platform 35** and **Sources for Android 35**. Then, click on the **SDK Tools** tab and make sure you have at least one version of the **Android SDK Build-Tools** and **Android Emulator** installed. After the tools installation is complete, configure the `ANDROID_HOME` environment variable. Go to **Windows Control Panel** > **User Accounts** > **User Accounts** (again) > **Change my environment variables** and click **New** to create a new `ANDROID_HOME` user variable. The value of this variable will point to the path to your Android SDK: **How to find installed SDK location?** By default, the Android SDK is installed at the following location: ```bash %LOCALAPPDATA%\Android\Sdk ``` To find the location of the SDK in Android Studio manually, go to **Settings** > **Languages & Frameworks** > **Android SDK**. See the location next to **Android SDK Location**. To verify that the new environment variable is loaded, open **PowerShell**, and copy and paste the following command: ```sh Get-ChildItem -Path Env: ``` The command will output all user environment variables. In this list, see if `ANDROID_HOME` has been added. To add platform-tools to the Path, go to **Windows Control Panel** > **User Accounts** > **User Accounts** (again) > **Change my environment variables** > **Path** > **Edit** > **New** and add the path to the platform-tools to the list as shown below: **How to find installed platform-tools location** By default, the platform-tools are installed at the following location: ```bash %LOCALAPPDATA%\Android\Sdk\platform-tools ``` Finally, make sure that you can run `adb` from the PowerShell. For example, run the `adb --version` to see which version of the `adb` your system is running. ### Set up an emulator On the Android Studio main screen, click **More Actions**, then **Virtual Device Manager** in the dropdown. Click the **Create device** button. Under **Add device**, choose the type of hardware you'd like to emulate. We recommend testing against a variety of devices, but if you're unsure where to start, the newest device in the Pixel line could be a good choice. Select an OS version to load on the emulator (probably one of the system images), and download the image (if required). Change any other settings you'd like, and press **Finish** to create the emulator. You can now run this emulator anytime by pressing the Play button in the AVD Manager window. ### Create a development build #### Install EAS CLI To build your app, you will need to install EAS CLI. You can do this by running the following command in your terminal: ```sh npm install -g eas-cli ``` #### Create an Expo account and login To build your app, you will need to create an Expo account and login to the EAS CLI. 1. [Sign up](https://expo.dev/signup) for an Expo account. 2. Run the following command in your terminal to log in to the EAS CLI: ```sh eas login ``` #### Configure your project Run the following command to create an EAS config in your project: ```sh eas build:configure ``` #### Create a build Run the following command to create a development build: ```sh eas build --platform android --profile development ``` #### Install the development build on your emulator After the build is complete, the CLI will prompt you to automatically download and install it on the Android Emulator. When prompted, press Y to directly install it on the emulator. If you miss this prompt, you can download the build from the link provided in the terminal and drag and drop it onto the Android Emulator to install it. --- ## Android Emulator with a development build (local) ### Set up an Android Emulator with a development build ### Install Watchman and JDK ##### macOS ##### Prerequisites Use a package manager such as [Homebrew](https://brew.sh/) to install the following dependency. ##### Install dependencies [Install Watchman](https://facebook.github.io/watchman/docs/install#macos) using a tool such as Homebrew: ```sh brew install watchman ``` Install OpenJDK distribution called Azul Zulu using Homebrew. This distribution offers JDKs for both Apple Silicon and Intel Macs. Run the following commands in a terminal: ```sh brew install --cask zulu@17 ``` After you install the JDK, add the `JAVA_HOME` environment variable in **~/.bash_profile** (or **~/.zshrc** if you use Zsh): ```bash export JAVA_HOME=/Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home ``` ##### Windows ##### Prerequisites Use a package manager such as [Chocolatey](https://chocolatey.org/) to install the following dependencies. ##### Install dependencies Install [Java SE Development Kit (JDK)](https://openjdk.org/): ```sh choco install -y microsoft-openjdk17 ``` ##### Linux ##### Install dependencies Follow [instructions from the Watchman documentation](https://facebook.github.io/watchman/docs/install#linux) to compile and install it from the source. Install [Java SE Development Kit (JDK)](https://openjdk.org/): You can download and install [OpenJDK@17](http://openjdk.java.net/) from [AdoptOpenJDK](https://adoptopenjdk.net/) or your system packager. ### Set up Android Studio ##### macOS Download and install [Android Studio](https://developer.android.com/studio). Open the **Android Studio** app, you will see the **SDK Components setup** screen. Click **Next** to continue to install the Android SDK and Android SDK Platform. Click **Next** again to verify the settings and install. By default, Android Studio will install the latest version of the Android SDK. However, Android 15 (`VanillaIceCream`) SDK is required to compile a React Native app. Open Android Studio, go to **Settings** > **Languages & Frameworks** > **Android SDK**. From the **SDK Platforms** tab, and under **Android 15 (`VanillaIceCream`)**, select **Android SDK Platform 35** and **Sources for Android 35**. Then, click on the **SDK Tools** tab and make sure you have at least one version of the **Android SDK Build-Tools** and **Android Emulator** installed. Copy or remember the path listed in the box that says **Android SDK Location**. Add the following lines to your **/.zprofile** or **~/.zshrc** (if you are using bash, then **~/.bash_profile** or **~/.bashrc**) config file: ```sh export ANDROID_HOME=$HOME/Library/Android/sdk export PATH=$PATH:$ANDROID_HOME/emulator export PATH=$PATH:$ANDROID_HOME/platform-tools ``` Reload the path environment variables in your current shell: ```sh source $HOME/.zshrc source $HOME/.bashrc ``` Finally, make sure that you can run `adb` from your terminal. **Troubleshooting: Android Studio not recognizing JDK** If Android Studio doesn't recognize your homebrew installed JDK, you can create a Gradle configuration file to explicitly set the Java path: 1. Create a Gradle properties file in your home directory: ```sh touch ~/.gradle/gradle.properties ``` 2. Add the following line to the **gradle.properties** file, replacing the path with your actual Java installation path: ```bash gradle.properties java.home=/Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home ``` 3. If you have an existing `.gradle` folder in your project directory, delete it and reopen your project in Android Studio: ```sh rm -rf .gradle ``` This should resolve issues with Android Studio not detecting your JDK installation. ##### Windows Download [Android Studio](https://developer.android.com/studio). Open **Android Studio Setup**. Under **Select components to install**, select Android Studio and Android Virtual Device. Then, click **Next**. In the Android Studio Setup Wizard, under **Install Type**, select **Standard** and click **Next**. The Android Studio Setup Wizard will ask you to verify the settings, such as the version of Android SDK, platform-tools, and so on. Click **Next** after you have verified. In the next window, accept licenses for all available components. By default, Android Studio will install the latest version of the Android SDK. However, Android 15 (`VanillaIceCream`) SDK is required to compile a React Native app. Open Android Studio, go to **Settings** > **Languages & Frameworks** > **Android SDK**. From the **SDK Platforms** tab, and under **Android 15 (`VanillaIceCream`)**, select **Android SDK Platform 35** and **Sources for Android 35**. Then, click on the **SDK Tools** tab and make sure you have at least one version of the **Android SDK Build-Tools** and **Android Emulator** installed. After the tools installation is complete, configure the `ANDROID_HOME` environment variable. Go to **Windows Control Panel** > **User Accounts** > **User Accounts** (again) > **Change my environment variables** and click **New** to create a new `ANDROID_HOME` user variable. The value of this variable will point to the path to your Android SDK: **How to find installed SDK location?** By default, the Android SDK is installed at the following location: ```bash %LOCALAPPDATA%\Android\Sdk ``` To find the location of the SDK in Android Studio manually, go to **Settings** > **Languages & Frameworks** > **Android SDK**. See the location next to **Android SDK Location**. To verify that the new environment variable is loaded, open **PowerShell**, and copy and paste the following command: ```sh Get-ChildItem -Path Env: ``` The command will output all user environment variables. In this list, see if `ANDROID_HOME` has been added. To add platform-tools to the Path, go to **Windows Control Panel** > **User Accounts** > **User Accounts** (again) > **Change my environment variables** > **Path** > **Edit** > **New** and add the path to the platform-tools to the list as shown below: **How to find installed platform-tools location** By default, the platform-tools are installed at the following location: ```bash %LOCALAPPDATA%\Android\Sdk\platform-tools ``` Finally, make sure that you can run `adb` from the PowerShell. For example, run the `adb --version` to see which version of the `adb` your system is running. ### Set up an emulator On the Android Studio main screen, click **More Actions**, then **Virtual Device Manager** in the dropdown. Click the **Create device** button. Under **Add device**, choose the type of hardware you'd like to emulate. We recommend testing against a variety of devices, but if you're unsure where to start, the newest device in the Pixel line could be a good choice. Select an OS version to load on the emulator (probably one of the system images), and download the image (if required). Change any other settings you'd like, and press **Finish** to create the emulator. You can now run this emulator anytime by pressing the Play button in the AVD Manager window. ### Running your app on an Android Emulator #### Install expo-dev-client Run the following command in your project's root directory: ```sh npx expo install expo-dev-client ``` Run the following from your terminal: ```sh npx expo run:android ``` > This command runs a development server after building your app. You can skip running `npx expo start` on the next page. --- ## iOS device with Expo Go ### Set up an iOS device with Expo Go #### Enroll in the Apple Developer Program To install Expo Go on your iOS device, you will need an active subscription to the Apple Developer Program. Sign up for the [Apple Developer Program here](https://developer.apple.com/programs/). #### Build Expo Go for iOS Run the following command to build Expo Go: ```sh npx eas-cli@latest go ``` #### Install TestFlight Download and install the [TestFlight app](https://apps.apple.com/us/app/testflight/id899247664). You can also scan the QR code below on your iOS device: Download link: [https://apps.apple.com/us/app/testflight/id899247664](https://apps.apple.com/us/app/testflight/id899247664) #### Add yourself as a tester 1. Go to [App Store Connect](https://appstoreconnect.apple.com). 2. Select the Expo Go app. 3. Navigate to the "TestFlight" tab. 4. Add your Apple ID email as an internal tester. Once you do, you should receive an email invitation to join the TestFlight beta. When you accept the invitation, you can install Expo Go on your iOS device. --- ## iOS device with a development build (EAS) ### Set up an iOS device with a development build #### Enroll in the Apple Developer Program To install a development build on your iOS device, you will need an active subscription to the Apple Developer Program. Sign up for the [Apple Developer Program here](https://developer.apple.com/programs/). #### Install EAS CLI To build your app, you will need to install EAS CLI. You can do this by running the following command in your terminal: ```sh npm install -g eas-cli ``` #### Create an Expo account and login Next, you will need to create an Expo account and login to the EAS CLI. 1. [Sign up](https://expo.dev/signup) for an Expo account. 2. Run the following command in your terminal to log in to the EAS CLI: ```sh eas login ``` #### Configure your project Run the following command to create an EAS config in your project: ```sh eas build:configure ``` #### Create an ad hoc provisioning profile To install a development build on your iOS device, you will need to create an ad hoc provisioning profile. Create one by running the following command in your terminal: ```sh eas device:create ``` #### Create a development build Run the following command to create a development build: ```sh eas build --platform ios --profile development ``` #### Install the development build on your device After the build is complete, scan the QR code in your terminal and tap **Open with iTunes** when it appears inside the Camera app. Alternatively, open the link displayed in the terminal on your device. After confirming the installation, the app will appear in your device's app library. #### Turn on developer mode 1. Open **Settings** > **Privacy & Security**, scroll down to the **Developer Mode** list item and navigate into it. 2. Tap the switch to enable **Developer Mode**. After you do so, Settings presents an alert to warn you that Developer Mode reduces your device's security. To continue enabling **Developer Mode**, tap the alert's **Restart** button. 3. After the device restarts and you unlock it, the device shows an alert confirming that you want to enable Developer Mode. Tap **Turn On**, and enter your device passcode when prompted. > Alternatively, if you have Xcode installed on your Mac, you can use it to [enable iOS developer mode](/guides/ios-developer-mode/#connect-an-ios-device-with-a-mac). --- ## iOS device with a development build (local) ### Set up an iOS device with a development build ### Set up Xcode and Watchman #### Install Xcode Open up the Mac App Store, search for [Xcode](https://apps.apple.com/us/app/xcode/id497799835), and click **Install** (or **Update** if you have it already). #### Install Xcode Command Line Tools Open Xcode, choose **Settings...** from the Xcode menu (or press cmd ⌘ + ,). Go to the **Locations** and install the tools by selecting the most recent version in the **Command Line Tools** dropdown. #### Install an iOS Simulator in Xcode To install an iOS Simulator, open **Xcode > Settings... > Components**, and under **Platform Support > iOS ...**, click **Get**. #### Install Watchman [Watchman](https://facebook.github.io/watchman/docs/install#macos) is a tool for watching changes in the filesystem. Installing it will result in better performance. You can install it with: ```sh brew update brew install watchman ``` ### Configure your project #### Install expo-dev-client Run the following command in your project's root directory: ```sh npx expo install expo-dev-client ``` #### Plug in your device via USB and enable developer mode 1. Connect your iOS device to your Mac using a USB cable. Unlock the device and tap **Trust** if prompted. 2. Open Xcode. From the menu bar, select **Window** > **Devices and Simulators**. You will see a warning in Xcode to enable developer mode. 3. On your iOS device, open **Settings** > **Privacy & Security**, scroll down to the **Developer Mode** list item and navigate into it. 4. Tap the switch to enable **Developer Mode**. After you do so, Settings presents an alert to warn you that Developer Mode reduces your device's security. To continue enabling **Developer Mode**, tap the alert's **Restart** button. 5. After the device restarts and you unlock it, the device shows an alert confirming that you want to enable Developer Mode. Tap **Turn On**, and enter your device passcode when prompted. #### Run the project on your device 1. Add the `ios.bundleIdentifier` in the **app.json** file in the root directory to a unique value so that Xcode generates the provisioning profile for the app signing step. 2. Run the following command in your project's root directory and select your plugged in device from the list: ```sh npx expo run:ios --device ``` > This command runs a development server after building your app. You can skip running `npx expo start` on the next page. --- ## iOS Simulator with Expo Go ### Set up an iOS Simulator with Expo Go ### Set up Xcode #### Install Xcode Open up the Mac App Store, search for [Xcode](https://apps.apple.com/us/app/xcode/id497799835), and click **Install** (or **Update** if you have it already). #### Install Xcode Command Line Tools Open Xcode, choose **Settings...** from the Xcode menu (or press cmd ⌘ + ,). Go to the **Locations** and install the tools by selecting the most recent version in the **Command Line Tools** dropdown. #### Install an iOS Simulator in Xcode To install an iOS Simulator, open **Xcode > Settings... > Components**, and under **Platform Support > iOS ...**, click **Get**. #### Install Watchman [Watchman](https://facebook.github.io/watchman/docs/install#macos) is a tool for watching changes in the filesystem. Installing it will result in better performance. You can install it with: ```sh brew update brew install watchman ``` ### Install Expo Go When you start a development server with `npx expo start` on the [start developing](/get-started/start-developing) page, press i to open the iOS Simulator. Expo CLI will install Expo Go automatically. --- ## iOS Simulator with a development build (EAS) ### Set up an iOS Simulator with a development build ### Set up Xcode #### Install Xcode Open up the Mac App Store, search for [Xcode](https://apps.apple.com/us/app/xcode/id497799835), and click **Install** (or **Update** if you have it already). #### Install Xcode Command Line Tools Open Xcode, choose **Settings...** from the Xcode menu (or press cmd ⌘ + ,). Go to the **Locations** and install the tools by selecting the most recent version in the **Command Line Tools** dropdown. #### Install an iOS Simulator in Xcode To install an iOS Simulator, open **Xcode > Settings... > Components**, and under **Platform Support > iOS ...**, click **Get**. #### Install Watchman [Watchman](https://facebook.github.io/watchman/docs/install#macos) is a tool for watching changes in the filesystem. Installing it will result in better performance. You can install it with: ```sh brew update brew install watchman ``` ### Create a development build #### Install EAS CLI To build your app, you will need to install EAS CLI. You can do this by running the following command in your terminal: ```sh npm install -g eas-cli ``` #### Create an Expo account and login Next, you will need to create an Expo account and login to the EAS CLI. 1. [Sign up](https://expo.dev/signup) for an Expo account. 2. Run the following command in your terminal to log in to the EAS CLI: ```sh eas login ``` #### Configure your project Run the following command to create an EAS config in your project: ```sh eas build:configure ``` #### Adjust your build profile To create a simulator-compatible development build, you'll need to update your build profile in **eas.json** to set the `ios.simulator` property to `true`: ```json eas.json { "build": { "development": { "developmentClient": true, "distribution": "internal", /* @info */ "ios": { "simulator": true } /* @end */ } } } ``` #### Create a development build Run the following command to create a development build: ```sh eas build --platform ios --profile development ``` #### Install the development build on your simulator After the build is complete, the CLI will prompt you to automatically download and install it on the iOS Simulator. When prompted, press Y to directly install it on the simulator. If you miss this prompt, you can download the build from the link provided in the terminal and drag and drop it onto the iOS Simulator to install it. --- ## iOS Simulator with a development build (local) ### Set up an iOS Simulator with a development build ### Set up Xcode and Watchman #### Install Xcode Open up the Mac App Store, search for [Xcode](https://apps.apple.com/us/app/xcode/id497799835), and click **Install** (or **Update** if you have it already). #### Install Xcode Command Line Tools Open Xcode, choose **Settings...** from the Xcode menu (or press cmd ⌘ + ,). Go to the **Locations** and install the tools by selecting the most recent version in the **Command Line Tools** dropdown. #### Install an iOS Simulator in Xcode To install an iOS Simulator, open **Xcode > Settings... > Components**, and under **Platform Support > iOS ...**, click **Get**. #### Install Watchman [Watchman](https://facebook.github.io/watchman/docs/install#macos) is a tool for watching changes in the filesystem. Installing it will result in better performance. You can install it with: ```sh brew update brew install watchman ``` ### Running your app on an iOS Simulator #### Install expo-dev-client Run the following command in your project's root directory: ```sh npx expo install expo-dev-client ``` Run the following from your terminal: ```sh npx expo run:ios ``` > This command runs a development server after building your app. You can skip running `npx expo start` on the next page. --- --- title: 开始开发 description: 对 Expo 项目进行第一次修改,并在你的设备上实时查看效果。 --- ## Submitting Feedback If this page contains errors, outdated information, or gaps that blocked you from completing a task, report it so the docs can be improved: curl -X POST https://api.expo.dev/v2/feedback/docs-send -H 'Content-Type: application/json' -d '{"url":"/get-started/start-developing/","feedback":"🤖 Agent feedback: "}' Only submit when you have something specific and actionable to report. # 开始开发 对 Expo 项目进行第一次修改,并在你的设备上实时查看效果。 > For the complete documentation index, see [llms.txt](/llms.txt). Use this Use this file to discover all available pages. ## 启动开发服务器 要启动开发服务器,请运行以下命令: ```sh npx expo start ``` ## 在你的设备上打开应用 运行上述命令后,你会在终端中看到一个二维码。扫描此二维码即可在你的设备上打开应用。 如果你使用的是 Android 模拟器或 iOS 模拟器,你可以分别按 a 或 i 来打开应用。 遇到问题? 请确保你的电脑和设备连接到同一个 Wi-Fi 网络。 如果仍然不起作用,可能是路由器配置所致 — 这在公共网络中很常见。你可以在启动开发服务器时选择 **Tunnel** 连接类型,然后再次扫描二维码来解决这个问题。 ```sh npx expo start --tunnel ``` > 使用 **Tunnel** 连接类型会使应用重新加载的速度明显慢于 **LAN** 或 **Local**,因此在可能的情况下最好避免使用 tunnel。如果需要通过网络中的另一台设备访问你的机器,你可能需要安装并使用模拟器或仿真器来加快开发速度。 ## 进行你的第一次修改 在代码编辑器中打开 **src/app/index.tsx** 文件并进行一次修改。 ```diff - 欢迎使用 Expo + 你好,世界! ``` 设备上没有显示更改? Expo Go 默认配置为在文件发生更改时自动重新加载应用,但我们还是来确认一下启用它的步骤,以防万一某些地方没有正常工作。 - 确保你已在 Expo CLI 中[启用开发模式](/workflow/development-mode#development-mode)。 - 关闭 Expo 应用并重新打开它。 - 应用再次打开后,摇晃你的设备以显示开发者菜单。按 Cmd ⌘ + D。 - 如果你看到已启用 **Fast Refresh**,请将其切换。如果你看到 **Disable Fast Refresh**,请关闭开发者菜单。现在再试着做一次更改。 ## 文件结构 下面,你可以熟悉默认项目的文件结构: Files ### app Contains the app's navigation, which is file-based. The file structure of the **src/app** directory determines the app's navigation. The app has two routes defined by two files: **src/app/index.tsx** and **src/app/explore.tsx**. The layout file in **src/app/_layout.tsx** sets up the tab navigator using the platform-specific **AppTabs** component. ## 功能 默认项目模板具有以下功能: Default project ### File-based routing The app has two screens: **src/app/index.tsx** and **src/app/explore.tsx**. The layout file in **src/app/_layout.tsx** sets up navigation using a platform-specific **AppTabs** component that uses native tabs on Android and iOS, and Expo Router UI tabs on web. --- --- title: 下一步 description: 开发、审查并提交你的项目。 --- ## Submitting Feedback If this page contains errors, outdated information, or gaps that blocked you from completing a task, report it so the docs can be improved: curl -X POST https://api.expo.dev/v2/feedback/docs-send -H 'Content-Type: application/json' -d '{"url":"/get-started/next-steps/","feedback":"🤖 Agent feedback: "}' Only submit when you have something specific and actionable to report. # 下一步 开发、审查并提交你的项目。 > For the complete documentation index, see [llms.txt](/llms.txt). Use this Use this file to discover all available pages. 下面是继续构建应用的下一步: ### 重置你的项目 你可以删除样板代码并用一个新项目重新开始。运行以下命令来重置你的项目: ```sh npm run reset-project ``` 此命令会将 **app** 中现有的文件移动到 **app-example**,然后创建一个新的 **app** 目录,并包含一个新的 **index.tsx** 文件。 ### 开发、审阅和部署 通过阅读 Develop 部分中的文档来学习如何开发。你将学会如何创建 [UI 元素](/develop/user-interface/splash-screen-and-app-icon)、添加 [单元测试](/develop/unit-testing)、引入 [原生模块](/config-plugins/introduction),等等。 当你完成应用开发后,你可以与团队成员分享以进行[审阅](/review/overview)。 最后,你可以将你的项目[构建](/deploy/build-project)并[提交到应用商店](/deploy/submit-to-app-stores)。 ### 分步指南 如果你想从头到尾通过逐步演练来学习如何使用 Expo 构建应用,请查看[教程](/tutorial/introduction)。 --- --- title: Expo AI 代理技能 description: Expo 提供的用于构建、部署和调试 Expo 与 React Native 应用的官方 AI 代理技能列表。 --- ## Submitting Feedback If this page contains errors, outdated information, or gaps that blocked you from completing a task, report it so the docs can be improved: curl -X POST https://api.expo.dev/v2/feedback/docs-send -H 'Content-Type: application/json' -d '{"url":"/skills/","feedback":"🤖 Agent feedback: "}' Only submit when you have something specific and actionable to report. # Expo AI 代理技能 Expo 提供的用于构建、部署和调试 Expo 与 React Native 应用的官方 AI 代理技能列表。 > For the complete documentation index, see [llms.txt](/llms.txt). Use this Use this file to discover all available pages. Expo Skills 是结构化的说明文件,旨在教导 AI 代理如何准确且高效地构建、部署和调试 Expo 与 React Native 应用。它们可与 Claude Code、Cursor、Codex 以及其他 AI 代理协同工作。 ## 安装 Expo Skills 运行以下命令,从插件市场添加并安装 Expo Skills: ```sh /plugin marketplace add expo/skills /plugin install expo ``` ## 可用的 Expo Skills `expo` 插件中提供了以下 skills: | Skill | Description | | --- | --- | | [`building-native-ui`](https://github.com/expo/skills/blob/main/plugins/expo/skills/building-native-ui/SKILL.md) | Complete guide for building beautiful apps with Expo Router. Covers fundamentals, styling, components, navigation, animations, patterns, and native tabs. | | [`eas-update-insights`](https://github.com/expo/skills/blob/main/plugins/expo/skills/eas-update-insights/SKILL.md) | Check the health of published EAS Updates: crash rates, install/launch counts, unique users, payload size, and the split between embedded and OTA users per channel. Use when the user asks how an update is performing, whether a rollout is healthy, how many users are on the embedded build vs OTA, or wants to gate CI on update health. | | [`expo-api-routes`](https://github.com/expo/skills/blob/main/plugins/expo/skills/expo-api-routes/SKILL.md) | Guidelines for creating API routes in Expo Router with EAS Hosting. | | [`expo-cicd-workflows`](https://github.com/expo/skills/blob/main/plugins/expo/skills/expo-cicd-workflows/SKILL.md) | Helps understand and write EAS workflow YAML files for Expo projects. Use this skill when the user asks about CI/CD or workflows in an Expo or EAS context, mentions .eas/workflows/, or wants help with EAS build pipelines or deployment automation. | | [`expo-deployment`](https://github.com/expo/skills/blob/main/plugins/expo/skills/expo-deployment/SKILL.md) | Deploying Expo apps to iOS App Store, Android Play Store, web hosting, and API routes. | | [`expo-dev-client`](https://github.com/expo/skills/blob/main/plugins/expo/skills/expo-dev-client/SKILL.md) | Build and distribute Expo development clients locally or via TestFlight. | | [`expo-module`](https://github.com/expo/skills/blob/main/plugins/expo/skills/expo-module/SKILL.md) | Guide for writing Expo native modules and views using the Expo Modules API (Swift, Kotlin, TypeScript). Covers module definition DSL, native views, shared objects, config plugins, lifecycle hooks, autolinking, and type system. Use when building or modifying native modules for Expo. | | [`expo-tailwind-setup`](https://github.com/expo/skills/blob/main/plugins/expo/skills/expo-tailwind-setup/SKILL.md) | Set up Tailwind CSS v4 in Expo with react-native-css and NativeWind v5 for universal styling. | | [`expo-ui-jetpack-compose`](https://github.com/expo/skills/blob/main/plugins/expo/skills/expo-ui-jetpack-compose/SKILL.md) | `@expo/ui/jetpack-compose` package lets you use Jetpack Compose Views and modifiers in your app. | | [`expo-ui-swift-ui`](https://github.com/expo/skills/blob/main/plugins/expo/skills/expo-ui-swift-ui/SKILL.md) | `@expo/ui/swift-ui` package lets you use SwiftUI Views and modifiers in your app. | | [`native-data-fetching`](https://github.com/expo/skills/blob/main/plugins/expo/skills/native-data-fetching/SKILL.md) | Use when implementing or debugging ANY network request, API call, or data fetching. Covers fetch API, React Query, SWR, error handling, caching, offline support, and Expo Router data loaders (`useLoaderData`). | | [`upgrading-expo`](https://github.com/expo/skills/blob/main/plugins/expo/skills/upgrading-expo/SKILL.md) | Guidelines for upgrading Expo SDK versions and fixing dependency issues. | | [`use-dom`](https://github.com/expo/skills/blob/main/plugins/expo/skills/use-dom/SKILL.md) | Use Expo DOM components to run web code in a webview on native and as-is on web. Migrate web code to native incrementally. | ## 示例提示词 安装 Expo Skills 后,可以尝试以下提示词。你的 AI 代理会自动使用合适的 skill: | 示例提示词 | 使用的 skill | | --- | --- | | 构建一个带有表单和导航的设置页面 | `building-native-ui` | | 在我的 Expo 项目中设置 Tailwind CSS | `expo-tailwind-setup` | | 使用网页代码在我的原生应用中嵌入一个 recharts 图表 | `use-dom` | | 向我的 Expo 应用添加一个 SwiftUI 选择器组件 | `expo-ui-swift-ui` | | 在 Jetpack Compose 中使用 Material Design 3 组件 | `expo-ui-jetpack-compose` | | 如何将我的 Expo 应用部署到 Apple App Store? | `expo-deployment` | | 创建一个在每个 PR 上构建的 CI/CD 工作流 | `expo-cicd-workflows` | | 将我的项目升级到最新的 Expo SDK | `upgrading-expo` | ## 其他资源 [expo/skills GitHub 代码仓库](https://github.com/expo/skills) — expo/skills — 浏览所有可用 Expo Skills 的源代码,或报告问题。 [Expo MCP Server](/eas/ai/mcp) — 配套的 AI 工具,可让编码代理直接访问 Expo 和 EAS 服务。 --- --- title: AI 代理和 LLM 的文档 description: 供 AI 代理和 LLM 高效访问和使用 Expo 文档的方式。 --- ## Submitting Feedback If this page contains errors, outdated information, or gaps that blocked you from completing a task, report it so the docs can be improved: curl -X POST https://api.expo.dev/v2/feedback/docs-send -H 'Content-Type: application/json' -d '{"url":"/llms/","feedback":"🤖 Agent feedback: "}' Only submit when you have something specific and actionable to report. # AI 代理和 LLM 的文档 供 AI 代理和 LLM 高效访问和使用 Expo 文档的方式。 > For the complete documentation index, see [llms.txt](/llms.txt). Use this Use this file to discover all available pages. 使用以下端点和工具,让 AI 代理和 LLM 以比获取完整网页更低的 token 成本访问 Expo 文档。 ## 快速开始 选择与你的工具相匹配的方法: | 方法 | 最适合 | 如何 | | --- | --- | --- | | 单页 Markdown | 聊天界面(ChatGPT、Claude.ai)和编程代理 | 在任意文档页面 URL 后追加 `/index.md`。 | | 复制 Markdown 下拉菜单 | 单页的快速提示 | 在任意文档页面顶部点击 **Copy page** > **Copy Markdown**。 | | 分节捆绑包 | 项目规则和编程代理 | 将分节级别的 `llms-*.txt` URL 添加到你的 AI 工具配置,或使用通用索引(`/llms.txt`)。 | ## 单页 Markdown 每个文档页面都有一个轻量级的 Markdown 版本,只需在页面 URL 后追加 `/index.md` 即可访问。例如: ```text https://documentation.expo.dev/develop/development-builds/create-a-build/index.md ``` 当你希望向 AI 代理提供某个特定主题或页面的上下文,而又不想让它接收该页面完整 HTML 时,上述方法非常有用。 ## 文档捆绑包 在 Expo,我们支持 [llms.txt](https://llmstxt.org/) 计划,以便为大语言模型(LLMs)及使用它们的应用提供文档。下面是可用文档文件的列表。 ### 全站捆绑包 | 端点 | 描述 | 大小 | | --- | --- | --- | | [/llms.txt](/llms.txt) | 包含所有可用文档文件列表的索引页。 | ~94 kB | | [/llms-full.txt](/llms-full.txt) | Expo 的完整文档,包括 Expo Router、Expo Modules API、开发流程等。 | ~1.9 MB | ### 分节捆绑包 | 端点 | 描述 | 大小 | | --- | --- | --- | | [/llms-eas.txt](/llms-eas.txt) | Expo Application Services(EAS)的完整文档。 | ~974 kB | | [/llms-sdk.txt](/llms-sdk.txt) | 最新 Expo SDK 的完整文档。 | ~2.6 MB | 在寻找已弃用的 Expo SDK 版本? - [/llms-sdk-v54.0.0.txt](/llms-sdk-v54.0.0.txt):Expo SDK v54.0.0 的文档 - [/llms-sdk-v53.0.0.txt](/llms-sdk-v53.0.0.txt):Expo SDK v53.0.0 的文档 - [/llms-sdk-v52.0.0.txt](/llms-sdk-v52.0.0.txt):Expo SDK v52.0.0 的文档 - [/llms-sdk-v51.0.0.txt](/llms-sdk-v51.0.0.txt):Expo SDK v51.0.0 的文档 --- --- title: 开发工具 description: Expo 工具和网站概览,可在你构建项目旅程的各个方面为你提供帮助。 --- ## Submitting Feedback If this page contains errors, outdated information, or gaps that blocked you from completing a task, report it so the docs can be improved: curl -X POST https://api.expo.dev/v2/feedback/docs-send -H 'Content-Type: application/json' -d '{"url":"/develop/tools/","feedback":"🤖 Agent feedback: "}' Only submit when you have something specific and actionable to report. # 开发工具 Expo 工具和网站概览,可在你构建项目旅程的各个方面为你提供帮助。 > For the complete documentation index, see [llms.txt](/llms.txt). Use this Use this file to discover all available pages. 当你使用 Expo 创建一个新项目时,了解以下必备工具和网站可以在应用开发过程中为你提供帮助。本页面概述了一系列推荐工具。 ## Expo CLI Expo CLI 是一个开发工具,在你创建新项目时会随 `expo` 包自动安装。你可以通过使用 `npx`(一个 Node.js 包运行器)来使用它。 它旨在帮助你在应用开发阶段更快地推进。例如,你与 Expo CLI 的第一次交互,就是通过运行命令 `npx expo start` 来启动开发服务器。 以下是在开发应用时会经常与 Expo CLI 一起使用的常见命令列表: | 命令 | 描述 | | --- | --- | | `npx expo start` | 启动开发服务器(无论你使用的是开发构建还是 Expo Go)。 | | `npx expo prebuild` | 使用 [Prebuild](/workflow/continuous-native-generation) 生成原生 Android 和 iOS 目录。 | | `npx expo run:android` | 在本地编译原生 Android 应用。 | | `npx expo run:ios` | 在本地编译原生 iOS 应用。 | | `npx expo install package-name` | 通过在此命令后添加 `--fix` 选项,用于安装新库,或验证并更新项目中的特定库。 | | `npx expo lint` | [设置并配置](/guides/using-eslint) ESLint。如果 ESLint 已经配置好,此命令会[对你的项目文件进行 lint 检查](/guides/using-eslint#usage)。 | 简而言之,Expo CLI 允许你开发、编译、启动应用等等。有关你可以使用 CLI 执行的更多选项和操作,请参阅 [Expo CLI 参考](/more/expo-cli)。 ## EAS CLI EAS CLI 用于登录你的 Expo 账户,并使用不同的 EAS 服务(例如 Build、Update 或 Submit)来编译你的应用。你也可以使用此工具来: - 将你的应用发布到应用商店 - 创建应用的开发、预览或生产构建 - 创建空中更新(OTA) - 管理你的应用凭证 - 为 iOS 设备创建一个 ad hoc provisioning profile 要使用 EAS CLI,你需要在本地机器上全局安装它,运行以下命令: ```sh npm install -g eas-cli ``` 你可以在终端窗口中使用 `eas --help` 来了解更多可用命令。完整参考请查看 [`eas-cli` npm 页面](https://www.npmjs.com/package/eas-cli)。 ## Expo Doctor Expo Doctor 是一个命令行工具,用于诊断 Expo 项目中的问题。要使用它,请在项目根目录中运行以下命令: ```sh npx expo-doctor ``` 此命令会检查并分析项目代码库中与 [app config](/workflow/configuration) 和 **package.json** 文件、依赖兼容性、配置文件以及项目整体健康状况相关的常见问题。检查完成后,Expo Doctor 会输出结果。 如果 Expo Doctor 发现问题,它会提供问题描述,以及如何修复或在哪里寻求帮助的建议。 默认情况下,Expo Doctor 会根据 [React Native directory](https://reactnative.directory/) 验证项目的包,并在原生目录存在时检查 app config 属性是否已正确同步。你可以在项目的 **package.json** 文件中配置这些检查。有关更多详情,请参阅 [`reactNativeDirectoryCheck`](/versions/latest/config/package-json#reactnativedirectorycheck) 和 [`appConfigFieldsNotSyncedCheck`](/versions/latest/config/package-json#appconfigfieldsnotsynced)。 你也可以使用 `npx expo-doctor --help` 来显示使用信息。 ## Orbit Orbit 是一款适用于 macOS 和 Windows 的应用,可实现: - 在实体设备和模拟器上安装并启动来自 EAS 的构建。 - 在 Android 模拟器或 iOS 模拟器上安装并启动来自 EAS 的更新。 - 在 Android 模拟器或 iOS 模拟器上启动 snack 项目。 - 使用本地文件来安装并启动应用。Orbit 支持任何 Android **.apk**、iOS Simulator 兼容的 **.app**,或 ad hoc 签名的应用。 - 查看你 EAS 控制台中固定项目的列表。 ### 安装 你可以通过 Homebrew 为 macOS 下载 Orbit,或者直接从 [GitHub releases](https://github.com/expo/orbit/releases) 下载。 ```sh brew install expo-orbit ``` 如果你希望 Orbit 在登录时自动启动,请点击菜单栏中的 Orbit 图标,然后选择 **Settings**,再勾选 **Launch on Login** 选项。 > Orbit 在 macOS 和 Windows 上都依赖 Android SDK,而 `xcrun` 仅在 macOS 上用于设备管理,这需要同时设置 [Android Studio](/workflow/android-studio-emulator) 和 [Xcode](/workflow/ios-simulator)。 ## VS Code 的 Expo Tools Expo Tools 是一个 VS Code 扩展,可在处理应用配置文件时提升你的开发体验。它为 app 配置、EAS 配置、商店配置以及 Expo Module 配置文件等提供自动补全和智能提示等功能。 [安装 Expo Tools VS Code 扩展](https://marketplace.visualstudio.com/items?itemName=expo.vscode-expo-tools) — 使用此链接安装该扩展,或直接在 VS Code 编辑器中搜索 Expo Tools。 你也可以使用它通过 VS Code 内置调试器来调试你的应用,设置断点、检查变量、通过调试控制台执行代码等。有关如何使用此扩展进行调试,请参阅[使用 VS Code 调试](/debugging/tools#debugging-with-vs-code)。 ## 使用 Snack 和 Expo Go 测试原型 ### Snack Snack 是一个基于浏览器的开发环境,工作方式与 Expo Go 类似。它非常适合分享代码片段并在不下载任何工具到电脑的情况下试验 React Native。 要使用它,请前往 [snack.expo.dev](https://snack.expo.dev/),编辑 **App.js** 中的 `` 组件,在右侧面板中选择一个平台(Android、iOS 或 web),并实时查看变化。 ### Expo Go [Expo Go](https://expo.dev/go) 是一个免费、开源的试验环境,供学生和学习者尝试 React Native。它适用于 Android 和 iOS。 有关如何使用它的更多信息: - 点击 [此链接](/get-started/set-up-your-environment?mode=expo-go) 前往“设置你的开发环境”指南 - 在 **你希望在哪里开发?** 下选择一个平台 - 在 **你希望如何开发?** 下选择 Expo Go - 按照该指南中的说明进行操作 > **注意:** Expo Go 的功能有限,不适合用于构建生产级项目。请改用[开发构建](/get-started/set-up-your-environment?mode=development-build)。 如果我打开了一个使用不受支持 SDK 版本的项目,会怎样? 在 Expo Go 中运行一个为不受支持的 SDK 版本创建的项目时,你会看到以下错误: ```sh "Project is incompatible with this version of Expo Go" ``` 要修复此问题,建议将你的项目升级到[受支持的 SDK 版本](/versions/latest#each-expo-sdk-version-depends-on-a-react-native-version)。如果你想了解如何操作,请参阅[将项目升级到新的 SDK 版本](/develop/tools#how-do-i-upgrade-my-project-from)。 如何从不受支持的 SDK 版本升级我的项目? 请参阅[Expo SDK 升级指南](/workflow/upgrading-expo-sdk-walkthrough)中的说明,将其升级到特定的 SDK 版本。 ## React Native 目录 任何与 React Native 兼容的库,在你使用开发构建创建项目时,都可以在 Expo 项目中使用。 [reactnative.directory](https://reactnative.directory/) 是一个可搜索的 React Native 库数据库。如果你要找的库未包含在 Expo SDK 中,可以使用该目录为你的项目查找兼容的库。 [使用库](/workflow/using-libraries) — 阅读本指南,了解 React Native 核心库、Expo SDK 库以及第三方库之间的区别。它还解释了如何判断第三方库的兼容性。 --- --- title: Expo 和 React Native 应用中的导航 description: 了解在 Expo 和 React Native 项目中集成导航的推荐方法。 --- ## Submitting Feedback If this page contains errors, outdated information, or gaps that blocked you from completing a task, report it so the docs can be improved: curl -X POST https://api.expo.dev/v2/feedback/docs-send -H 'Content-Type: application/json' -d '{"url":"/develop/app-navigation/","feedback":"🤖 Agent feedback: "}' Only submit when you have something specific and actionable to report. # Expo 和 React Native 应用中的导航 了解在 Expo 和 React Native 项目中集成导航的推荐方法。 > For the complete documentation index, see [llms.txt](/llms.txt). Use this Use this file to discover all available pages. React Native 核心库不包含内置的导航解决方案,因此你可以选择最适合你需求的导航库。对于 Expo 和 React Native 应用,通常是在 [React Navigation](https://reactnavigation.org/) 和 [Expo Router](/router/introduction) 之间做选择。 ## 为什么 React Native 应用需要导航库 React Native 核心包含基础 UI 组件、触摸处理、设备 API 和网络通信,但不包括诸如存储、相机、地图、大多数设备传感器以及 **导航** 在内的功能!这些内容旨在由社区库来提供。 ## React Navigation React Navigation 是一个基于组件的导航库,在 React Native 生态系统中被广泛使用。它允许你完全通过代码组合堆栈、标签页和抽屉导航器,从而实现复杂流程、自定义过渡效果以及特定于应用的 UX 模式。 该库提供平台特定的外观和体验,具有流畅的动画和手势、统一的移动端和 Web 路由、带有静态配置的自动深度链接、类型化路由,并且高度可定制。 [React Navigation:入门](https://reactnavigation.org/docs/getting-started) — 了解如何开始使用 React Navigation。 ## Expo Router(推荐用于 Expo 项目) Expo Router 是一个面向 Expo 和 React Native 项目的基于文件的路由库,它建立在 React Navigation 之上。通过遵循 **app** 目录约定,它会将文件转换为路由,并与 Expo 集成,无需额外设置即可用于 [Expo CLI](/more/expo-cli) 和打包。该库还增加了诸如类型化路由、动态路由、开发环境中的延迟打包、Web 端静态渲染以及自动深度链接等功能。 使用 `npx create-expo-app@latest --template default@sdk-55` 创建的新 Expo 项目默认包含 Expo Router,因此你可以快速交付跨平台导航,同时在需要时仍然可以使用 React Navigation API。 [Expo Router 简介](/router/introduction) — Expo Router 是一个为使用 Expo 构建的通用 React Native 应用提供的开源路由库。 [安装](/router/installation) — 了解如何通过创建一个带有 Expo Router 的新项目,或将该库添加到现有项目中,快速开始。 [核心概念](/router/basics/core-concepts) — 了解 Expo 中基于文件的路由核心概念。 --- --- title: 启动画面和应用图标 description: 了解如何向你的 Expo 项目添加启动画面和应用图标。 --- ## Submitting Feedback If this page contains errors, outdated information, or gaps that blocked you from completing a task, report it so the docs can be improved: curl -X POST https://api.expo.dev/v2/feedback/docs-send -H 'Content-Type: application/json' -d '{"url":"/develop/user-interface/splash-screen-and-app-icon/","feedback":"🤖 Agent feedback: "}' Only submit when you have something specific and actionable to report. # 启动画面和应用图标 了解如何向你的 Expo 项目添加启动画面和应用图标。 > For the complete documentation index, see [llms.txt](/llms.txt). Use this Use this file to discover all available pages. 启动画面和应用图标是移动应用的基本元素。它们在应用的用户体验和品牌塑造中起着重要作用。本指南提供了如何创建并将它们添加到你的应用中的步骤。 [创建应用图标和启动画面](https://www.youtube.com/watch?v=3Bsw8a1BJoQ) — 查看一份详细演示,了解如何为 Expo 项目创建应用图标和启动画面。 ## 启动画面 启动画面,也称为启动屏幕,是用户打开应用时看到的第一个界面。它会在应用加载期间保持可见。你还可以使用原生的 [SplashScreen API](/versions/latest/sdk/splash-screen) 来控制启动画面消失的时机。 [`expo-splash-screen`](/versions/latest/sdk/splash-screen) 内置了一个 [配置插件](/config-plugins/introduction),可让你配置启动图标和背景颜色等属性。 > **警告** **不要使用 Expo Go 或开发构建来测试你的启动画面**。当启动画面可见时,Expo Go 会渲染你的应用图标,这可能会干扰测试。开发构建包含 `expo-dev-client`,它有自己的启动画面,可能会导致冲突。**请改用 [预览构建](/build/eas-json#preview-builds) 或 [生产构建](/build/eas-json#production-builds)**。 ### 创建启动画面图标 要创建启动画面图标,你可以使用这个 [Figma 模板](https://www.figma.com/community/file/1466490409418563617)。它为 Android 和 iOS 的图标与启动图像提供了最基础的设计。 **推荐:** - 使用 1024x1024 的图片。 - 使用 **.png** 文件。 - 使用透明背景。 ### 将启动图标导出为 .png 创建启动画面图标后,将其导出为 **.png** 并保存到 **assets/images** 目录中。默认情况下,Expo 使用 **splash-icon.png** 作为文件名。如果你决定更改启动画面文件的名称,请务必在下一步中使用该名称。 > **注意:** **目前,仅支持 .png 图片** 作为 Expo 项目中的启动画面图标。如果你使用其他图片格式,应用的生产构建将会失败。 ### 配置启动画面图标 打开应用配置文件,在 plugins 下设置以下属性: ```json { "expo": { "plugins": [ [ "expo-splash-screen", { "backgroundColor": "#232323", "image": "./assets/images/splash-icon.png", "dark": { "image": "./assets/images/splash-icon-dark.png", "backgroundColor": "#000000" }, "imageWidth": 200 } ] ] } } ``` 要测试你的新启动画面,请为 [内部分发](/tutorial/eas/internal-distribution-builds) 或生产环境构建你的应用,具体可参考 [Android](/tutorial/eas/android-production-build) 和 [iOS](/tutorial/eas/ios-production-build) 指南。 [可配置的启动画面属性](/versions/latest/sdk/splash-screen#configurable-properties) — 了解 SplashScreen API 的可配置属性。 分别为 Android 和 iOS 配置 `expo-splash-screen` 属性 [`expo-splash-screen`](/versions/latest/sdk/splash-screen) 还支持 `android` 和 `ios` 属性,用于为特定平台配置启动画面。请参见以下示例: ```json { "expo": { "plugins": [ [ "expo-splash-screen", { "ios": { "backgroundColor": "#ffffff", "image": "./assets/images/splash-icon.png", "resizeMode": "cover" }, "android": { "backgroundColor": "#0c7cff", "image": "./assets/images/splash-android-icon.png", "imageWidth": 150 } } ] ] } } ``` 没有使用 prebuild? 如果你的应用没有使用 [Expo Prebuild](/more/glossary-of-terms#prebuild)(以前称为 _managed workflow_)来生成原生的 **android** 和 **ios** 目录,那么应用配置中的更改将不会生效。更多信息请参见 [如何手动自定义配置](https://github.com/expo/expo/tree/main/packages/expo-splash-screen#-installation-in-bare-react-native-projects)。 故障排查:iOS 上没有出现新的启动画面 对于 SDK 52 及更早版本,在 iOS 开发构建中,启动屏幕有时会在不同构建之间保持缓存,使测试新图片变得更困难。Apple 建议在重新构建前清除 _derived data_ 目录,这可以通过运行 Expo CLI 来完成: ```sh npx expo run:ios --no-build-cache ``` 更多信息请参见 [Apple 关于测试启动屏幕的指南](https://developer.apple.com/documentation/technotes/tn3118-debugging-your-apps-launch-screen)。 ## 应用图标 应用图标是你的应用用户在设备主屏幕和应用商店中看到的内容。Android 和 iOS 有不同且严格的要求。 ### 创建应用图标 要创建应用图标,你可以使用这个 [Figma 模板](https://www.figma.com/community/file/1466490409418563617)。它为 Android 和 iOS 的图标与启动图像提供了最基础的设计。 ### 将图标图片导出为 .png 创建应用图标后,将其导出为 **.png** 并保存到 **assets/images** 目录中。默认情况下,Expo 使用 **icon.png** 作为文件名。如果你决定使用其他文件名,请务必在下一步中使用它。 ### 在应用配置中添加图标 打开应用配置,并将 [`icon`](/versions/latest/config/app#icon) 属性的值设置为本地路径,以便将其指向你新的应用图标: ```json { "icon": "./assets/images/icon.png" } ``` Android 和 iOS 的自定义配置提示 #### Android 可以使用 [`android.adaptiveIcon`](/versions/latest/config/app#adaptiveicon) 属性进一步自定义 Android 图标,它会覆盖前面提到的两个设置。 Android 自适应图标由两个独立图层组成 — 前景图像和背景颜色或背景图像。这使操作系统能够将图标裁切成不同形状,并支持视觉效果。对于 Android 13 及更高版本,操作系统支持主题化应用图标,它使用壁纸和主题来确定由设备主题设置的颜色。 你提供的设计应遵循用于启动器图标的 [Android 自适应图标指南](https://developer.android.com/develop/ui/views/launch/icon_design_adaptive)。你还应该: - 使用 **.png** 文件。 - 使用 `android.adaptiveIcon.foregroundImage` 属性指定前景图像的路径。 - 使用 `android.adaptiveIcon.monochromeImage` 属性指定单色图像的路径。 - 默认背景颜色为白色;若要指定其他背景颜色,请使用 `android.adaptiveIcon.backgroundColor` 属性。你也可以改用 `android.adaptiveIcon.backgroundImage` 属性指定背景图像。请确保它与前景图像具有相同的尺寸。 你还可以为不支持自适应图标的旧版 Android 设备提供单独的图标。你可以通过 `android.icon` 属性来实现。这个单一图标将是前景层和背景层的组合。 > 请参见 [Apple 最佳实践](https://developer.apple.com/design/human-interface-guidelines/app-icons/#Best-practices),以确保你的图标看起来专业,例如在不同壁纸上测试图标,并避免在产品字标旁边放置文本。请提供至少 512x512 像素的图标。 #### iOS [图标制作器](https://www.youtube.com/watch?v=RZ_QMym3adw) — 了解如何使用新的图标制作器为 Expo 项目创建应用图标。 对于 iOS,你的应用图标应遵循 [Apple 人机界面指南](https://developer.apple.com/design/human-interface-guidelines/app-icons/)。你可以使用 [Icon Composer](https://developer.apple.com/icon-composer/) 应用来创建你的应用图标。这将输出一个 **.icon** 目录,你可以将其添加到项目的 **assets** 目录中。然后你可以在应用配置中提供该目录的路径。使用此方法时,深色模式支持由 Icon Composer 处理,因此你无需提供变体。 > **信息** **注意:** 通过 `ios.icon` 提供 Icon Composer **.icon** 目录的支持从 **SDK 54** 开始可用。 ```json { "expo": { "ios": { "icon": "./assets/app.icon" } } } ``` 另外,之前提供图片的方式仍然受支持。你应该: - 使用 **.png** 文件。 - 1024x1024 是一个不错的尺寸。如果你有一个使用 `npx create-expo-app` 创建的 Expo 项目, [EAS Build](/build/setup) 会为你生成其他尺寸。若是裸 React Native 项目,请自行生成图标。EAS Build 生成的最大尺寸是 1024x1024。 - 图标必须是严格的正方形。例如,1023x1024 的图标无效。 - 确保图标填满整个正方形,没有圆角或其他透明像素。操作系统会在适当的时候对你的图标进行裁切。 - 使用 `ios.icon` 指定不同系统外观的图标(例如深色和着色)是可以提供的。如果指定了,这将覆盖应用配置文件顶部的图标键。请参见下面的示例: ```json { "expo": { "ios": { "icon": { "dark": "./assets/images/ios-dark.png", "light": "./assets/images/ios-light.png", "tinted": "./assets/images/ios-tinted.png" } } } } ``` --- --- title: 安全区域 description: 了解如何为 Expo 项目中的屏幕组件添加安全区域。 --- ## Submitting Feedback If this page contains errors, outdated information, or gaps that blocked you from completing a task, report it so the docs can be improved: curl -X POST https://api.expo.dev/v2/feedback/docs-send -H 'Content-Type: application/json' -d '{"url":"/develop/user-interface/safe-areas/","feedback":"🤖 Agent feedback: "}' Only submit when you have something specific and actionable to report. # 安全区域 了解如何为 Expo 项目中的屏幕组件添加安全区域。 > For the complete documentation index, see [llms.txt](/llms.txt). Use this Use this file to discover all available pages. 创建安全区域可确保应用屏幕的内容被正确定位。这意味着它不会被刘海、状态栏、主屏幕指示器以及设备物理硬件或操作系统控制的其他界面元素遮挡。当内容被遮挡时,它会被这些界面元素覆盖。 下面是一个应用屏幕内容在 Android 上被状态栏遮挡的示例。在 iOS 上,相同的内容会被圆角、刘海和状态栏遮挡。 ## 使用 `react-native-safe-area-context` 库 [`react-native-safe-area-context`](https://github.com/AppAndFlow/react-native-safe-area-context) 为处理 Android 和 iOS 设备的安全区域边距提供了一个灵活的 API。它还提供了一个 `SafeAreaView` 组件,你可以用它来替代 [``](https://reactnative.dev/docs/view),从而在屏幕组件中自动适配安全区域。 使用该库后,前一个示例的结果会变化,因为它将内容显示在安全区域内,如下所示: ### 安装 如果你使用了[默认模板](/get-started/create-a-project)创建项目,则可以跳过安装 `react-native-safe-area-context`。该库作为 Expo Router 库的 peer dependency 已经安装。否则,请运行以下命令进行安装: ```sh npx expo install react-native-safe-area-context ``` ### 用法 你可以直接使用 [`SafeAreaView`](https://appandflow.github.io/react-native-safe-area-context/api/safe-area-view) 来包裹屏幕组件的内容。它本质上是一个普通的 ``,会将安全区域边距作为额外的 padding 或 margin 应用进去。 ```tsx import { Text } from 'react-native'; import { SafeAreaView } from 'react-native-safe-area-context'; export default function HomeScreen() { return ( 内容位于安全区域内。 ); } ``` 使用了不同的 Expo 模板且没有安装 Expo Router? 在屏幕组件中使用 `SafeAreaView` 之前,请先导入并将 [`SafeAreaProvider`](https://appandflow.github.io/react-native-safe-area-context/api/safe-area-provider) 添加到根组件文件中(例如 **App.tsx**)。 ```tsx import { SafeAreaProvider } from 'react-native-safe-area-context'; export default function App() { return ( return ...; ); } ``` ## 另一种方式:`useSafeAreaInsets` 钩子 除了 `SafeAreaView` 之外,你还可以在屏幕组件中使用 [`useSafeAreaInsets`](https://appandflow.github.io/react-native-safe-area-context/api/use-safe-area-insets) 钩子。它可以直接访问安全区域边距,让你使用该钩子的 inset 为 `` 的每个边应用 padding。 下面的示例使用了 `useSafeAreaInsets` 钩子。它通过 `insets.top` 为 `` 应用顶部 padding。 ```tsx import { Text, View } from 'react-native'; import { useSafeAreaInsets } from 'react-native-safe-area-context'; export default function HomeScreen() { const insets = useSafeAreaInsets(); return ( 内容位于安全区域内。 ); } ``` 该钩子会以以下对象提供边距: ```ts { top: number, right: number, bottom: number, left: number } ``` ## 其他信息 ### 最小示例 下面是一个最小可运行示例,它使用 `useSafeAreaInsets` 钩子为视图应用顶部 padding。 ```tsx import { Text, View } from 'react-native'; import { SafeAreaProvider, useSafeAreaInsets } from 'react-native-safe-area-context'; function HomeScreen() { const insets = useSafeAreaInsets(); return ( 内容位于安全区域内。 ); } export default function App() { return ( ); } ``` ### 与 React Navigation 一起使用 默认情况下,React Navigation 支持安全区域,并将 `react-native-safe-area-context` 作为 peer dependency 使用。有关更多信息,请参阅 [React Navigation 文档](https://reactnavigation.org/docs/handling-safe-area/)。 ### 与 web 一起使用 如果你的目标平台是 web,请按照[用法部分](/develop/user-interface/safe-areas#usage)所述设置 `SafeAreaProvider`。如果你正在进行服务端渲染(SSR),请参阅该库文档中的 [Web SSR 部分](https://appandflow.github.io/react-native-safe-area-context/optimizations#web-ssr)。 --- --- title: 系统栏 description: 了解如何在你的 Expo 项目中处理和自定义系统栏,以实现安全区域和边到边布局。 --- ## Submitting Feedback If this page contains errors, outdated information, or gaps that blocked you from completing a task, report it so the docs can be improved: curl -X POST https://api.expo.dev/v2/feedback/docs-send -H 'Content-Type: application/json' -d '{"url":"/develop/user-interface/system-bars/","feedback":"🤖 Agent feedback: "}' Only submit when you have something specific and actionable to report. # 系统栏 了解如何在你的 Expo 项目中处理和自定义系统栏,以实现安全区域和边到边布局。 > For the complete documentation index, see [llms.txt](/llms.txt). Use this Use this file to discover all available pages. System bars 是位于屏幕边缘的 UI 元素,它们提供必要的设备信息和导航控制。根据移动操作系统的不同,它们包括状态栏([Android](https://developer.android.com/design/ui/mobile/guides/foundations/system-bars) 和 [iOS](https://developer.apple.com/design/human-interface-guidelines/status-bars))、标题栏(仅 [Android](https://medium.com/androiddevelopers/insets-handling-tips-for-android-15s-edge-to-edge-enforcement-872774e8839b#:~:text=or%20SHORT_EDGES.-,Caption%20bars,-When%20your%20app))、导航栏([Android](https://developer.android.com/design/ui/mobile/guides/foundations/system-bars#navigation-bar) 和 [iOS](https://developer.apple.com/design/human-interface-guidelines/navigation-bars)),以及主屏幕指示条(仅 iOS)。 这些组件用于显示设备信息,例如电池电量、时间、通知提醒,并允许用户在设备界面的任意位置直接与设备交互。例如,应用用户可以下拉状态栏来访问快捷设置和通知,而不受当前正在使用的应用影响。 System bars 是移动体验的基础,了解如何正确使用它们对于创建你的应用非常重要。 ## 使用安全区域处理重叠 你的应用内容有时可能会绘制到系统栏后面。为了解决这个问题,你需要通过避免重叠并确保系统栏的控件可见,来正确定位应用内容。 以下指南将带你了解如何使用 `SafeAreaView` 或 hook,直接为屏幕的每一边应用 inset。 [安全区域](/develop/user-interface/safe-areas) — 了解如何为 Expo 项目中的屏幕组件添加安全区域。 ### Android 上的安全区域和边到边布局 在 [Android 上的 edge-to-edge](https://expo.dev/blog/edge-to-edge-display-now-streamlined-for-android) 之前,通常会使用半透明的状态栏和导航栏。采用这种方式时,绘制在这些栏后面的内容实际上已经位于它们下方,因此通常不需要考虑安全区域。 现在,随着 [Android 上的 edge-to-edge](https://expo.dev/blog/edge-to-edge-display-now-streamlined-for-android) 的启用,你需要使用安全区域来确保内容不会与系统栏重叠。 ## 自定义系统栏 系统栏可以自定义,以匹配你的应用设计,并在不同场景下提供更好的可见性。在使用 Expo 时,可使用两个库来实现这一点:`expo-status-bar` 和 `expo-navigation-bar`(仅 Android)。 ### 状态栏配置 状态栏出现在 Android 和 iOS 屏幕顶部。你可以使用 [`expo-status-bar`](/versions/latest/sdk/status-bar) 对其进行自定义。它提供了一个 `StatusBar` 组件,你可以使用它在应用运行时通过 [`style`](/versions/latest/sdk/status-bar#style) 属性或 [`setStatusBarStyle`](/versions/latest/sdk/status-bar#statusbarsetstatusbarstylestyle-animated) 方法来控制状态栏的外观: ```tsx import { StatusBar } from 'expo-status-bar'; export default function RootLayout() { <> {/* 在状态栏中使用浅色文本而不是深色文本,以便在深色背景上提供更高的对比度。 */} ; } ``` > **注意:** 在 Expo 默认模板中,`style` 属性设置为 `auto`。它会根据你的应用当前使用的配色方案(浅色或深色模式)自动选择合适的样式。 要控制 `StatusBar` 的可见性,你可以将 [`hidden`](/versions/latest/sdk/status-bar#hidden) 属性设为 `true`,或者使用 [`setStatusBarHidden`](/versions/latest/sdk/status-bar#statusbarsetstatusbarhiddenhidden-animation) 方法。 **在 Android 上启用 edge-to-edge 后,依赖不透明状态栏的 `expo-status-bar` 功能将 [不可用](https://developer.android.com/about/versions/15/behavior-changes-15#edge-to-edge)**。此时只能自定义样式和可见性。其他属性将不会生效并会发出警告。 ### 导航栏配置(仅 Android) 在 Android 设备上,导航栏显示在屏幕底部。你可以使用 [`expo-navigation-bar`](/versions/latest/sdk/navigation-bar) 库来自定义它。它提供了一个 `NavigationBar` 组件,你可以使用它通过 [`setStyle`](/versions/latest/sdk/navigation-bar#navigationbarsetstylestyle) 方法设置导航栏样式: ```tsx import { Platform } from 'react-native'; import * as NavigationBar from 'expo-navigation-bar'; import { useEffect } from 'react'; useEffect(() => { if (Platform.OS === 'android') { // 设置导航栏样式 NavigationBar.setStyle('dark'); } }, []); ``` 要控制 `NavigationBar` 的可见性,你可以使用 [`setVisibilityAsync`](/versions/latest/sdk/navigation-bar#navigationbarsetvisibilityasyncvisibility) 方法。 **在 Android 上启用 edge-to-edge 后,依赖不透明导航栏的 `expo-navigation-bar` 功能将 [不可用](https://developer.android.com/about/versions/15/behavior-changes-15#edge-to-edge)**。此时只能自定义样式和可见性。其他属性将不会生效并会发出警告。 --- --- title: 字体 description: 了解如何使用本地文件或 Google 字体包将自定义字体集成到你的应用中 --- ## Submitting Feedback If this page contains errors, outdated information, or gaps that blocked you from completing a task, report it so the docs can be improved: curl -X POST https://api.expo.dev/v2/feedback/docs-send -H 'Content-Type: application/json' -d '{"url":"/develop/user-interface/fonts/","feedback":"🤖 Agent feedback: "}' Only submit when you have something specific and actionable to report. # 字体 了解如何使用本地文件或 Google 字体包将自定义字体集成到你的应用中 > For the complete documentation index, see [llms.txt](/llms.txt). Use this Use this file to discover all available pages. Android 和 iOS 自带各自的平台字体。为了提供一致的用户体验并增强应用的品牌形象,你可以使用自定义字体。 本指南介绍了将自定义字体添加并加载到项目中的不同方式,也提供了与字体相关的其他信息。 ## 添加自定义字体 你可以通过两种方式将自定义字体添加到项目中: - 将字体文件添加到本地资源中。例如,放在 **assets/fonts** 目录下的字体文件。 - 安装 Google Font 包。例如,安装 [`@expo-google-fonts/inter`](https://www.npmjs.com/package/@expo-google-fonts/inter) 包。 ### 支持的字体格式 Expo SDK 官方支持在 Android、iOS 和 web 平台上使用 OTF 和 TTF 字体格式。如果你的字体是其他格式,则需要进行高级配置才能在项目中支持该格式。 ### 可变字体 可变字体(包括 OTF 和 TTF 中的可变字体实现)并非在所有平台上都受支持。若要实现完整的平台支持,请使用静态字体。或者,你也可以使用类似 [fontTools](https://fonttools.readthedocs.io/en/latest/varLib/mutator.html) 的工具,从可变字体中提取你想使用的特定轴配置,并将其保存为单独的字体文件。 ### 如何在 OTF 和 TTF 之间选择 如果你使用的字体同时有 OTF 和 TTF 版本,优先选择 OTF。**.otf** 文件比 **.ttf** 文件更小。有时,在某些场景下 OTF 的渲染效果也会稍好一些。 ## 使用本地字体文件 将文件复制到项目的 **assets/fonts** 目录中。 > **assets/fonts** 目录路径是在 React Native 应用中放置字体文件的一种常见约定。如果你遵循自定义约定,也可以把这些文件放在其他位置。 在项目中使用本地字体文件有两种方式: - 使用 [`expo-font` 配置插件](/versions/latest/sdk/font#configuration-in-app-config) 将字体文件嵌入(仅 Android 和 iOS)。 - 在运行时使用 [`useFonts`](/versions/latest/sdk/font#usefontsmap) hook 加载字体文件(Android、iOS 和 web)。 ### 使用 `expo-font` 配置插件 `expo-font` 配置插件允许将一个或多个字体文件嵌入到项目的原生代码中。它支持 Android 和 iOS 的 `ttf` 和 `otf`,而 `woff` 和 `woff2` 仅在 iOS 上受支持。 > **Note:** 配置插件仅在原生平台(Android 和 iOS)上运行。对于 web,请改用 [`useFonts` hook](/develop/user-interface/fonts#with-usefonts-hook)。 由于以下优点,这是向应用添加字体的推荐方式: - 应用在设备上启动时,字体即可立即可用。 - 应用启动时无需额外代码来异步加载字体。 - 由于字体被打包进应用,因此在所有安装了该应用的设备上都能一致地使用字体。 不过,这种方式也有一些限制: - 不适用于 Expo Go,因为这种方式需要[创建开发构建](/develop/development-builds/create-a-build)。 要将字体嵌入项目,请按以下步骤操作: 在项目中添加自定义字体文件后,安装 `expo-font` 库。 ```sh npx expo install expo-font ``` 将配置插件添加到你的 [app config](/versions/latest/config/app#plugins) 文件中。配置必须使用 [`fonts`、`android` 或 `ios`](/versions/latest/sdk/font#configurable-properties) 属性包含字体文件路径,这些属性接受一个或多个字体定义的数组。每个字体文件的路径相对于项目根目录。 下面的示例展示了字体可指定的所有有效方式:可以是一个包含 `fontFamily` 和其他属性的对象数组,也可以是一个字体文件路径数组。 对于 Android,你可以指定 `fontFamily`、`weight`,以及可选的 `style`(默认为 `"normal"`),这样字体会被嵌入为原生 [XML resources](https://developer.android.com/develop/ui/views/text-and-emoji/fonts-in-xml)。如果你在数组中只提供字体文件路径,那么文件名会成为 Android 上的字体族名称。iOS 总是从字体文件本身提取字体族名称。 如果你计划只使用 `fontFamily` 来引用字体,请提供字体路径数组(如下方的 `FiraSans-MediumItalic.ttf` 所示),并遵循我们关于[如何确定要使用哪个字体族名称](/develop/user-interface/fonts#how-to-determine-which-font-family-name-to-use)的建议。 如果你想通过 `fontFamily`、`weight` 和 `style` 的组合来引用字体,请提供对象数组(如下方的 `Inter` 所示)。 ```json { "expo": { "plugins": [ [ "expo-font", { "fonts": [ "./assets/fonts/FiraSans-MediumItalic.ttf" ], "android": { "fonts": [ { "fontFamily": "Inter", "fontDefinitions": [ { "path": "./assets/fonts/Inter-BoldItalic.ttf", "weight": 700, "style": "italic" }, { "path": "./assets/fonts/Inter-Bold.ttf", "weight": 700 } ] } ] }, "ios": { "fonts": ["./assets/fonts/Inter-Bold.ttf", "./assets/fonts/Inter-BoldItalic.ttf"] } } ] ] } } ``` 使用配置插件嵌入字体后,创建一个[新的开发构建](/develop/development-builds/create-a-build),并将其安装到你的设备、Android 模拟器或 iOS 模拟器上。 你可以通过指定 `fontFamily` 样式属性在 `` 中使用该字体。下面的示例与上面配置中定义的字体相对应。 ```tsx Inter Bold Inter Bold Italic Fira Sans Medium Italic ``` 在现有 React Native 项目中使用这种方式? - **Android:** 将字体文件复制到 **android/app/src/main/assets/fonts**。 - **iOS:** 参见 Apple Developer 文档中的 [Adding a Custom Font to Your App](https://developer.apple.com/documentation/uikit/text_display_and_fonts/adding_a_custom_font_to_your_app)。 #### 如何确定要使用哪个字体族名称 - 如果你将字体以文件路径数组的形式提供(如上所述),在 Android 上,文件名(不含扩展名)会成为字体族名称。在 iOS 上,字体族名称会从字体文件本身读取。我们建议将字体文件命名为与其 [PostScript 名称](/develop/user-interface/fonts#what-is-postscript-name-of-a-font)相同,以便两个平台上的字体族名称保持一致。 - 如果你使用对象语法,请提供 “Family Name”。你可以在 macOS 的 Font Book 应用、[fontdrop.info](https://fontdrop.info/) 或其他程序中找到它。 字体文件的 PostScript 名称是什么? 字体文件的 **PostScript 名称** 是按照 Adobe 的 PostScript 标准分配给字体的唯一标识符。操作系统和应用使用它来引用该字体。它不是字体的 **显示名称**。 例如,Inter Black 字体文件的 PostScript 名称是 `Inter-Black`。 _Font Book app 在 macOS 上的截图。_ ### 使用 `useFonts` hook `expo-font` 库中的 `useFonts` hook 允许异步加载字体文件。这个 hook 会跟踪加载状态,并在应用初始化时加载字体。 它适用于所有 Expo SDK 版本以及 Expo Go。要在项目中使用 `useFonts` hook 加载字体,请按以下步骤操作: 在项目中添加自定义字体文件后,安装 `expo-font` 和 `expo-splash-screen` 库。 ```sh npx expo install expo-font expo-splash-screen ``` [`expo-splash-screen`](/versions/latest/sdk/splash-screen) 库提供了 `SplashScreen` 组件,你可以用它在字体加载完成之前阻止应用渲染。 在项目中的顶层组件中,例如根布局(**src/app/_layout.tsx**)文件,使用 `useFonts` hook 映射字体文件: ```tsx import { useFonts } from 'expo-font'; import * as SplashScreen from 'expo-splash-screen'; import {useEffect} from 'react'; SplashScreen.preventAutoHideAsync(); export default function RootLayout() { const [loaded, error] = useFonts({ 'Inter-Black': require('./assets/fonts/Inter-Black.otf'), }); useEffect(() => { if (loaded || error) { SplashScreen.hideAsync(); } }, [loaded, error]); if (!loaded && !error) { return null; } return ( ... ) } ``` 在 React 组件中通过 `fontFamily` 样式属性在 `` 上使用该字体: ```tsx Inter Black ``` ## 使用 Google Fonts Expo 对 [Google Fonts](https://fonts.google.com/) 中列出的所有字体提供一流支持。它们可通过 [`@expo-google-fonts`](https://github.com/expo/google-fonts) 库使用。使用该库中的任意字体包,你都可以快速集成该字体及其变体。 在项目中使用 Google Font 有两种方式: - 使用 [`expo-font` 配置插件](/versions/latest/sdk/font#configuration-in-appjsonappconfigjs) 将已安装的字体嵌入应用。 - 在运行时使用 [`useFonts`](/versions/latest/sdk/font#usefontsmap) hook 异步加载已安装的字体。 ### 使用 `expo-font` 配置插件 > **注意:** 使用 `expo-font` 配置插件嵌入 Google Font,与自行嵌入自定义字体具有相同的优点和限制。更多信息请参见[使用 `expo-font` 配置插件加载本地字体文件](/develop/user-interface/fonts#with-expo-font-config-plugin)。 安装字体包。例如,要使用 Inter Black 字体,请使用下面的命令安装 [`@expo-google-fonts/inter`](https://www.npmjs.com/package/@expo-google-fonts/inter) 包。 ```sh npx expo install expo-font @expo-google-fonts/inter ``` 将配置插件添加到你的 [app config](/versions/latest/config/app#plugins) 文件中。配置必须包含字体文件的路径,并使用 [`fonts`](/versions/latest/sdk/font#configurable-properties) 属性,该属性接受一个或多个字体文件组成的数组。字体文件路径是从 `node_modules` 目录中的字体包定义的。例如,如果你有一个名为 `@expo-google-fonts/inter` 的字体包,那么文件名就是 **Inter_900Black.ttf**。 ```json { "plugins": [ [ "expo-font", { "fonts": ["node_modules/@expo-google-fonts/inter/900Black/Inter_900Black.ttf"] } ] ] } ``` 通过配置插件嵌入字体后,创建一个[新的开发构建](/develop/development-builds/create-a-build),并将其安装到你的设备、Android 模拟器或 iOS 模拟器上。 在 Android 上,你可以使用字体文件名。例如,`Inter_900Black`。在 iOS 上,使用字体及其字重名称([PostScript 名称](/develop/user-interface/fonts#what-is-postscript-name-of-a-font))。下面的示例演示了如何使用 [`Platform`](https://reactnative.dev/docs/platform-specific-code#platform-module) 为每个平台选择正确的字体族名称: ```tsx import { Platform } from 'react-native'; // 在 React 组件内部: Inter Black ``` ### 使用 `useFonts` hook > **注意:** 使用 `useFonts` hook 加载 Google Font,与自行嵌入自定义字体具有相同的优点和限制。更多信息请参见[使用 `useFonts` hook 加载本地字体文件](/develop/user-interface/fonts#with-usefonts-hook)。 每个 Google Fonts 包都提供了 `useFonts` hook 来异步加载字体。该 hook 会跟踪加载状态,并在应用初始化时加载字体。字体包还会导入字体文件,因此你无需显式导入它。 安装 Google Fonts 包、`expo-font` 和 `expo-splash-screen` 库。 ```sh npx expo install @expo-google-fonts/inter expo-font expo-splash-screen ``` [`expo-splash-screen`](/versions/latest/sdk/splash-screen) 库提供了 `SplashScreen` 组件,你可以用它在字体加载完成之前阻止应用渲染。 安装字体包后,在项目中的顶层组件中使用 `useFonts` hook 映射字体,例如根布局(**src/app/_layout.tsx**)文件: ```tsx // 其余导入语句 import { Inter_900Black, useFonts } from '@expo-google-fonts/inter'; import * as SplashScreen from 'expo-splash-screen'; import {useEffect} from 'react'; SplashScreen.preventAutoHideAsync(); export default function RootLayout() { const [loaded, error] = useFonts({ Inter_900Black, }); useEffect(() => { if (loaded || error) { SplashScreen.hideAsync(); } }, [loaded, error]); if (!loaded && !error) { return null; } return ( ... ) } ``` 在 React 组件中,通过在 `` 上使用 `fontFamily` 样式属性来使用该字体: ```tsx Inter Black ``` ## 附加信息 ### 最小示例 [expo-font 用法](/versions/latest/sdk/font#usage) — expo-font — 请参阅 Expo Fonts API 参考中的用法部分,了解使用自定义字体的最小示例。 ### 超出 OTF 和 TTF 之外的格式 如果你的字体格式不是 OTF 或 TTF,你必须[自定义 Metro bundler 配置,将其作为额外资源包含进去](/guides/customizing-metro#adding-more-file-extensions-to-assetexts),它才会生效。在某些情况下,渲染平台不支持的字体格式可能会导致你的应用崩溃。 作为参考,下表提供了在各个原生平台上可用的格式列表: | 格式 | Android | iOS | Web | | --- | --- | --- | --- | | bdf | ✗ | ✗ | ✗ | | dfont | ✓ | ✗ | ✗ | | eot | ✗ | ✗ | ✓ | | fon | ✗ | ✗ | ✗ | | otf | ✓ | ✓ | ✓ | | ps | ✗ | ✗ | ✗ | | svg | ✗ | ✗ | ✓ | | ttc | ✗ | ✗ | ✗ | | ttf | ✓ | ✓ | ✓ | | woff | ✗ | ✓ | ✓ | | woff2 | ✗ | ✓ | ✓ | ### 平台内置字体 如果你不想通过指定 `fontFamily` 来使用自定义字体,那么将使用平台的默认字体。每个平台都有一组内置字体。在 Android 上,默认字体是 Roboto。在 iOS 上,是 SF Pro。 平台的默认字体通常都很易读。不过,如果系统默认字体被更改为另一种不易读的字体,也不必惊讶。在这种情况下,请使用你的自定义字体,这样你就可以精确控制用户将看到的内容。 ### 处理 `@expo/vector-icons` 的首次加载 当 `@expo/vector-icons` 库中的图标第一次加载时,它们在你的应用中会显示为不可见图标。一旦加载完成,它们就会被缓存,供应用后续使用。为了避免在应用首次加载时显示不可见图标,请在初始加载界面期间使用 [`useFonts`](/versions/latest/sdk/font#usefontsmap) 进行预加载。例如: ```tsx import { useFonts } from 'expo-font'; import Ionicons from '@expo/vector-icons/Ionicons'; export default function RootLayout() { useFonts([require('./assets/fonts/Inter-Black.otf', Ionicons.font)]); return ( ... ) } ``` 现在,你可以在 React 组件中使用 `Ionicons` 库中的任意图标: ```tsx ``` [图标](/guides/icons) — 了解如何在你的 Expo 应用中使用各种类型的图标,包括矢量图标、自定义图标字体、图标图片和图标按钮。 ### 直接从网页加载远程字体 > **如果你正在加载远程字体,请确保它们是从已正确配置 CORS 的源提供的**。如果你不这样做,你的远程字体在 web 平台上可能无法正常加载。 从本地资源加载字体是在应用中加载字体最安全的方式。将字体作为本地资源包含后,当你将应用提交到应用商店时,这些字体会与应用下载包一起打包,并可立即使用。你无需担心 CORS 或其他潜在问题。 不过,直接从网页加载字体文件的方法是将 `require('./assets/fonts/FontName.otf')` 替换为字体的 URL,如下面的示例所示。 ```tsx import { useFonts } from 'expo-font'; import { Text, View, StyleSheet } from 'react-native'; export default function App() { const [loaded, error] = useFonts({ 'Inter-SemiBoldItalic': 'https://rsms.me/inter/font-files/Inter-SemiBoldItalic.otf?v=3.12', }); if (!loaded || !error) { return null; } return ( Inter SemiBoldItalic 平台默认字体 ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', }, }); ``` --- --- title: 资源 description: 了解如何在你的项目中使用静态资源,包括图片、视频、声音、数据库文件和字体。 --- ## Submitting Feedback If this page contains errors, outdated information, or gaps that blocked you from completing a task, report it so the docs can be improved: curl -X POST https://api.expo.dev/v2/feedback/docs-send -H 'Content-Type: application/json' -d '{"url":"/develop/user-interface/assets/","feedback":"🤖 Agent feedback: "}' Only submit when you have something specific and actionable to report. # 资源 了解如何在你的项目中使用静态资源,包括图片、视频、声音、数据库文件和字体。 > For the complete documentation index, see [llms.txt](/llms.txt). Use this Use this file to discover all available pages. A **静态资源** 是与应用的二进制文件(原生二进制)一起打包的文件。此类文件不属于应用的 JavaScript bundle,其中包含你的应用代码。静态资源的常见类型包括图片、视频、音频、SQLite 数据库文件和字体。这些资源可以从你的项目中本地提供,也可以通过网络远程提供。 本指南介绍了在项目中加载和使用静态资源的不同方式,并提供了有关如何优化和压缩资源的补充信息。 ## 本地提供资源 当资源存储在项目的文件系统中时,可以在构建时将其嵌入到应用二进制文件中,或者在运行时加载。你可以像使用 JavaScript 模块一样,通过 `require` 或 `import` 语句来导入它。 例如,要在 **App.js** 中渲染一张名为 **example.png** 的图片,你可以使用 `require` 从项目的 **assets/images** 目录导入该图片,并将其传递给 `` 组件: ```tsx ``` 在上面的示例中,打包器会读取所导入图片的元数据,并自动提供宽度和高度。有关更多信息,请参阅 [静态图片资源](https://reactnative.dev/docs/images#static-image-resources)。 `expo-image` 和 `expo-file-system` 等库与 `` 组件处理本地资源的方式类似。 ### 资源如何在本地提供 本地存储的资源在开发环境中通过 HTTP 提供。在生产应用中,它们会在构建时自动打包进应用二进制文件,并在设备上从磁盘提供。 ### 使用 `expo-asset` 配置插件在构建时加载资源 要在构建时加载资源,你可以使用 `expo-asset` 库中的 [配置插件](/versions/latest/sdk/asset#example-appjson-with-config-plugin)。此插件会将资源文件嵌入到你的原生项目中。 安装 `expo-asset` 库。 ```sh npx expo install expo-asset ``` 将配置插件添加到项目的 [app config](/versions/latest/config/app#plugins) 文件中。配置必须使用 [`assets`](/versions/latest/sdk/asset#configurable-properties) 属性包含资源文件的路径,该属性接受一个或多个要链接到原生项目的文件或目录数组。 由于 app config 文件位于项目根目录中,因此每个资源文件的路径都必须相对于项目根目录。 ```json { "expo": { "plugins": [ [ "expo-asset", { "assets": ["./assets/images/example.png"] } ] ] } } ``` 使用配置插件嵌入资源后,创建一个新的 [开发构建](/develop/development-builds/create-a-build)。现在,你可以在项目中导入并使用该资源,而无需使用 `require` 或 `import` 语句。 例如,上面的配置插件已链接 **example.png**。你可以直接将其导入到组件中,并使用其资源名称作为 URI。请注意,在不使用 `require` 渲染资源时,需要显式提供宽度 / 高度。 ```tsx import { Image } from 'expo-image'; ... export default function HomeScreen() { return ; } ``` > **信息** `expo-asset` 配置插件支持不同的文件格式。有关这些格式的更多信息,请参阅 [Assets API 参考](/versions/latest/sdk/asset#configurable-properties)。如果你没有看到配置插件支持的文件格式,可以使用 [`useAssets`](/develop/user-interface/assets#load-an-asset-at-runtime-with-useassets-hook) Hook 在运行时加载资源。 ### 使用 `useAssets` Hook 在运行时加载资源 `expo-asset` 库中的 `useAssets` Hook 允许异步加载资源。此 Hook 会下载资源并将其存储在本地,资源加载完成后会返回该资源实例的列表。 安装 `expo-asset` 库。 ```sh npx expo install expo-asset ``` 在屏幕组件中从 `expo-asset` 库导入 [`useAssets`](/versions/latest/sdk/asset#useassetsmoduleids) Hook: ```tsx import { useAssets } from 'expo-asset'; export default function HomeScreen() { const [assets, error] = useAssets([ require('path/to/example-1.jpg'), require('path/to/example-2.png'), ]); return assets ? : null; } ``` ## 远程提供资源 当资源是远程提供时,它不会在构建时打包进应用二进制文件中。如果资源托管在远程位置,你可以在项目中使用该资源的 URL。例如,将 URL 传递给 `` 组件以渲染远程图片: ```jsx import { Image } from 'expo-image'; ... function App() { return ( ); } ``` 通过网页 URL 提供的远程图片,其可用性无法保证,因为可能无法连接互联网,或者资源可能已被移除。 此外,远程加载资源还要求你提供资源元数据。在上面的示例中,由于打包器无法获取图片的宽度和高度,因此这些值被显式传递给 `` 组件。如果不这样做,图片默认会显示为 0px x 0px。 ## 其他信息 ### 手动优化方法 #### 图片 你可以使用以下工具压缩图片: - [`guetzli`](https://github.com/google/guetzli) - [`pngcrush`](https://pmt.sourceforge.io/pngcrush/) - [`optipng`](http://optipng.sourceforge.net/) 某些图片优化器是无损的。它们会对图片重新编码,使其更小,同时不会改变或丢失显示的像素。当你需要确保每个像素都与原始图片完全一致时,无损优化器和像 PNG 这样的无损图片格式是不错的选择。 其他图片优化器是有损的。优化后的图片与原始图片不同。通常,有损优化器更高效,因为它们会丢弃可减少文件大小但对人眼几乎没有差别的视觉信息。像 `imagemagick` 这样的工具可以使用 [SSIM](https://en.wikipedia.org/wiki/Structural_similarity) 等比较算法来展示两张图片的相似程度。优化后图片与原始图片相似度超过 95% 而文件大小远小于原始文件大小的情况非常常见。 #### 其他资源 对于 GIF 或视频等资源,或者非代码和非图片资源,如何优化和压缩这些资源由你自行决定。 > **注意**:GIF 是一种非常低效的格式。现代视频编解码器可以在更高质量下生成显著更小的文件大小。 ### 字体 有关如何向应用添加自定义字体的更多信息,请参阅 [添加自定义字体](/develop/user-interface/fonts#add-a-custom-font)。 --- --- title: 颜色主题 description: 了解如何在你的应用中支持亮色和暗色模式。 --- ## Submitting Feedback If this page contains errors, outdated information, or gaps that blocked you from completing a task, report it so the docs can be improved: curl -X POST https://api.expo.dev/v2/feedback/docs-send -H 'Content-Type: application/json' -d '{"url":"/develop/user-interface/color-themes/","feedback":"🤖 Agent feedback: "}' Only submit when you have something specific and actionable to report. # 颜色主题 了解如何在你的应用中支持亮色和暗色模式。 > For the complete documentation index, see [llms.txt](/llms.txt). Use this Use this file to discover all available pages. 应用程序通常支持浅色和深色配色方案。以下是在 Expo 项目中同时支持这两种模式的示例: ## 配置 > 对于 Android 和 iOS 项目,需要额外配置才能支持在浅色和深色模式之间切换。对于 web,不需要额外配置。 要配置支持的外观样式,你可以在项目的 [app config](/versions/latest/config/app) 中使用 [`userInterfaceStyle`](/versions/latest/config/app#userinterfacestyle) 属性。默认情况下,当你使用 [默认模板](/get-started/create-a-project) 创建新项目时,此属性会设置为 `automatic`。 下面是一个配置示例: ```json { "expo": { "userInterfaceStyle": "automatic" } } ``` 你也可以通过将 [`android.userInterfaceStyle`](/versions/latest/config/app#userinterfacestyle-2) 或 [`ios.userInterfaceStyle`](/versions/latest/config/app#userinterfacestyle-1) 设置为首选值,来为特定平台配置 `userInterfaceStyle` 属性。 > 如果缺少此属性,应用将默认使用 `light` 样式。 当你创建开发构建时,必须安装 [`expo-system-ui`](/versions/latest/sdk/system-ui#installation) 以支持 Android 的外观样式。否则,`userInterfaceStyle` 属性会被忽略。 ```sh npx expo install expo-system-ui ``` 如果项目配置错误,并且没有安装 `expo-system-ui`,终端中会显示以下警告: ```sh » android: userInterfaceStyle: 在你的项目中安装 expo-system-ui 以启用此功能。 ``` 你也可以使用以下命令检查项目是否配置错误: ```sh npx expo config --type introspect ``` 使用原生 React Native 应用? #### Android 确保在 **AndroidManifest.xml** 中的 `MainActivity`(以及任何其他需要此行为的 activity)上包含 `uiMode` 标志: ```xml ``` 在 **MainActivity.java** 中实现 `onConfigurationChanged` 方法: ```java import android.content.Intent; import android.content.res.Configuration; public class MainActivity extends ReactActivity { ... @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); Intent intent = new Intent("onConfigurationChanged"); intent.putExtra("newConfig", newConfig); sendBroadcast(intent); } ... } ``` #### iOS 你可以在应用的 **Info.plist** 中使用 [`UIUserInterfaceStyle`](https://developer.apple.com/documentation/bundleresources/information_property_list/uiuserinterfacestyle) 键来配置支持的样式。使用 `Automatic` 可同时支持浅色和深色模式。 ### 支持的外观样式 `userInterfaceStyle` 属性支持以下值: - `automatic`:跟随系统外观设置,并在用户进行任何更改时通知。 - `light`:将应用限制为仅支持浅色主题。 - `dark`:将应用限制为仅支持深色主题。 ## 检测配色方案 要在项目中检测配色方案,请使用 `react-native` 中的 `Appearance` 或 `useColorScheme`: ```tsx import { Appearance, useColorScheme } from 'react-native'; ``` 然后,你可以像下面这样使用 `useColorScheme()` 钩子: ```tsx function MyComponent() { let colorScheme = useColorScheme(); if (colorScheme === 'dark') { // 渲染一些深色内容 } else { // 渲染一些浅色内容 } } ``` 在某些情况下,你可能会发现通过 [`Appearance.getColorScheme()` 获取当前配色方案,或者使用 `Appearance.addChangeListener()` 监听变化](https://reactnative.dev/docs/appearance) 会很有帮助。 ## 其他信息 ### 最小示例 ```tsx import { Text, StyleSheet, View, useColorScheme } from 'react-native'; import { StatusBar } from 'expo-status-bar'; export default function App() { const colorScheme = useColorScheme(); const themeTextStyle = colorScheme === 'light' ? styles.lightThemeText : styles.darkThemeText; const themeContainerStyle = colorScheme === 'light' ? styles.lightContainer : styles.darkContainer; return ( 配色方案:{colorScheme} ); } const styles = StyleSheet.create({ container: { flex: 1, alignItems: 'center', justifyContent: 'center', }, text: { fontSize: 20, }, lightContainer: { backgroundColor: '#d0d0c0', }, darkContainer: { backgroundColor: '#242c40', }, lightThemeText: { color: '#242c40', }, darkThemeText: { color: '#d0d0c0', }, }); ``` ### 提示 在开发项目时,你可以使用以下快捷键更改模拟器或设备的外观: - 如果使用 Android 模拟器,你可以运行 `adb shell "cmd uimode night yes"` 来启用深色模式,运行 `adb shell "cmd uimode night no"` 来禁用深色模式。 - 如果使用实体 Android 设备或 Android 模拟器,你可以在设备设置中切换系统深色模式设置。 - 如果在本地使用 iOS 模拟器,你可以使用 Cmd ⌘ + Shift + a 快捷键在浅色和深色模式之间切换。 --- --- title: 动画 description: 了解如何集成 React Native 动画并在你的 Expo 项目中使用它。 --- ## Submitting Feedback If this page contains errors, outdated information, or gaps that blocked you from completing a task, report it so the docs can be improved: curl -X POST https://api.expo.dev/v2/feedback/docs-send -H 'Content-Type: application/json' -d '{"url":"/develop/user-interface/animation/","feedback":"🤖 Agent feedback: "}' Only submit when you have something specific and actionable to report. # 动画 了解如何集成 React Native 动画并在你的 Expo 项目中使用它。 > For the complete documentation index, see [llms.txt](/llms.txt). Use this Use this file to discover all available pages. 动画是增强并提供更佳用户体验的绝佳方式。在你的 Expo 项目中,你可以使用 React Native 的 [Animated API](https://reactnative.dev/docs/next/animations)。不过,如果你想使用性能更好、更高级的动画,可以使用 [`react-native-reanimated`](https://docs.swmansion.com/react-native-reanimated/) 库。它提供了一个 API,简化了创建平滑、强大且易于维护的动画的过程。 ## 安装 如果你是使用[默认模板](/get-started/create-a-project)创建的项目,则可以跳过安装 `react-native-reanimated`。该库已经预装。否则,请运行以下命令进行安装: ```sh npx expo install react-native-reanimated ``` ## 使用 ### 最小示例 以下示例展示了如何使用 `react-native-reanimated` 库创建一个简单动画。有关该 API 和高级用法的更多信息,请参阅 [`react-native-reanimated` 文档](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/your-first-animation)。 ```tsx import Animated, { useSharedValue, withTiming, useAnimatedStyle, Easing, } from 'react-native-reanimated'; import { View, Button, StyleSheet } from 'react-native'; export default function AnimatedStyleUpdateExample() { const randomWidth = useSharedValue(10); const config = { duration: 500, easing: Easing.bezier(0.5, 0.01, 0, 1), }; const style = useAnimatedStyle(() => { return { width: withTiming(randomWidth.value, config), }; }); return (