如何通过EAS配置releaseChannel切换API_URL?解决未定义问题
解决EAS Build中Constants.manifest.releaseChannel未定义的问题
核心原因
在新版EAS Build架构中,Constants.manifest.releaseChannel已被废弃,且直接依赖该字段会因构建流程的变量传递逻辑导致未定义。推荐改用环境变量或动态配置文件来实现渠道切换。
方法1:用EAS环境变量直接传递API地址
- 修改
eas.json,在对应构建profile中添加环境变量:
{ "build": { "productionTest": { "android": { "buildType": "apk" }, "env": { "API_URL": "https://prod.APPNAME.com" } }, "stagingTest": { "android": { "buildType": "apk" }, "env": { "API_URL": "https://staging.APPNAME.com" } } } }
- 在
environment.js中直接读取环境变量:
const API_URL = process.env.API_URL; export default { API_URL };
方法2:通过渠道变量动态判断API地址
如果需要保留渠道判断逻辑,可按以下步骤配置:
- 在
eas.json的profile中传递渠道标识:
{ "build": { "productionTest": { "android": { "buildType": "apk" }, "env": { "RELEASE_CHANNEL": "production" } }, "stagingTest": { "android": { "buildType": "apk" }, "env": { "RELEASE_CHANNEL": "staging" } } } }
- 在
environment.js中根据渠道变量切换地址:
const releaseChannel = process.env.RELEASE_CHANNEL; const API_URL = releaseChannel === 'production' ? 'https://prod.APPNAME.com' : 'https://staging.APPNAME.com'; export default { API_URL };
方法3:改用Constants.expoConfig读取渠道(兼容旧逻辑)
如果坚持使用渠道字段判断,需替换废弃的Constants.manifest为Constants.expoConfig:
- 确保
eas.json的profile配置了releaseChannel:
{ "build": { "productionTest": { "android": { "buildType": "apk" }, "releaseChannel": "production" } } }
- 更新
environment.js的读取逻辑:
import Constants from 'expo-constants'; const releaseChannel = Constants.expoConfig?.releaseChannel; const API_URL = releaseChannel === 'production' ? 'https://prod.APPNAME.com' : 'https://staging.APPNAME.com'; export default { API_URL };
额外注意事项
- 确保EAS CLI为最新版本:执行
npm install -g eas-cli更新 - 本地开发时,可在项目根目录创建
.env文件,添加对应环境变量以匹配构建逻辑 - Expo SDK 48+版本中,
Constants.manifest已完全废弃,必须使用Constants.expoConfig
内容的提问来源于stack exchange,提问作者DarkSide77
相关产品推荐
相关产品推荐

