解決CocoaPods安裝卡住反應慢或失敗

旺仔小小饅頭發表於2021-07-22

使用Flutter打包IOS,都會有三方依賴。IOS的依賴是用CocoaPods來管理的。使用Flutter doctor會提示安裝CocoaPods.

根據提示安裝CocoaPods,直接安裝就卡在這裡一直不動了。

$sudo gem install cocoapods
複製程式碼

在網上找到個引數,可以檢視安裝進度,在最後加上-V。

# 注意V是大寫
$sudo gem install cocoapods -V 
複製程式碼

看到安裝過程,發現是網的問題,因為gem的源地址在國外

解決gem安裝慢或卡住

  • 使用梯子直接安裝。
  • 將gem的源改為國內映象。

替換源是最簡單的方法

# 替換源
$ gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/
# 檢視替換後的源地址
$ gem sources -l
https://gems.ruby-china.com
# 確保只有 gems.ruby-china.com
複製程式碼

解決Failed to build gem native extension

替換gem源之後安裝到最後又碰到以下問題。

Building native extensions.  This could take a while...
current directory: /Library/Ruby/Gems/2.3.0/gems/ffi-1.14.2/ext/ffi_c
/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/bin/ruby -r ./siteconf20210203-987-tfjdrx.rb extconf.rb
mkmf.rb can't find header files for ruby at /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/include/ruby.h
ERROR:  Error installing cocoapods:
	ERROR: Failed to build gem native extension.

    Building has failed. See above output for more information on the failure.
extconf failed, exit code 1

Gem files will remain installed in /Library/Ruby/Gems/2.3.0/gems/ffi-1.14.2 for inspection.
Results logged to /Library/Ruby/Gems/2.3.0/extensions/universal-darwin-18/2.3.0/ffi-1.14.2/gem_make.out
複製程式碼

Google搜尋發現是MacOS缺少一些標頭檔案。因為我的是10.14。所以直接使用以下命令安裝工具包。

$ open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg
複製程式碼

如果不想用命令也可以直接進入這個目錄,雙擊安裝,都會彈出安裝介面。 macOS_SDK_headers_for_macOS_10.14 安裝後,重新使用sudo gem install cocoapods就能安裝成功了。

最後執行Flutter doctor,IOS顯示正常。

$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 1.22.6, on Mac OS X 10.14.6 18G7016 darwin-x64,
    locale zh-Hans-CN)

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[✓] Xcode - develop for iOS and macOS (Xcode 11.3.1)
[✓] Android Studio (version 4.0)
[!] Connected device
    ! No devices available

! Doctor found issues in 1 category.
複製程式碼

相關文章