Expo Classic迁移至EAS后,无法复现Release Channel环境切换功能
升级Expo至最新版后,替换
expo publish为eas build的环境配置方案 旧版环境切换实现
此前通过Updates.releaseChannel实现环境专属配置,根据发布渠道动态切换API地址,代码如下:
export function getEnvConst() { if (Updates.releaseChannel.startsWith("prod")) { return { apiUrl: "https://prodUrl.com/" }; } else if (Updates.releaseChannel.startsWith("staging")) { return { apiUrl: "https://stagingUrl.com/" }; } else if (Updates.releaseChannel.startsWith("uat")) { return { apiUrl: "https://uatUrl.com/" }; } else { return { apiUrl: "https://devUrl.com/" }; } }
发布时通过指定渠道命令切换环境:
expo publish --release-channel prod expo publish --release-channel uat expo publish --release-channel staging
开发时执行npm start会自动进入else分支使用开发环境地址(无发布渠道)。
迁移eas build后的问题
尝试两种方法均未复现原有逻辑:
- EAS Channel配置无效
在eas.json中配置了不同profile对应不同channel,将代码中releaseChannel替换为channel后无效果,执行eas build -p android --profile staging未达到预期。eas.json配置如下:
{ "cli": { "version": ">= 3.8.1" }, "build": { "development": { "developmentClient": true, "distribution": "internal", "ios": { "resourceClass": "m-medium" }, "channel": "dev" }, "staging": { "android": { "buildType": "apk" }, "ios": { "resourceClass": "m-medium" }, "channel": "staging" }, "uat": { "distribution": "internal", "ios": { "resourceClass": "m-medium" }, "channel": "uat" }, "production": { "ios": { "resourceClass": "m-medium" }, "channel": "production" } }, "submit": { "production": {} } }
- 环境变量在开发模式无法读取
在eas.json中添加env配置后,代码中export const apiUrl = process.env.API_URL在执行npm start时无法读取到值,暂未测试eas build。相关配置如下:
{ "cli": { "version": ">= 3.8.1" }, "build": { "development": { "developmentClient": true, "distribution": "internal", "ios": { "resourceClass": "m-medium" }, "channel": "dev", "env": { "API_URL": "http://devUrl.com/" } }, "production": { "ios": { "resourceClass": "m-medium" }, "channel": "production", "env": { "API_URL": "https://prodUrl.com/" } } }, "submit": { "production": {} }, "development": { "client": "local" }, "env": { "development": { "API_URL": "https://devUrl.com/" } } }
解决方案
方案1:使用EAS Channel结合Updates.channel
EAS的Channel和旧版releaseChannel逻辑类似,调整代码即可适配:
import * as Updates from 'expo-updates'; export function getEnvConst() { // 开发模式下Updates.channel可能为空,做兼容处理 const currentChannel = Updates.channel ?? ''; if (currentChannel.startsWith("prod")) { return { apiUrl: "https://prodUrl.com/" }; } else if (currentChannel.startsWith("staging")) { return { apiUrl: "https://stagingUrl.com/" }; } else if (currentChannel.startsWith("uat")) { return { apiUrl: "https://uatUrl.com/" }; } else { return { apiUrl: "https://devUrl.com/" }; } }
发布更新时需指定对应channel:eas update --channel staging,开发模式下npm start会自动进入else分支使用开发地址,和旧逻辑一致。
方案2:使用环境变量配合expo-constants(推荐)
EAS环境变量需通过expo-constants读取,同时补充开发模式的配置:
- 安装依赖:
npm install expo-constants - 项目根目录创建
.env.development文件:
API_URL=https://devUrl.com/
- 完善
eas.json的环境变量配置:
{ "cli": { "version": ">= 3.8.1" }, "build": { "development": { "developmentClient": true, "distribution": "internal", "ios": { "resourceClass": "m-medium" }, "channel": "dev", "env": { "API_URL": "https://devUrl.com/" } }, "staging": { "android": { "buildType": "apk" }, "ios": { "resourceClass": "m-medium" }, "channel": "staging", "env": { "API_URL": "https://stagingUrl.com/" } }, "uat": { "distribution": "internal", "ios": { "resourceClass": "m-medium" }, "channel": "uat", "env": { "API_URL": "https://uatUrl.com/" } }, "production": { "ios": { "resourceClass": "m-medium" }, "channel": "production", "env": { "API_URL": "https://prodUrl.com/" } } }, "submit": { "production": {} } }
- 修改代码读取环境变量:
import Constants from 'expo-constants'; export const apiUrl = Constants.expoConfig?.extra?.API_URL ?? 'https://devUrl.com/';
EAS会将env变量注入到Constants.expoConfig.extra,开发模式下expo start自动读取.env.development的变量,实现开发、构建环境统一适配。
内容的提问来源于stack exchange,提问作者Lawrence
相关产品推荐
相关产品推荐

