统一项目Cocoapods版本和Gem环境

2,682 阅读2分钟

统一项目Cocoapods版本和Gem环境

通过Gem统一项目Cocoapods版本

  • 检测是否有rbenv,直接在命令行中输入rbenv,看是否会出现版本提示信息。如果没有出现,需要通过

    brew install rbenv
    

    命令来安装.

    如果安装过程中出现如下报错: ==> Pouring ruby-build-20220412.all.bottle.tar.gz Error: No such file or directory @ rb_sysopen - /Users/xxx/Library/Caches/Homebrew/downloads/4ff0b9e0c99846bf0bf4b5be5f73dc12cd27499bdeff6d8b6dede0f2b860f969--ruby-build-20220412.all.bottle.tar.gz
    先安装ruby-build,执行brew install ruby-build
    然后再次执行brew install rbenv即可

  • 使用rbenv管理Ruby版本。3.0.0是最新稳定版本,或者使用rvm install ruby --head安装rvm能够支持的最新ruby版本

    rbenv install 3.0.0
    
    rbenv shell 3.0.0
    

    1.查看当前rvm版本可以管理的ruby版本列表rvm list known,如果rvm版本过低,高版本的ruby是安装不了的

    2.rvm list查看本地安装的ruby版本

    使ruby环境生效

  • 安装后可以查询系统级Gem依赖

    gem list
    

使用Bundler管理工程中的Gem环境

  • 在iOS工程中初始化Bundler环境,在工程主目录中执行 bundle init命令,这时主目录中会生成一个Gemfile文件.

    1.如果遇到如下错误:

    /Users/xxx/.rbenv/versions/3.0.0/lib/ruby/3.0.0/rubygems.rb:281:in find_spec_for_exe': Could not find 'bundler' (1.15.4) required by your /Users/xxx/app/demo/Gemfile.lock. (Gem::GemNotFoundException) To update to the latest version installed on your system, runbundle update --bundler. To install the missing version, rungem install bundler:1.15.4from /Users/xxx/.rbenv/versions/3.0.0/lib/ruby/3.0.0/rubygems.rb:300:inactivate_bin_path' from /Users/xxx/.rbenv/versions/3.0.0/bin/bundle:23:in <main>

    可以根据报错提示先执行gem install bundler:1.15.4,然后执行bundle init

    2.如果已经存在Gemfile文件,仍然执行bundle init,会有如下报错:

    Gemfile already exists at /Users/xxx/app/demo/Gemfile

  • 编辑Gemfile,执行Pod的版本

    # frozen_string_literal: true
    # A sample Gemfile
    # source "https://gems.ruby-china.org"
    source "https://rubygems.org"
    git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
    gem "cocoapods", "1.11.3"
    gem "fastlane"
    gem 'xcodeproj'
    gem 'badge'
    gem 'dotenv'
    
    plugins_path = File.join(File.dirname(__FILE__), 'fastlane', 'Pluginfile')
    eval_gemfile(plugins_path) if File.exist?(plugins_path)
    
  • 在当前主目录中执行 bundle install,执行完成之后,指定版本的Cocoapods就安装成功了。可以通过pod --version命令查看版本号

    1.执行bundle install时,如果有如下报错:

    Could not reach host gems.ruby-china.org. Check your network connection and try again

    检查source源是不是"gems.ruby-china.org",改为"rubygems.org"

    2.如果有如下冲突报错:

    Bundler could not find compatible versions for gem "xcodeproj": In snapshot (Gemfile.lock): xcodeproj (= 1.8.1)

    In Gemfile: xcodeproj

    cocoapods (= 1.11.3) was resolved to 1.11.3, which depends on xcodeproj (>= 1.21.0, < 2.0)

    fastlane was resolved to 2.98.0, which depends on xcodeproj (>= 1.5.7, < 2.0.0)

    Running bundle update will rebuild your snapshot from scratch, using only the gems in your Gemfile, which may resolve the conflict.

    执行bundle update,更新Gemfile.lock即可

  • 在项目主目录下执行 bundle exec gem list 可以查看到当前Cocoapods的版本

  • 执行bundle exec pod install/update就可以使用指定版本的Cocoapods进行install或update