統一專案Cocoapods版本和Gem環境

語言: CN / TW / HK

統一專案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 "http://gems.ruby-china.org"

    source "http://rubygems.org" git_source(:github) {|repo_name| "http://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源是不是"http://gems.ruby-china.org",改為"http://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