FlutterコマンドでiOS Simulatorが認識できない時の対処

2022.09.29

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

こんにちは、CX事業本部 IoT事業部の若槻です。

Flutterの実装で数ヶ月ぶりにiOS Simulatorを使おうとしたところ、flutter runコマンドでiOS Simulatorが認識できていませんでした。

$ flutter run
Multiple devices found:
macOS (desktop) • macos  • darwin-arm64   • macOS 12.6 21G115 darwin-arm
Chrome (web)    • chrome • web-javascript • Google Chrome 105.0.5195.125
[1]: macOS (macos)
[2]: Chrome (chrome)
Please choose one (To quit, press "q/Q"):

flutter emulatorsコマンドを実行すると次のようなエラーが出るようになっていました。やはりSimulatorが認識できていないようです。

$ flutter emulators
Unable to find any emulator sources. Please ensure you have some
Android AVD images or an iOS Simulator available.

flutter devicesを実行するとこちらのデバイス一覧にもSimulatorが出てきません。

$ flutter devices
2 connected devices:

macOS (desktop) • macos  • darwin-arm64   • macOS 12.6 21G115 darwin-arm
Chrome (web)    • chrome • web-javascript • Google Chrome 105.0.5195.125

Flutterのバージョンを確認すると古かったようなのでアップグレードします。が、アップグレード後も事象は解決しませんでした。

$ flutter upgrade
Flutter is already up to date on channel stable
Flutter 3.3.3 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 18a827f393 (15 hours ago) • 2022-09-28 10:03:14 -0700
Engine • revision 5c984c26eb
Tools • Dart 2.18.2 • DevTools 2.15.0

また念の為iOS Simulatorを起動した状態で同コマンドを実行してみましたが解消はしませんでした。

対処、解決

flutter doctorコマンドによりFlutterの依存関係のセットアップ状況を確認してみると、Xcodeでイシューが2つ出ています。(Android周りはここでは使わないので一旦無視)

$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.3.3, on macOS 12.6 21G115 darwin-arm, locale en-JP)
[✗] Android toolchain - develop for Android devices
    ✗ Unable to locate Android SDK.
      Install Android Studio from: https://developer.android.com/studio/index.html
      On first launch it will assist you in installing the Android SDK components.
      (or visit https://flutter.dev/docs/get-started/install/macos#android-setup for detailed instructions).
      If the Android SDK has been installed to a custom location, please use
      `flutter config --android-sdk` to update to that location.

[✗] 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 see https://guides.cocoapods.org/using/getting-started.html#installation for instructions.
[✓] Chrome - develop for the web
[!] Android Studio (not installed)
[✓] VS Code (version 1.71.2)
[✓] Connected device (2 available)
[✓] HTTP Host Availability

まず、Xcode installation is incompleteとあり、Xcodeのインストールが完了していないとのことなので、対応します。

App StoreでXcodeをアップデートします。

flutter doctorを実行すると、Xcode installation is incompleteのIssueは消えました。

$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.3.3, on macOS 12.6 21G115 darwin-arm, locale en-JP)
[✗] Android toolchain - develop for Android devices
    ✗ Unable to locate Android SDK.
      Install Android Studio from: https://developer.android.com/studio/index.html
      On first launch it will assist you in installing the Android SDK components.
      (or visit https://flutter.dev/docs/get-started/install/macos#android-setup for detailed instructions).
      If the Android SDK has been installed to a custom location, please use
      `flutter config --android-sdk` to update to that location.

[!] Xcode - develop for iOS and macOS (Xcode 14.0.1)
    ✗ 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.
[✓] Chrome - develop for the web
[!] Android Studio (not installed)
[✓] VS Code (version 1.71.2)
[✓] Connected device (3 available)
[✓] HTTP Host Availability

! Doctor found issues in 3 categories.

次にCocoaPods not installed.とあるのでCocoaPodsをインストールします。CocoaPodsはSwiftやObjective-Cの依存関係を管理するライブラリです。

$ sudo gem install cocoapods

XCodeに関するイシューがすべて消えました。

$ flutter doctor            
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.3.3, on macOS 12.6 21G115 darwin-arm, locale en-JP)
[✗] Android toolchain - develop for Android devices
    ✗ Unable to locate Android SDK.
      Install Android Studio from: https://developer.android.com/studio/index.html
      On first launch it will assist you in installing the Android SDK components.
      (or visit https://flutter.dev/docs/get-started/install/macos#android-setup for detailed instructions).
      If the Android SDK has been installed to a custom location, please use
      `flutter config --android-sdk` to update to that location.

[✓] Xcode - develop for iOS and macOS (Xcode 14.0.1)
[✓] Chrome - develop for the web
[!] Android Studio (not installed)
[✓] VS Code (version 1.71.2)
[✓] Connected device (3 available)
[✓] HTTP Host Availability

! Doctor found issues in 2 categories.

するとデバイス一覧にちゃんとiOS Simulatorが表示されるようになりました。

$ flutter devices
3 connected devices:

iPhone 14 Pro Max (mobile) • B2CE44F2-EA3C-4050-85A4-2F188B917247 • ios            • com.apple.CoreSimulator.SimRuntime.iOS-16-0 (simulator)
macOS (desktop)            • macos                                • darwin-arm64   • macOS 12.6 21G115 darwin-arm
Chrome (web)               • chrome                               • web-javascript • Google Chrome 105.0.5195.125

しかしflutter emulatorsコマンドの実行は引き続きエラーとなります。

$ flutter emulators
Unable to find any emulator sources. Please ensure you have some
Android AVD images or an iOS Simulator available.

ここでflutter runを実行するとCocoaPodsがクラッシュしたというエラーとなりました。

$ flutter run
Downloading darwin-x64/FlutterMacOS.framework tools...           2,379ms
Downloading darwin-x64/gen_snapshot tools...                       663ms
Downloading darwin-x64-profile/FlutterMacOS.framework tools...      1,210ms
Downloading darwin-x64-profile tools...                            313ms
Downloading darwin-x64-profile/gen_snapshot tools...               494ms
Downloading darwin-x64-release/FlutterMacOS.framework tools...        991ms
Downloading darwin-x64-release tools...                            283ms
Downloading darwin-x64-release/gen_snapshot tools...               504ms
Running "flutter pub get" in flutter_sample_app...               1,285ms
Launching lib/main.dart on iPhone 8 in debug mode...
Running pod install...                                           1,074ms
CocoaPods' output:
↳
      Preparing

    Analyzing dependencies

    Inspecting targets to integrate
      Using `ARCHS` setting to build architectures of target `Pods-Runner`: (``)

    Fetching external sources
    -> Fetching podspec for `Flutter` from `Flutter`
    -> Fetching podspec for `firebase_analytics` from `.symlinks/plugins/firebase_analytics/ios`
    firebase_analytics: Using Firebase SDK version '9.5.0' defined in 'firebase_core'
    -> Fetching podspec for `firebase_core` from `.symlinks/plugins/firebase_core/ios`
    firebase_core: Using Firebase SDK version '9.5.0' defined in 'firebase_core'

Error output from CocoaPods:
↳
    /Library/Ruby/Gems/2.6.0/gems/ffi-1.15.5/lib/ffi/library.rb:275: [BUG] Bus Error at 0x0000000102b64000
    ruby 2.6.8p205 (2021-07-07 revision 67951) [universal.arm64e-darwin21]

    -- Crash Report log information --------------------------------------------
       See Crash Report log file under the one of following:
         * ~/Library/Logs/DiagnosticReports
         * /Library/Logs/DiagnosticReports
       for more details.
    Don't forget to include the above Crash Report log file in bug reports.

    -- Control frame information -----------------------------------------------
    [NOTE]
    You may have encountered a bug in the Ruby interpreter or extension libraries.
    Bug reports are welcome.
    For details: https://www.ruby-lang.org/bugreport.html

    [IMPORTANT]
    Don't forget to include the Crash Report log file under
    DiagnosticReports directory in bug reports.

(中略)

Error: To set up CocoaPods for ARM macOS, run:
  sudo gem uninstall ffi && sudo gem install ffi -- --enable-libffi-alloc



Error running pod install
Error launching application on iPhone 8.

gemでインストールされたライブラリがARMのMac OSに対応していなかったようです。エラーメッセージに従い、libffiを有効にしてffiをインストールし直します。

$ sudo gem uninstall ffi && sudo gem install ffi -- --enable-libffi-alloc

するとflutter devicesコマンドで一覧にiOS Simulatorが表示されるようになりました。

$ flutter devices
3 connected devices:

iPhone 14 (mobile) • 3CCD8BF0-A046-40CF-98BD-6A2B523EA0EB • ios            • com.apple.CoreSimulator.SimRuntime.iOS-16-0 (simulator)
macOS (desktop)    • macos                                • darwin-arm64   • macOS 12.6 21G115 darwin-arm
Chrome (web)       • chrome                               • web-javascript • Google Chrome 105.0.5195.125

またflutter runを実行すると、iOS SimulatorでFlutter アプリが無事起動できるようになりました。

$ flutter run
Launching lib/main.dart on iPhone 14 in debug mode...
Running Xcode build...                                                  
 └─Compiling, linking and signing...                      2,181ms
Xcode build done.                                            8.6s
Syncing files to device iPhone 14...                                49ms

Flutter run key commands.
r Hot reload. 
R Hot restart.
h List all available interactive commands.
d Detach (terminate "flutter run" but leave application running).
c Clear the screen
q Quit (terminate the application on the device).

 Running with sound null safety 

An Observatory debugger and profiler on iPhone 14 is available at: http://127.0.0.1:49421/SxMbPWgsMyk=/
The Flutter DevTools debugger and profiler on iPhone 14 is available at: http://127.0.0.1:9101?uri=http://127.0.0.1:49421/SxMbPWgsMyk=/

おわりに

M1 MacにしてからiOS Simulatorをまともに使っていなかったため、いくつか対処が必要なようでしたが、エラーメッセージに従って冷静に対応したら、ちゃんと動くようになって良かったです。

参考

以上