likes
comments
collection
share

解决CocoaPods安装卡住反应慢或失败

作者站长头像
站长
· 阅读数 5

使用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

如果不想用命令也可以直接进入这个目录,双击安装,都会弹出安装界面。 解决CocoaPods安装卡住反应慢或失败 安装后,重新使用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.