転職したらスマレジだった件

スマレジのエンジニアやまてのテックブログです。マジレス大歓迎です。

Flutter の環境構築 ② Flutter の実行に必要なもろもろのインストール

スマレジの テックファーム(SES 部門)のWebエンジニア やまて(@r_yamate) と申します。

実務では SES の派遣先で、テーブルオーダーシステムの機能改修業務の設計などを担当しています。(2022 年 3 月〜)

はじめに

Flutter 開発環境の構築について、遭遇したエラーなどのつまずいた点、分からなくて調べた点などを含めて書き出します。

環境

前回

ryamate.hatenablog.com

目次

Flutter の実行に必要なものの再確認

Flutter の実行に必要なものを flutter doctor コマンドで確認します。

$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.3.10, on macOS 12.6.1 21G217 darwin-x64, locale ja-JP)
[!] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
    ! Some Android licenses not accepted.  To resolve this, run: flutter doctor --android-licenses
[✗] 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 (version 2022.1)
    ✗ Unable to find bundled Java version.
[✓] VS Code (version 1.74.3)
[✓] Connected device (2 available)
[✓] HTTP Host Availability

! Doctor found issues in 3 categories.

(すでに[✓]のものもありますが、)記載のある項目を書き出すと、以下の通りです。

チェックマークがついていない項目をインストールします。

以降も、インストールなどが正しくできているかどうかは、flutter doctor(または、flutter doctor -v)を実行して確認します。

1. Android アプリ開発の関連

Android アプリ開発の関連では以下を実行します。

1-1. Android Studio のインストール

Android Studio をインストールします。

Android Studio は、 Android アプリ開発用の公式の統合開発環境IDE)です。

developer.android.com

1-2. Android のライセンス許可

Android Studio をインストールして、起動、初期セットアップを行った後は、ライセンスの許可を行います。

下記のコマンドでライセンスを許可します。

flutter doctor --android-licenses

何度か、同意するかを y/N で聞かれるので、全てに y と入力します。

ライセンスの許可について flutter doctor でエラーが残る場合は、以下を確認します。

Android Studio を起動して、More Actions から SDK Manager を選択します。

SDK Tools」タブを選択して、「Android SDK Command-line Tools (latest)」にチェックが入っているかを確認します。(入っていなければチェックを入れます)

1-3. トラブルシューティング ✗ Unable to find bundled Java version.

私が環境構築していたときは、 flutter doctor を実行した際に、以下の「バンドルされているJavaのバージョンを見つけることができない」というエラーが出ました。

$ flutter doctor

...
[!] Android Studio (version 2022.1)
    ✗ Unable to find bundled Java version.
...

以下の方法で解決しました。

flutterが見に行くPATHをシンボリックリンクで作ってあげる

Githubのflutterのissue( Unable to find bundled Java version #118502 )によると、flutterがAndroid Studio配下のJavaを認識するためのPATHが間違っている?模様。

zenn.dev

記事の通り、以下のコマンドを実行しました。

cd /Applications/Android\ Studio.app/Contents
sudo ln -nfs jbr jre

以下の通り、エラー(✗ Unable to find bundled Java version.)が出なくなりました。

$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.3.10, on macOS 12.6.1 21G217 darwin-x64, locale ja-JP)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
[!] Xcode - develop for iOS and macOS (Xcode 14.2)
    ✗ 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 (version 2022.1)
[✓] VS Code (version 1.74.3)
[✓] Connected device (3 available)
[✓] HTTP Host Availability

! Doctor found issues in 1 category.

2. iOS アプリ開発の関連

iOS アプリ開発の関連では以下を実行します。

  • 2-1. Xcode のインストール
  • 2-2. CocoaPods のインストール

2-1. Xcode のインストール

App StoreXcode をインストールします。(7.8GBとサイズが大きいので時間がかかりました。)

developer.apple.com

2-2. CocoaPods のインストール

CocoaPods をインストールします。CocoaPods は Swift と Objective-C の依存関係管理ツールです。

cocoapods.org

Homebrew を使って、下記のコマンドで CocoaPods をインストールします。

brew install cocoapods
  • 実行時ログ

      $ brew install cocoapods
      Running `brew update --auto-update`...
      ==> Auto-updated Homebrew!
      Updated 1 tap (homebrew/core).
      ==> New Formulae
      ancient                                                                      zsh-autopair
    
      ==> Fetching cocoapods
      ==> Downloading https://ghcr.io/v2/homebrew/core/cocoapods/manifests/1.11.3_1-1
      ######################################################################## 100.0%
      ==> Downloading https://ghcr.io/v2/homebrew/core/cocoapods/blobs/sha256:2a61da56e48bfb8effb0c890fc8e131dfd98c2d14d51bf8fd1ed34ab95cf39e5
      ==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:2a61da56e48bfb8effb0c890fc8e131dfd98c2d14d51bf8fd1ed34ab95cf39e5?se=
      ######################################################################## 100.0%
      ==> Pouring cocoapods--1.11.3_1.monterey.bottle.1.tar.gz
      🍺  /usr/local/Cellar/cocoapods/1.11.3_1: 14,173 files, 30.0MB
      ==> Running `brew cleanup cocoapods`...
      Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
      Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
    

3. VS Code

VS Code での開発の関連では以下を実行します。

3-1. VS Code のインストール

VS Code をインストールします。

code.visualstudio.com

3-2. Flutter プラグインVS Code 拡張機能)のインストール

VS Code拡張機能 Flutter をインストールします。

3-3. Flutter のアプリ作成

Flutter のアプリを作成します。

画面上部のメニューバーから、「表示」→「コマンドパレット」を選択します。

「flutter」と入力すると表示される「Flutter: New Project」を選択します。

どの Flutter テンプレートからプロジェクトを作成するかについて、 Application を選択します。

アプリケーション名を入力して、Enterを押下します。

しばらく待つと、Flutter アプリが作成されました。

おわりに

今回は、Flutter の実行に必要なもろもろのインストールをするところまで書きました。

次回は、Android エミュレーターの実行まで書き出します。

ryamate.hatenablog.com

ありがとうございました。



違和感があって舌で触っちゃう親知らずを抜いて、作業の集中力がアップしそう。