Flutter開発中にcocoapods 使おうとしたらインストールに手こずった話と、インストールしたcocoapods がIntelliJから読み込めなくて困った話
IntelliJ使ってるけど、Android Studio でも多分同じはず
TL;DR
- 最新版のruby を使ってインストールしてください
- gem のpath をきちんと通してください
環境
- macOS 11.5.2
- IntelliJ IDEA 2021.2.1 (Community Edition)
- Flutter 2.5.3 (Dart SDK 2.14.4)
症状
flutter doctor
で以下のようなエラーが出て、iPhone Simulator にアプリがデプロイできなくなった。
[!] Xcode - develop for iOS and macOS
✗ 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 see https://guides.cocoapods.org/using/getting-started.html#installation for instructions.
エラー文面に従ってcocoapods
のインストールを試みるも失敗。
$ 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.6.0/gems/ffi-1.15.4/ext/ffi_c
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/ruby -I /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0 -r ./siteconf20211103-95050-3b6zow.rb extconf.rb
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.
...
...
To see why this extension failed to compile, please check the mkmf.log which can be found here:
/Library/Ruby/Gems/2.6.0/extensions/universal-darwin-20/2.6.0/ffi-1.15.4/mkmf.log
extconf failed, exit code 1
Gem files will remain installed in /Library/Ruby/Gems/2.6.0/gems/ffi-1.15.4 for inspection.
Results logged to /Library/Ruby/Gems/2.6.0/extensions/universal-darwin-20/2.6.0/ffi-1.15.4/gem_make.out
解決法
1. 最新のruby をインストールする
元々入ってたやつ↓
- ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.arm64e-darwin20]
- gem 3.0.3
rbenv
を使って最新版をインストールしてみる(詳しいrbenv の使い方は割愛)
https://github.com/rbenv/rbenv
- ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [arm64-darwin20]
- gem 3.2.22
この状態で試してみたら、すんなりインストールできた
*** LOCAL GEMS ***
cocoapods (1.11.2)
cocoapods-core (1.11.2)
cocoapods-deintegrate (1.0.5)
cocoapods-downloader (1.5.1)
cocoapods-plugins (1.0.0)
cocoapods-search (1.0.1)
cocoapods-trunk (1.6.0)
cocoapods-try (1.2.0)
ちなみに、古いバージョンのcocoapods なら最新版のruby を使わなくてもインストールできるようだが、
flutter 起動時のminimum requirement に引っかかってしまう
2. InelliJ からロードできるようにする
terminal からflutter run
で実行した場合は無事起動できるようになったのだが、なぜかIDEからは起動できないという事象が発生した
調べてみると、同じ症状に悩まされている人も
gem のpath をbashrc
やzshrc
に記述して再起動すると解決する
export PATH=$HOME/.rbenv/shims/gem:$PATH
どうやらGUI経由で起動されたAndroid Studioと、Terminal経由で起動されたAndroid Studio でPATHの読み込み結果が変わるらしい?
確かに上記のpath を通していない状態でも、open
コマンドでIntelliJ 自体を立ち上げてみたらちゃんとCocoapodsのpathが通っていた