如何强制Fastlane使用指定Ruby版本解决Codepush运行错误?
强制Fastlane使用指定Ruby版本的解决方案
以下是针对MacOS 13.5环境下,让Fastlane 2.214.0强制使用rbenv配置的Ruby 2.7.4的可行方案:
方案1:确保rbenv Shell配置优先加载
- 检查当前Shell配置文件(MacOS 13默认是
~/.zshrc,旧版是~/.bash_profile),确认存在以下rbenv初始化代码:eval "$(rbenv init - zsh)" export PATH="$HOME/.rbenv/shims:$PATH" - 执行
source ~/.zshrc刷新配置,运行ruby -v确认输出为ruby 2.7.4p191(或对应版本号) - 卸载原Fastlane并重新安装到当前Ruby环境:
gem uninstall fastlane gem install fastlane -v 2.214.0 - 验证Fastlane路径:执行
which fastlane,应返回~/.rbenv/shims/fastlane而非系统路径
方案2:用项目本地Ruby版本锁定
- 在项目根目录执行,生成
.ruby-version文件锁定当前目录的Ruby版本:rbenv local 2.7.4 - 项目根目录创建或修改
Gemfile,明确指定Ruby和依赖版本:ruby '2.7.4' gem 'fastlane', '2.214.0' gem 'xcodeproj' - 安装项目依赖:
bundle install - 后续通过
bundle exec调用Fastlane,强制使用指定环境:bundle exec fastlane android codepush
方案3:直接指定Ruby解释器调用Fastlane
- 获取rbenv管理的Ruby 2.7.4路径:
输出示例:rbenv which ruby/Users/你的用户名/.rbenv/versions/2.7.4/bin/ruby - 用该路径直接调用Fastlane:
/Users/你的用户名/.rbenv/versions/2.7.4/bin/ruby ~/.rbenv/shims/fastlane android codepush - 可添加别名简化调用,在
~/.zshrc中加入:
之后直接执行alias fastlane27="/Users/你的用户名/.rbenv/versions/2.7.4/bin/ruby ~/.rbenv/shims/fastlane"fastlane27 android codepush即可
方案4:移除系统级Fastlane(若存在)
- 检查系统是否安装了全局Fastlane:
若输出包含which fastlane/usr/bin/fastlane,说明存在系统级安装 - 执行卸载:
sudo gem uninstall fastlane - 确保rbenv的shims目录在PATH最前端(参考方案1的配置),重新安装Fastlane到rbenv环境即可
内容的提问来源于stack exchange,提问作者Septatrivanto Wandy
相关产品推荐
相关产品推荐

