LoginSignup
23
7

More than 1 year has passed since last update.

macでflutter開発始めようとしたら思った以上にエラー出てきて時間かかった話。

Last updated at Posted at 2021-08-10

そもそもこの記事は何?

mac環境でflutterの開発を始めようとした際、手順を記した記事に従って実行していったのだが、つまづいた部分が多々あった。
都度調べながら実行するとそれなりに時間がかかってしまったので、解決方法の記事と一緒に、大まかな手順をまとめた。

方針

以下のページの手順に従って進めていく。
https://qiita.com/oekazuma/items/92e9bae4268fea107efa
https://zenn.dev/kboy/books/ca6a9c93fd23f3/viewer/5232dc
https://www.flutter-study.dev/getting-started/install
この記事では、具体的な手順や詳細の記述は省く。
詰まった部分とその解決方法を列挙的に記述していく。
##手順

1. まずflutterをインストール。

問題なくインストール完了。

2. flutter doctorを実行し、足りないものを確認。

Running "flutter pub get" in flutter_tools...                       8.9s
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.2.3, on macOS 11.2.3 20D91 darwin-x64, 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/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
[✓] Chrome - develop for the web
[!] Android Studio (not installed)
[✓] VS Code (version 1.58.0)
[✓] Connected device (1 available)
! Doctor found issues in 3 categories.

エラー1: Xcode installation is incomplete

Xcodeをインストールしてるはずなのにないと言われた。

[!] 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

xcode自体はすでにinstallされていてiOS開発もしているので、存在していないということはないはず。
インストールしたら実行しろと言われているコマンドは実行したことがなかったので、それが原因であろうと考え、実際に以下の2つのコマンドを実行。

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

これで再度flutter doctorを実行すると、通った。

[✓] Xcode - develop for iOS and macOS

エラー2: Unable to locate Android SDK. Android Studioがない

[✗] 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.

元の記事の流れに従って、Android Studioをmacにインストールしてみる。
再度flutter doctorを実行すると、以下。

[!] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    ✗ Could not determine java version

エラー3: Could not determine java version javaをインストールしたのにversion確認ができない

本当にないのか確認(Android Studioのインストール時に同時に入る認識だった)。

$ java --version
The operation couldn’t be completed. Unable to locate a Java Runtime.
Please visit http://www.java.com for information on installing Java.
$ /usr/libexec/java_home -V
The operation couldn’t be completed. Unable to locate a Java Runtime.
Please visit http://www.java.com for information on installing Java.

本当にないらしい。
そこで、以下のページを参考にhomebrewでjavaをインストールする。
https://qiita.com/ponsuke0531/items/9be8dee67a91c6c78d4f
上記記事に従ってjavaをインストールしたが、以下の結果に。

$ java -version
The operation couldn’t be completed. Unable to locate a Java Runtime.
Please visit http://www.java.com for information on installing Java.
$ /usr/libexec/java_home -V
The operation couldn’t be completed. Unable to locate a Java Runtime.
Please visit http://www.java.com for information on installing Java.

入れたはずなのに動かない。。
深いしんどみの海の中で探し当てた一筋の光。
https://zenn.dev/satokazur222/articles/66568417b291d8
どうやらシンボリックリンクを貼る必要があるらしかった。
自分の場合はjava11をインストールしたので、以下のコマンドを実行。

sudo ln -sfn $(brew --prefix)/opt/openjdk\@11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk

さて、これで、

$ java -version
openjdk version "11.0.10" 2021-01-19
OpenJDK Runtime Environment (build 11.0.10+9)
OpenJDK 64-Bit Server VM (build 11.0.10+9, mixed mode)
$ /usr/libexec/java_home -V
Matching Java Virtual Machines (1):
    11.0.10 (x86_64) "UNDEFINED" - "OpenJDK 11.0.10" /usr/local/Cellar/openjdk@11/11.0.10/libexec/openjdk.jdk/Contents/Home
/usr/local/Cellar/openjdk@11/11.0.10/libexec/openjdk.jdk/Contents/Home

javaのインストール完了!
再度、flutter doctorの実行。

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.2.3, on macOS 11.2.3 20D91 darwin-x64, locale ja-JP)
[!] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    ✗ Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/macos#android-setup for more details.
[✓] Xcode - develop for iOS and macOS
[✓] Chrome - develop for the web
[!] Android Studio (version 2020.3)
    ✗ Unable to find bundled Java version.
[✓] VS Code (version 1.58.0)
[✓] Connected device (1 available)
! Doctor found issues in 2 categories.

あれ?
Android Studioでjavaのversionがないといっている??
なぜだ?

エラー4: Unable to find bundled Java version. JavaをインストールしたのにAndroid Studioから見つけられない

Unable to find bundled Java version.への対応をしないといけない。
以下の記事を参考に、対応。
https://stackoverflow.com/questions/68569430/flutter-unable-to-find-bundled-java-versionflutter-doctor-after-updated-andro

i. $JAVA_HOMEを設定

https://mkyong.com/java/how-to-set-java_home-environment-variable-on-mac-os-x/#what-is-usrlibexecjava-home
自分はMacOS Big Surを使っているが、bashを使うように変更しているので、.bash_profileに追記。

.bash_profile
## 以下を追記
export JAVA_HOME=$(/usr/libexec/java_home)
$ source ~/.bash_profile
$ echo $JAVA_HOME
/usr/local/Cellar/openjdk@11/11.0.10/libexec/openjdk.jdk/Contents/Home
ii. Android Studioの設定

以下の記事を参考に、コマンドを実行。
https://stackoverflow.com/questions/68569430/flutter-unable-to-find-bundled-java-versionflutter-doctor-after-updated-andro

cd /Applications/Android\ Studio.app/Contents/jre
ln -s ../jre jdk
ln -s "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin" jdk
iii. 確認
$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.2.3, on macOS 11.2.3 20D91 darwin-x64, locale ja-JP)
[!] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    ✗ Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/macos#android-setup for more details.
[✓] Xcode - develop for iOS and macOS
[✓] Chrome - develop for the web
[✓] Android Studio (version 2020.3)
[✓] VS Code (version 1.58.0)
[✓] Connected device (1 available)
! Doctor found issues in 1 category.

はい、想定通りの回答。
あとはライセンスを付与していきましょう。

エラー5: Android license status unknown licenceを付与するコマンドが落ちる

記事の通りに、そして表示の通りに、flutter doctor --android-licensesを実行。

Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema
	at com.android.repository.api.SchemaModule$SchemaModuleVersion.<init>(SchemaModule.java:156)
	at com.android.repository.api.SchemaModule.<init>(SchemaModule.java:75)
	at com.android.sdklib.repository.AndroidSdkHandler.<clinit>(AndroidSdkHandler.java:81)
	at com.android.sdklib.tool.sdkmanager.SdkManagerCli.main(SdkManagerCli.java:73)
	at com.android.sdklib.tool.sdkmanager.SdkManagerCli.main(SdkManagerCli.java:48)
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.annotation.XmlSchema
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
	... 5 more

またなんか落ちた。
以下のページを参考に対応。
https://zenn.dev/captain_blue/articles/flutter-android-licenses-classnotfound
左のナビゲーションバーのprojectから、More actionsをクリックすると、その中にSDK Managerがある。
スクリーンショット 2021-08-10 20.13.13.png
あとは、記事の通りに実行。
すると、flutter doctor --android-licensesが実行できるように!!
色々Acceptするか聞かれるので全部Acceptして、ライセンス付与完了!

$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.2.3, on macOS 11.2.3 20D91 darwin-x64, locale ja-JP)
[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
[✓] Xcode - develop for iOS and macOS
[✓] Chrome - develop for the web
[✓] Android Studio (version 2020.3)
[✓] VS Code (version 1.58.0)
[✓] Connected device (1 available)
• No issues found!

手順完了!

と言うことで、flutterの開発環境が整いました!!
早速開発していきたいと思います!

23
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
23
7