You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过EAS配置releaseChannel切换API_URL?解决未定义问题

解决EAS Build中Constants.manifest.releaseChannel未定义的问题

核心原因

在新版EAS Build架构中,Constants.manifest.releaseChannel已被废弃,且直接依赖该字段会因构建流程的变量传递逻辑导致未定义。推荐改用环境变量或动态配置文件来实现渠道切换。


方法1:用EAS环境变量直接传递API地址

  1. 修改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"
      }
    }
  }
}
  1. 在environment.js中直接读取环境变量:
const API_URL = process.env.API_URL;

export default { API_URL };

方法2:通过渠道变量动态判断API地址

如果需要保留渠道判断逻辑,可按以下步骤配置:

  1. 在eas.json的profile中传递渠道标识:
{
  "build": {
    "productionTest": {
      "android": {
        "buildType": "apk"
      },
      "env": {
        "RELEASE_CHANNEL": "production"
      }
    },
    "stagingTest": {
      "android": {
        "buildType": "apk"
      },
      "env": {
        "RELEASE_CHANNEL": "staging"
      }
    }
  }
}
  1. 在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:

  1. 确保eas.json的profile配置了releaseChannel:
{
  "build": {
    "productionTest": {
      "android": {
        "buildType": "apk"
      },
      "releaseChannel": "production"
    }
  }
}
  1. 更新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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 01:45:32