首页指南参考教程

在现有项目中使用 EAS 更新

了解如何在现有 React Native 项目中使用 EAS 更新。


EAS 更新适用于使用 react-native init 创建的裸 React Native 项目。这些项目有 android 和 ios 目录,你可以直接修改原生文件。

¥EAS Update works with bare React Native projects created with react-native init. These projects have android and ios directories that you can modify native files directly.

配置裸 React Native 项目的步骤与配置 Expo 项目的步骤相同。但是,你可能需要编辑 eas update:configure 生成的一些代码,具体取决于你构建和运行项目的方式。

¥The steps for configuring a bare React Native project are identical to the steps for configuring an Expo project. However, you may need to edit some of the code eas update:configure generates depending on how you build and run your project.

应用配置

¥App config

eas update:configure 会将两个值添加到你项目的应用配置中。

¥The eas update:configure will add two values to your project's app config.

app.json
{
  "expo": {
    "runtimeVersion": "1.0.0",
    "updates": {
      "url": "https://u.expo.dev/..."
      %%placeholder-start%%... %%placeholder-end%%
    }
    %%placeholder-start%%... %%placeholder-end%%
  }
}

runtimeVersion 属性保证构建的原生代码和更新之间的兼容性。每当你更改项目中的任何原生代码时,都需要手动设置此值。阅读 我们关于运行时版本的文档 并学习如何 避免发布不良更新

¥The runtimeVersion property guarantees compatibility between a build's native code and an update. It's necessary to set this value manually whenever you make a change to any native code in your project. Read our doc on runtime versions and learn how to avoid publishing bad updates.

updates.url 属性是“https://u.expo.dev”域,后跟 EAS 服务器上的项目 ID。如果直接访问该 URL,你将看到有关缺少标头的错误。你可以通过向 URL 添加三个查询参数来查看清单:runtime-versionchannel-nameplatform。如果我们发布运行时版本为 1.0.0、通道为 production、平台为 android 的更新,你可以访问的完整 URL 将类似于以下内容:

¥The updates.url property is the "https://u.expo.dev" domain followed by your project's ID on EAS servers. If you go to the URL directly, you'll see an error about missing a header. You can see a manifest by adding three query parameters to the URL: runtime-version, channel-name, and platform. If we published an update with a runtime version of 1.0.0, a channel of production, and a platform of android, the full URL you could visit would be similar to this:

https://u.expo.dev/your-project-id?runtime-version=1.0.0&channel-name=production&platform=android

EAS 配置和原生文件

¥EAS config and native files

要生成 eas.json 文件,请运行 eas build:configure。此命令将创建 eas.json 文件,并且还将修改 android 目录中的 AndroidManifest.xml 文件和 ios 目录中的 Expo.plist 文件。

¥To generate the eas.json file, run eas build:configure. This command will create the eas.json file, and it will also modify the AndroidManifest.xml file inside the android directory and the Expo.plist file inside the ios directory.

在 eas.json 内,你需要将 channel 属性添加到你想要向其发送更新的每个构建配置文件。假设你使用默认的 eas.json 配置,我们建议将 channel 属性添加到 previewproduction 构建配置文件中。

¥Inside eas.json, you'll want to add channel properties to each build profile you'd like to send updates to. Assuming you're using the default eas.json configuration, we recommend adding channel properties to the preview and production build profiles.

eas.json
{
  "cli": {
    "version": ">= 2.1.0"
  },
  "build": {
    "development": {
      "developmentClient": true,
      "distribution": "internal"
    },
    "preview": {
      "distribution": "internal",
      "channel": "preview"
    },
    "production": {
      "channel": "production"
    }
  },
  "submit": {
    "production": {}
  }
}

在 android/app/src/main/AndroidManifest.xml 中,你将看到以下添加内容:

¥Inside android/app/src/main/AndroidManifest.xml, you'll see the following additions:

android/app/src/main/AndroidManifest.xml
<meta-data android:name="expo.modules.updates.EXPO_UPDATE_URL" android:value="https://u.expo.dev/your-project-id"/>
<meta-data android:name="expo.modules.updates.EXPO_RUNTIME_VERSION" android:value="@string/expo_runtime_version"/>

在 android/app/src/main/res/values/strings.xml 中,你将在 resources 对象中看到 expo_runtime_version 字符串条目:

¥Inside android/app/src/main/res/values/strings.xml, you'll see the expo_runtime_version string entry in the resources object:

EXPO_UPDATE_URL 值应包含你的项目 ID。

¥The EXPO_UPDATE_URL value should contain your project's ID.

在 ios/project-name/Supporting/Expo.plist 内,你将看到以下添加内容:

¥Inside ios/project-name/Supporting/Expo.plist, you'll see the following additions:

ios/project-name/Supporting/Expo.plist
<key>EXUpdatesRuntimeVersion</key>
<string>1.0.0</string>
<key>EXUpdatesURL</key>
<string>https://u.expo.dev/your-project-id</string>

EXUpdatesURL 值应包含你的项目 ID。

¥The EXUpdatesURL value should contain your project's ID.

将项目构建到构建中后,expo-updates 库将使用上面定义的原生配置以及 eas.json 中指定的通道来请求清单。

¥Once you have built your project into a build, the expo-updates library will make requests for manifests with the native configuration defined above, along with the channel specified in eas.json.

手动配置通道

¥Configuring the channel manually

如果你使用 EAS Build 创建构建,则 eas.json 中的通道名称将在构建时自动添加到我们构建的 AndroidManifest.xml 和 Expo.plist 中。如果你使用 EAS Build,则不需要执行以下步骤。

¥If you create a build with EAS Build, the channel name from eas.json will automatically be added to our build's AndroidManifest.xml and Expo.plist at build time. If you're using EAS Build, the following steps are not necessary.

如果你的项目未使用 EAS Build 或者你正在使用 npx expo run:ios --configuration Releasenpx expo run:android --variant release 创建发布版本,则需要在 AndroidManifest.xml 和 Expo.plist 中手动设置通道配置。

¥If your project is not using EAS Build or you are creating release builds with either npx expo run:ios --configuration Release or npx expo run:android --variant release, you'll need to set the channel configuration manually inside both AndroidManifest.xml and Expo.plist.

在 AndroidManifest.xml 中,你需要添加以下内容,并将 your-channel-name 替换为与你的项目匹配的通道:

¥In AndroidManifest.xml, you'll need to add the following, replacing your-channel-name with the channel that matches your project:

android/app/src/main/AndroidManifest.xml
<meta-data android:name="expo.modules.updates.UPDATES_CONFIGURATION_REQUEST_HEADERS_KEY" android:value="{&quot;expo-channel-name&quot;:&quot;your-channel-name&quot;}"/>

在 Expo.plist 中,你需要添加以下内容,将 your-channel-name 替换为与你的项目匹配的通道:

¥In Expo.plist, you'll need to add the following, replacing your-channel-name with the channel that matches your project:

ios/project-name/Supporting/Expo.plist
<key>EXUpdatesRequestHeaders</key>
<dict>
  <key>expo-channel-name</key>
  <string>your-channel-name</string>
</dict>

如果你在 app.json 中设置 requestHeaders 属性,则该配置也会由 npx expo prebuild 生成:

¥This configuration is also generated by npx expo prebuild if you set the requestHeaders property in your app.json:

app.json
{
  "expo": {
    %%placeholder-start%%... %%placeholder-end%%
    "updates": {
      %%placeholder-start%%... %%placeholder-end%%
      "requestHeaders": {
        "expo-channel-name": "your-channel-name"
      }
      %%placeholder-start%%... %%placeholder-end%%
    }
    %%placeholder-start%%... %%placeholder-end%%
  }
}

下一步是什么

¥What's next

使用 EAS 更新设置你的项目后,最终你将对项目进行原生更改。每当发生这种情况时,你都需要更新项目应用配置中的 runtimeVersion。完成后,你将需要进行新的构建,之后你将能够使用 eas update 发送更新。如果你的应用未按预期更新,验证你的配置

¥Once your project is set up with EAS Update, eventually you'll make native changes to your project. Whenever that happens, you'll need to update the runtimeVersion in your project's app config. Once that's done, you'll need to make new builds, after which, you'll be able to send updates with eas update. If your app is not updating as expected, validate your configuration.

Expo 中文网 - 粤ICP备13048890号