Flutter SDK の iOS 用設定をした記録

環境は macOS High Sierra で、Xcode 9.2、Android Studio 3.0 はインストール済み。


Flutter SDK をインストールした直後、flutter doctor の結果がこんな感じだった。

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel beta, v0.1.5, on Mac OS X 10.13.3 17D102, locale ja-JP)
[✓] Android toolchain - develop for Android devices (Android SDK 27.0.0)
[!] iOS toolchain - develop for iOS devices (Xcode 9.2)
    ✗ libimobiledevice and ideviceinstaller are not installed. To install, run:
        brew install --HEAD libimobiledevice
        brew install ideviceinstaller
    ✗ ios-deploy not installed. To install:
        brew install ios-deploy
    ✗ CocoaPods not installed.
        CocoaPods is used to retrieve the iOS platform side's plugin code that responds to your plugin usage on the Dart side.
        Without resolving iOS dependencies with CocoaPods, plugins will not work on iOS.
        For more info, see https://flutter.io/platform-plugins
      To install:
        brew install cocoapods
        pod setup
[✓] Android Studio (version 3.0)
[!] Connected devices
    ! No devices available

Homebrew の更新

brew で色々インストールしてと書いてあるのでその通り

  • brew install --HEAD libimobiledevice
  • brew install ideviceinstaller
  • brew install ios-deploy

まで実行したところで、エラーが出ていることに気づいた。
ビルドはできたけど、権限がなくて /usr/local 配下にシンボリックリンクが作れないという趣旨の英文。つまり、ファイル自体はインストールできたけど、リンクが貼られていないので正常に使えないという中途半端な状態のようだ。

Qiita に @takustaqu さんが書かれている下記の記事によると、Homebrew がすでにインストールされている状態のまま、再度公式サイトに書かれているインストール手順を実行すればよいという。

High SierraにアップデートされたMacでHomebrewがうまく動かない時
https://qiita.com/takustaqu/items/65aa67286345baac363e

その通りにしたところ、特にエラーもなく Homebrew がインストールし直された。

ここで brew doctor を実行すると、リンクが貼れていないパッケージの一覧が出る。

$ brew doctor
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry or file an issue; just ignore this. Thanks!

Warning: You have unlinked kegs in your Cellar
Leaving kegs unlinked can lead to build-trouble and cause brews that depend on
those kegs to fail to run properly once built. Run `brew link` on these:
  libimobiledevice
  libtool
  libplist
  libtasn1
  usbmuxd
  libusb
$

brew link を使うと足りないリンクを張れるようなので、その通りにして、再度 brew doctor してみる。

$ brew link libimobiledevice
Linking /usr/local/Cellar/libimobiledevice/HEAD-3a37a4e_2... 39 symlinks created
$ brew link libtool
Linking /usr/local/Cellar/libtool/2.4.6_1... 20 symlinks created
$ brew link libplist
Linking /usr/local/Cellar/libplist/2.0.0... 12 symlinks created
$ brew link libtasn1
Linking /usr/local/Cellar/libtasn1/4.13... 52 symlinks created
$ brew link usbmuxd
Linking /usr/local/Cellar/usbmuxd/1.0.10_1... 7 symlinks created
$ brew link libusb
Linking /usr/local/Cellar/libusb/1.0.21... 6 symlinks created
$ brew doctor
Your system is ready to brew.
$

ちゃんと入れられたようだ。

CocoaPods の更新

brew install cocoapods したところ、またも、インストールはできたけどシンボリックリンクが作れないとエラーが出た。ただし先ほどとは違って、作ろうとした場所に、すでにファイルがあるためらしい。

$ brew install cocoapods
==> Downloading https://homebrew.bintray.com/bottles/cocoapods-1.4.0.high_sierra
######################################################################## 100.0%
==> Pouring cocoapods-1.4.0.high_sierra.bottle.tar.gz
Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink bin/pod
Target /usr/local/bin/pod
already exists. You may want to remove it:
  rm '/usr/local/bin/pod'

To force the link and overwrite all conflicting files:
  brew link --overwrite cocoapods

To list all files that would be deleted:
  brew link --overwrite --dry-run cocoapods

Possible conflicting files are:
/usr/local/bin/pod
/usr/local/bin/xcodeproj
==> Summary
🍺  /usr/local/Cellar/cocoapods/1.4.0: 8,818 files, 13.0MB
$

さっぱり覚えてないけど、入れたんだっけ。と思いつつ、試しに次の手順を実行してみたところ、今度は Ruby 2.0 がないというエラー。

 $ pod setup
 -bash: /usr/local/bin/pod: /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby: bad interpreter: No such file or directory
 $ 

ググってみると、原因は、macOS High Sierra から、標準で入っている Ruby のバージョンが 2.0 から 2.3 へ上がったためらしい。RubyGems を更新すればいいのかな? と思ってやってみたけど、またエラー。

$ sudo gem update --system
Password:
Updating rubygems-update
Fetching: rubygems-update-2.7.6.gem (100%)
Successfully installed rubygems-update-2.7.6
Parsing documentation for rubygems-update-2.7.6
Installing ri documentation for rubygems-update-2.7.6
Installing darkfish documentation for rubygems-update-2.7.6
Done installing documentation for rubygems-update after 45 seconds
Parsing documentation for rubygems-update-2.7.6
Done installing documentation for rubygems-update after 0 seconds
Installing RubyGems 2.7.6
ERROR:  While executing gem ... (Errno::EPERM)
    Operation not permitted @ rb_sysopen - /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/bin/gem
$ 

エラーメッセージで検索したところ、下記の記事で対処方法が紹介されていた。

High Sierraでcocoapodsが使えなくなった時にやったこと - Project Unknown
http://www.project-unknown.jp/entry/cocoapods-high-sierra

記事末尾にある通り、インストール先を指定する方の

$ sudo gem update --system -n /usr/local/bin
$ sudo gem install -n /usr/local/bin cocoapods

をやったら、成功した。続けて、

$ pod setup

を実行。

2 回目の flutter doctor

CocoaPods を brew からじゃなくて gem からインストールしちゃったけど、大丈夫かな? と思いつつ、再度 flutter doctor を実行してみたら、新しいメッセージが出ていた。

$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel beta, v0.1.5, on Mac OS X 10.13.3 17D102, locale ja-JP)
[✓] Android toolchain - develop for Android devices (Android SDK 27.0.0)
[!] iOS toolchain - develop for iOS devices
    ✗ 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
[✓] Android Studio (version 3.0)
[✓] Connected devices (1 available)

! Doctor found issues in 1 category.
$

Xcode から呼び出される開発者向けコマンドは、複数の場所にインストールしておいて、xcode-select コマンドで切り替えることができるらしい。例えば Xcode の製品版に付属しているやつと、β版に付属しているやつを、両方インストールしておいて、切り替えたりするのに使える。
もうひとつ、Xcode 本体を省いて開発者向けコマンドだけを収めたサブセットの "Command Line Tools" というパッケージがあって、これをインストールした時にも、xcode-select で場所を指定しておくらしい。Xcode を入れていないのに xcode-select という名前のコマンドを使うのがちょっと変な感じだけど。Linux で /etc/alternatives とか使って環境を切り替えるのと似たような仕組みなんだと思う。

で、今思い返すと……さっき Homebrew をインストールし直したときに、こんなメッセージが出て、確かに return キー押したわ!

==> The Xcode Command Line Tools will be installed.

Press RETURN to continue or any other key to abort

さらにその後に、xcode-select を呼んでいるところも見つかった。

==> /usr/bin/sudo /usr/bin/xcode-select --switch /Library/Developer/CommandLineTools

つまり、

  1. この Mac にはもともと Xcode がインストールされていたのに、
  2. Homebrew のインストーラが気を利かせて「Xcode 入ってないかもしれないから、Command Line Tools 入れてあげるね^^ ついでに xcode-select も実行しておいてあげるね^^」という動きをした結果、
  3. flutter doctor から見ると「あれ? さっきは Xcode 入ってなかったっけ? なんかサブセットみたいのに切り替わってる? Xcode 本体を用意してほしいんだけど(・_・」

となって、flutter doctor に

[!] iOS toolchain - develop for iOS devices
    ✗ 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

が出るに至った、ということか。

であれば、Xcode 自体が壊れたとかではないのだから、上記メッセージの最後にあるように xcode-select し直せばよさそうだ。

$ sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
$

やったか……!?

$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel beta, v0.1.5, on Mac OS X 10.13.3 17D102, locale ja-JP)
[✓] Android toolchain - develop for Android devices (Android SDK 27.0.0)
[✓] iOS toolchain - develop for iOS devices (Xcode 9.2)
[✓] Android Studio (version 3.0)
[✓] Connected devices (1 available)

• No issues found!
$ 

やったー!!