配置多个应用变体

了解如何配置动态应用配置以在单个设备上安装多个应用变体。


在本章中,我们将配置项目,以便在单个设备上同时运行多种构建类型(开发版、预览版、生产版)。这种设置将让我们在无需卸载和重新安装不同版本的情况下测试应用开发的各个阶段。

🌐 In this chapter, we'll configure our project to run multiple build types (development, preview, production) on a single device simultaneously. This setup will allow us to test various stages of our app development without the need to uninstall and reinstall different versions.

观看:如何配置多个应用变体
观看:如何配置多个应用变体

每个版本都需要一个独特的 Android 应用 ID 和 iOS 包标识符,以便在同一设备上同时安装。以下是在我们的 app.json 文件中设置这些 ID 的方法:

🌐 Each variant requires a unique Android Application ID and iOS Bundle Identifier to enable simultaneous installations on one device. Here's how the IDs are set up in our app.json file:

app.json
{ "ios": { "bundleIdentifier": "com.yourname.stickersmash" %%placeholder-start%%... %%placeholder-end%% }, "android": { "package": "com.yourname.stickersmash" %%placeholder-start%%... %%placeholder-end%% } }

1

添加 app.config.js 用于动态配置

🌐 Add app.config.js for dynamic configuration

app.json 包含应用相关的配置,以 JSON 文件的形式存在。它是静态的,如果我们想为某些属性使用动态值,这并不理想。我们将根据环境变量为所有构建配置添加不同的 Android 应用 ID 和 iOS 包标识符。

  • 在你的项目根目录下,创建一个名为 app.config.js 的新文件。
  • app.config.js 中,导出一个默认函数,该函数以 config 作为参数。然后我们将解构 config,以复制 app.json 中的所有现有属性。
app.config.js
export default ({ config }) => ({ ...config, });

2

根据环境更新动态值

🌐 Update dynamic values based on environment

为了识别构建类型,让我们在 app.config.js 中为 developmentpreview 构建配置添加两个名为 IS_DEVIS_PREVIEW 的环境变量:

🌐 To identify the build type, let's add two environment variables called IS_DEV andIS_PREVIEW for development and preview build profiles in app.config.js:

app.config.js
const IS_DEV = process.env.APP_VARIANT === 'development'; const IS_PREVIEW = process.env.APP_VARIANT === 'preview';

然后,添加两个函数,用于动态更改应用名称、Android 应用 ID 和 iOS Bundle 标识符:

🌐 Then, add two functions that dynamically change the app name, Android Application ID and iOS Bundle Identifier:

app.config.js
const getUniqueIdentifier = () => { if (IS_DEV) { return 'com.yourname.stickersmash.dev'; } if (IS_PREVIEW) { return 'com.yourname.stickersmash.preview'; } return 'com.yourname.stickersmash'; }; const getAppName = () => { if (IS_DEV) { return 'StickerSmash (Dev)'; } if (IS_PREVIEW) { return 'StickerSmash (Preview)'; } return 'StickerSmash: Emoji Stickers'; };

我们将使用 getAppName() 为应用分配动态的 name 值,并使用 getUniqueIdentifier() 区分开发和预览版本的 android.packageios.bundleIdentifier:

🌐 We'll use getAppName() to assign dynamic name values for the app and getUniqueIdentifier() to differentiate android.package and ios.bundleIdentifier for development and preview builds:

app.config.js
export default ({ config }) => ({ ...config, name: getAppName(), ios: { ...config.ios, bundleIdentifier: getUniqueIdentifier(), }, android: { ...config.android, package: getUniqueIdentifier(), }, });

3

配置 eas.json

🌐 Configure eas.json

eas.json 中,添加 APP_VARIANT 环境变量:

🌐 In eas.json, add the APP_VARIANT environment variable:

eas.json
{ "build": { "development": { "developmentClient": true, "distribution": "internal", "env": { "APP_VARIANT": "development" } }, "preview": { "distribution": "internal", "env": { "APP_VARIANT": "preview" } } %%placeholder-start%%... %%placeholder-end%% } }

运行 eas build --profile development 现在会将 APP_VARIANT 设置为 development

🌐 Running eas build --profile development will now set APP_VARIANT to development.

注意:由于我们更改了 Android 应用 ID 和 iOS Bundle 标识符,EAS CLI 会提示我们为 Android 生成新的密钥库,为 iOS 生成新的配置描述文件。想了解这些步骤的具体内容,请参阅上一章获取更多信息。

由于我们的 ios-simulator 构建配置继承了 development,因此该配置会自动应用于 iOS 模拟器。

🌐 Since our ios-simulator build profile extends development, this configuration is automatically applied for iOS Simulators.

4

运行开发服务器

🌐 Run development server

在构建完成后,按照前几章的相同步骤将其安装到设备或模拟器/模拟器上。

由于我们使用 APP_VARIANT 环境变量来标识我们的开发构建,因此在启动开发服务器时需要将其传递给命令。为此,请在我们项目的 package.json"scripts" 字段中添加一个 dev 脚本:

🌐 Since we're identifying our development build with the APP_VARIANT environment variable, we need to pass it to the command when starting the development server. To do this, add a dev script in the "scripts" field of our project's package.json:

package.json
{ "scripts": { "dev": "APP_VARIANT=development npx expo start" } }

运行 npm run dev 命令以启动开发服务器:

🌐 Run the npm run dev command to start the development server:

Terminal
npm run dev

此脚本将在本地评估 app.config.js 并为 development 配置加载环境变量。

🌐 This script will evaluate app.config.js locally and load the environment variable for the development profile.

现在,我们的开发版本将同时在 Android 和 iOS 上运行,并显示来自 app.config.js 的修改后的应用名称。例如,下面的开发版本正在 iOS 模拟器上运行。可以看到应用名称是 StickerSmash (Dev)

🌐 Now, our development build will run on both Android and iOS, displaying the modified app name from app.config.js. For example, the below development build is running on an iOS Simulator. See that the app name is StickerSmash (Dev):

现在你可以继续使用 app.json 存放静态值,并使用 app.config.js 存放动态值。

🌐 You can now continue using app.json for static values and use app.config.js for dynamic values.

概括

🌐 Summary

Chapter 5: Configure multiple app variants

We successfully created app.config.js just for our dynamic configuration while leaving static configuration in app.json unchanged, added environment variables in eas.json to configure specific build profile, and learned how to start the development server with a custom package.json script.

In the next chapter, learn about what are internal distribution builds, why we need them, and how to create them.

Next: Create and share internal distribution build