在macOS Mojave上配置Flutter环境时出错小记

2020-03-11

最近转岗项目经理后,已经很久没摸过家里面笔记本了,所以今天下班后,特地给Macbook开机了下,然后看到Flutter最近开始势头往上走,所以顺着Flutter官方手册把Flutter环境全部搭建了一遍。

前面都没有什么问题,在最后执行flutter doctor进行检查的时候问题出现了,先是告诉我没安装CocoaPods。

chengyong-Mac:~ chengyong$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.12.13+hotfix.8, on Mac OS X 10.14.6 18G95, locale zh-Hans-US)

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
[✗] Xcode - develop for iOS and macOS
    ✗ Xcode installation is incomplete; a full installation is necessary for iOS development.
      Download at: https://developer.apple.com/xcode/download/
      Or install Xcode via the App Store.
      Once installed, run:
        sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
        sudo xcodebuild -runFirstLaunch
    ✗ CocoaPods not installed.
        CocoaPods is used to retrieve the iOS and macOS platform side's plugin code that responds to your plugin usage
        on the Dart side.
        Without CocoaPods, plugins will not work on iOS or macOS.
        For more info, see https://flutter.dev/platform-plugins
      To install:
        sudo gem install cocoapods
[✓] Android Studio (version 3.6)
[✓] Connected device (1 available)

! Doctor found issues in 1 category.

然后我就按照他的指导,使用sudo gem install cocoapods命令以为可以顺利安装时,此时问题出现了,我心里面开始有点小慌张了,报错信息如下。

chengyong-Mac:~ chengyong$ sudo gem install cocoapods
Building native extensions.  This could take a while...
ERROR:  Error installing cocoapods:
    ERROR: Failed to build gem native extension.

    current directory: /Library/Ruby/Gems/2.3.0/gems/ffi-1.12.2/ext/ffi_c
/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/bin/ruby -r ./siteconf20200311-1195-1fkgxdl.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

extconf failed, exit code 1

Gem files will remain installed in /Library/Ruby/Gems/2.3.0/gems/ffi-1.12.2 for inspection.
Results logged to /Library/Ruby/Gems/2.3.0/extensions/universal-darwin-18/2.3.0/ffi-1.12.2/gem_make.out

此时,我把这里面一些关键信息扔给了Google,Google很快给了我一个stackoverflow的链接,看问题的日期还是很新的问题,这个问题里面并没有正面的回答原提问者的问题,但还是给了一个link指引告诉解决方案,两个链接贴在下面。

https://stackoverflow.com/questions/60481623
https://stackoverflow.com/questions/46377667

然后我们找到第二个链接里面的最高赞回答,按照他的思路来,问题顺利解决。

复盘下问题的原因:回答者的意思是,这是苹果官方的一个bug,对于在macOS 10.14安装完Xcode 11后,Xcode 11附带了macOS 10.15 SDK,并且其中包含了ruby2.6的头文件,但却不包含macOS 10.14的ruby2.3的头文件。既然Xcode 11都已经有了这两个SDK,那么我们不必再安装旧的头文件来污染系统目录,而是在使用ruby的时候直接通过选择该SDK,就可以找到适当的ruby2.3头文件。

切换使用10.14 SDK

sudo xcode-select --switch /Library/Developer/CommandLineTools
ruby -rrbconfig -e 'puts RbConfig::CONFIG["rubyhdrdir"]'

切换回Xcode 11 默认SDK

sudo xcode-select --switch /Applications/Xcode.app

解决完问题后,从github上拉了个开源项目运行了下,顺利跑起来了,而且最新的Android Studio能直接在Android和iPhone设备上同时调试,优秀。

无回应:“在macOS Mojave上配置Flutter环境时出错小记”

发表评论

电子邮件地址不会被公开。 必填项已用*标注