Flutterの開発環境をmacに導入
イントロダクション
アプリやwebサービスを作るためには多くの方法があります。なかでも、Flutterは最近非常に盛り上がりを見せています。私自身、モバイルアプリ開発やwebアプリにはほとんど携わったことがないのですが、この機に勉強を兼ねてFlutterで遊んでみました。
目次
なぜFlutter?
Googleが開発しているのでAndroidはもちろん、iOS、webアプリを同時に作ることができます。 そして、マテリアルデザインにも対応しています。 Flutterで使われているDart言語はJavaに近いので、Javaを知っている人には覚えやすいかもしれません。
環境
- PC : macbook Air (2015 early)
- OS : macOS Mojave(10.14.5)
- CPU : 1.6 GHz Intel Core i5
- メモリ : 4GB
公式によると、必要な要件は以下の通りです。
- OS : macOS (64-bit)
- ディスク容量 : 700MB
- ツール(
git
以外はデフォルトで入っているはず) :bash
,curl
,git 2.x
,mkdir
,rm
,unzip
,which
インストール
公式サイトにしたがって、導入していきます。
Flutter SDKのダウンロード
- 公式サイトからzipファイルをダウンロード
- 適当な場所で解凍し、置きたい場所に移動
- PATHを通す(ex.
$ export PATH="$PATH:/Applications/flutter/bin"
) - (オプション)開発バイナリをプレダウンロードしたいときは
$ flutter precashe
$ flutter doctor
でPCの環境をチェック(ログは以下参照)
flutter doctor
で未インストールのツールを表示した結果。
Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, v1.5.4-hotfix.2, on Mac OS X 10.14.5 18F132, locale ja-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/setup/#android-setup for detailed instructions). If the Android SDK has been installed to a custom location, set ANDROID_HOME to that location. You may also want to add it to your PATH environment variable. [✗] 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 ✗ libimobiledevice and ideviceinstaller are not installed. To install with Brew, run: brew update brew install --HEAD usbmuxd brew link usbmuxd 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.dev/platform-plugins To install: brew install cocoapods pod setup [!] Android Studio (not installed) [!] VS Code (version 1.34.0) ✗ Flutter extension not installed; install from https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter [!] Connected device ! No devices available ! Doctor found issues in 5 categories.
セットアップ
flutter doctor
の結果を受けて、必要なツールのインストールをします。
iOSセットアップ
- App StoreからXcodeのインストール
$ sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
をターミナルで実行$ sudo xcodebuild -license
→agree
でライセンス$ open -a Simulator
でシミュレータを起動- Hardware > DeviceでiPhone 5S以上の端末になっているか確認、画面のサイズを変更するときはWindow > Scale
- 初期化されたアプリを生成するため、以下のコマンド6でflutter起動
flutter create my_app cd my_app flutter run
- 準備をするため、以下のコマンドを実行
$ open ios/Runner.xcworkspace
→Runner
タブ →signing
のteam
で自分のApple IDを認証Runner
タブ →Bundle Indentifier
でオリジナルなものに変更(com.[my team name].testApp
等、ここで少し詰まった)- iOSデバイスをmacと接続(初めて接続するときは
信頼
をタップ) 設定
→一般
→デバイス管理
→[自分のアカウント名]
→このアカウトを信頼
flutter run
でiOSデバイス上で起動
1でインストールするツールは以下。
brew update brew install --HEAD usbmuxd brew link usbmuxd brew install --HEAD libimobiledevice brew install ideviceinstaller ios-deploy cocoapods pod setup
3がうまくできていないときは以下のようなエラーメッセージが出ます。Team
の設定を確認し、Bundle Identifier
は適当なものに変えてください。
... Could not build the precompiled application for the device. It appears that your application still contains the default signing identifier. Try replacing 'com.example' with your signing id in Xcode: open ios/Runner.xcworkspace Error launching application on your iPad Pro.
すべてインストールが終了し、もう一度$ flutter doctor
をして、以下の結果が出ればOK。
[✓] iOS toolchain - develop for iOS devices (Xcode 10.2.1)
Androidセットアップ
- Android Studioをインストール
$ flutter doctor --android-licenses
でライセンスを確認- Android Studioを起動して、
configure
→plugin
でflutterと検索、インストール - (実機で動かすとき)AndroidデバイスをUSBでmacと接続して、
設定
→開発者向けオプション
で開発者向けオプション
とUSBデバック
を有効にする - (エミュレータを使うとき)Android Studioを起動し、
configure
→AVD manager
→Create Virtual Device
で画面の指示に従いエミュレータを作成、起動
2でライセンスを確認しないと、$ flutter doctor
で以下のようなエラーが出ます。
[!] Android toolchain - develop for Android devices (Android SDK version 28.0.3) ✗ Android licenses not accepted. To resolve this, run: flutter doctor --android-licenses
3でプラグインを導入しないと、$ flutter doctor
で以下のようなエラーが出ます。
[!] Android Studio (version 3.4) ✗ Flutter plugin not installed; this adds Flutter specific functionality. ✗ Dart plugin not installed; this adds Dart specific functionality.
VScodeプラグイン
$ flutter doctor
を起動すると、以下のようなエラーメッセージが出ます。これはVScodeのプラグインが入っていないためですので、VScodeでflutter
と検索、インストールします。
[!] VS Code (version 1.34.0) ✗ Flutter extension not installed; install from https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter
実行
上記すべて完了したら、$ flutter doctor
で状態を確認します。
Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, v1.5.4-hotfix.2, on Mac OS X 10.14.5 18F132, locale ja-JP) [✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3) [✓] iOS toolchain - develop for iOS devices (Xcode 10.2.1) [✓] Android Studio (version 3.4) [✓] VS Code (version 1.34.0) [!] Connected device ! No devices available
試しにiPadを繋いで$ flutter run
してみたら、問題なくテストアプリ(ボタンを押してカウントするアプリ)を起動できました。
まとめ
macOSにFlutterの開発環境を導入しました。導入は基本的にflutter doctor
の指示に従えば問題なくできるはずです。今後は簡単なアプリを作って練習していきたいと思います。