This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

配置多个应用变体

编辑页面

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


在本章中,我们将配置项目,使其能够在同一台设备上同时运行多个构建类型(development、preview、production)。这种设置将使我们能够测试应用开发的不同阶段,而无需卸载并重新安装不同版本。

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

使用唯一的 bundle 标识符配置 development、preview 和 production 应用变体,以便它们能在同一台设备上并排运行。


每个变体都需要一个唯一的 Android Application ID 和 iOS Bundle Identifier,以便能在一台设备上同时安装。以下是这些 ID 在我们的 app.json 文件中的设置方式:

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

1

添加 app.config.js 以进行动态配置

app.json 在 JSON 文件中包含与应用相关的配置。它是静态的,如果我们想使用某些属性的动态值,它并不理想。我们将基于环境变量为所有构建配置文件添加不同的 Android Application ID 和 iOS Bundle Identifier。

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

2

根据环境更新动态值

为了识别构建类型,让我们在 app.config.js 中为 developmentpreview 构建配置文件添加两个环境变量,分别叫做 IS_DEVIS_PREVIEW

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

然后,添加两个函数,动态更改应用名称、Android Application ID 和 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(开发版)'; } if (IS_PREVIEW) { return 'StickerSmash(预览版)'; } return 'StickerSmash:表情贴纸'; };

我们将使用 getAppName() 为应用分配动态的 name 值,并使用 getUniqueIdentifier() 来区分 development 和 preview 构建的 android.packageios.bundleIdentifier

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

3

配置 eas.json

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

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

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

4

运行开发服务器

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

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

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

Terminal
npm run dev

此脚本会在本地计算 app.config.js,并为 development 配置文件加载环境变量。

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

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

总结

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: Chapter 6: Create and share internal distribution build