flutterプロジェクトをasdfでどこまでいけるか見てみた

asdfはバージョン管理をまとめてやってくれるツールです。
プロジェクトで扱う 複数ライブラリ/言語 のバージョン管理が面倒になったのでasdfでまとめたく、やってみました。

筆者環境は以下です。
M3 MacBook
mac OS:15.2
Xcode:16.0

事前準備

# インストール
brew install asdf
# パスを通す
echo -e "\n. $(brew --prefix asdf)/libexec/asdf.sh" >> ${ZDOTDIR:-~}/.zshrc
source ~/.zshrc

各プラグインを追加

asdf plugin-add ruby
asdf plugin-add bundler
asdf plugin-add cocoapods
asdf plugin add flutter
asdf plugin add nodejs

プラグインを削除する場合は、 asdf plugin remove <name> コマンドを実行。

以下で利用可能なバージョンを確認できます。

asdf list-all flutter

欲しいバージョンをインストール

asdf install ruby 2.7.8
asdf install bundler 2.4.22
asdf install cocoapods 1.15.2
asdf install flutter 3.13.2-stable
asdf install nodejs 20.17.0

コマンドが利用できるようにとりあえずglobalで設定。

asdf global ruby 2.7.8
asdf global bundler 2.4.22
asdf global cocoapods 1.15.2
asdf global flutter 3.13.2-stable
asdf global nodejs 20.17.0

asdfで設定されているバージョンを確認するには以下。

asdf list
# 特定のバージョンを見たい時は以下のようにする
asdf list flutter

動作可能か確認

% flutter doctor
[!] Xcode - develop for iOS and macOS (Xcode 16.2)
    ✗ CocoaPods installed but not working.
        You appear to have CocoaPods installed but it is not working.
        This can happen if the version of Ruby that CocoaPods was installed with is different from the one being used to invoke it.
        This can usually be fixed by re-installing CocoaPods.
      To re-install see https://guides.cocoapods.org/using/getting-started.html#installation for instructions.

わけあって当初は Xcode:16.2、cocoapods:1.11.3 のバージョンとして進めていてその際にはflutter doctorで引っかかりました。そのあとは以下で解消。

cd ~/.asdf/installs/cocoapods/1.11.3
bundle update ruby-prof
% flutter doctor
[✓] Xcode - develop for iOS and macOS (Xcode 16.2)

プロジェクト単位でバージョンを指定

asdf local ruby 2.7.8
asdf local bundler 2.4.22
asdf local cocoapods 1.15.2
asdf local flutter 3.13.2-stable
asdf local nodejs 20.17.0

.tool-versionsが生成されて、PJ単位でバージョンが管理できるようになります。

VSCodeの設定

settings.jsonを編集します。
dart.flutterSdkPathに"/Users/<yourname>/.asdf/installs/flutter/3.13.2-stable"のように設定したいところですが、これだと個人の名前が入りコード管理できません。

これに関しては、こちらのページを参考にさせてもらいました。

対応としてはシンボリックリンクをプロジェクト直下に作成します。

ln -s $HOME/.asdf/installs/flutter/3.13.2-stable flutter_sdk

settings.jsonを以下のように設定します。ついでにVSCodeの検索に引っかかるのが鬱陶しいので、検索対象除外設定も入れます。

	"dart.flutterSdkPath": "flutter_sdk",
    "search.exclude": {
        "**/flutter_sdk": true
    }

.gitignoreで作成したリンクを管理対象外にします。

flutter_sdk

最後に静的解析を使っている場合にはこちらも対象外にしておきます。

analyzer:
  exclude:
    - "flutter_sdk/**"

まとめ

asdfで一式インストールしてflutter runを実行することができました。
今後はasdfで一括でバージョン管理できるようになるでしょう。

導入したばっかなので、今後つまずきポイントがあれば追記します。

いいなと思ったら応援しよう!