MAC安裝Flutter

系統環境要求

Flutter因爲是新出的框架,所以對系統還是有一定的要求的。

  • MacOS(64-bit)
  • 磁盤空間:大於700M,如果算上Android Studio等編輯工具,儘量大於3G。
  • 命令號工具:bash、mkdir、rm、git、curl、unzip、which、brew 這些命令在都可以使用。

下載Flutter SDK包

官網鏈接:https://flutter.io/setup-macos/

直接在 Get the Flutter SDK 中下載當前最新的SDK

配置環境變量

壓縮包下載好以後,找個位置進行解壓。這個位置很重要,因爲下面配置環境變量的時候要用到。我配置到了當前用戶目錄文件夾。

  1. 打開終端工具,使用vim進行配置環境變量,命令如下:
vim ~/.bash_profile
  1. 在打開的文件中增加一行代碼
export PATH=/Users/用戶名/flutter/bin:$PATH

提示:這行命令你要根據你把壓縮包解壓的位置來進行編寫,寫的是你的路徑,很有可能不跟文章一樣。

  1. 配置文件完成後,使用 source 命令重新加載一下,具體命令如下:
source ~/.bash_profile
  1. 使用命令檢查是否安裝成功,具體命令如下:
flutter -h

出現flutter可用命令提示後,表示安裝成功。

檢查開發環境

我們安裝好了Flutter,但是還不具備開發環境。開發還需要很多軟件和插件的支持,那到底需要哪些插件和軟件那?我們可以使用Flutter爲我們提供的命令來進行檢查:

flutter doctor

顯示結果:

[✓] Flutter (Channel stable, v1.2.1, on Mac OS X 10.13.6 17G4015, locale zh-Hans-CN)
[✗] 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.io/setup/#android-setup for detailed instructions).
      If 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 10.2.1)
    ✗ 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
[!] Android Studio (not installed)
[!] VS Code (version 1.36.1)
    ✗ Flutter extension not installed; install from
      https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter
[!] Connected device
    ! No devices available

注意:帶❌的就必須安裝,帶❗️的就可以暫時忽略。
VS Code是我之前就安裝的,如果沒有安裝是不會出現下面這個帶❌信息的

[!] VS Code (version 1.36.1)
   ✗ Flutter extension not installed; install from
     https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

所以,這個暫時可以先忽略

解決帶❌的問題

1、Android環境

1.1 安裝 Android Studio

打開 Android Studio, 打開 plugins ,安裝 flutter

完成後

[✗] 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.io/setup/#android-setup for detailed instructions).
      If 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.
.
.
.
[!] Android Studio (not installed)

轉變爲

[!] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    ✗ Android licenses not accepted.  To resolve this, run: flutter doctor --android-licenses
.
.
.
[✓] Android Studio (version 3.4)
1.2 執行 flutter doctor --android-licenses同意相關協議

此處 N 多信息,都是相關協議文件,一路 y 即可。

.
.
.此處 N 多信息,都是相關協議文件,一路 y 即可。
.
.
---------------------------------------
Accept? (y/N): y
All SDK package licenses accepted

完成後

[!] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    ✗ Android licenses not accepted.  To resolve this, run: flutter doctor --android-licenses

轉變爲

[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)

2、iOS環境

[!] iOS toolchain - develop for iOS devices (Xcode 10.2.1)
    ✗ 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

在安裝iOS環境時,就碰到一些坑

2.1 坑1:/usr/local is not writable.

看到這個提示,我們第一時間就是想着修改讀寫權限

當你執行sudo chown -R $(whoami) /usr/local進行修改時,系統會有如下提示:

Operation not permitted

現在問題就卡住了,因爲執行brew update之類的命令,需要對/usr/local進行寫入操作。但是操作用戶無法像對普通文件夾操作一樣,通過chown獲得write權限。由於/usr/local是系統文件夾,macOS限制了對其的操作權限。

蘋果從 OS X El Capitan 10.11 系統開始使用了 Rootless 機制,可以將該機制理解爲一個更高等級的系統的內核保護措施,系統默認將會鎖定 /system/sbin/usr 這三個目錄。

在終端輸入

csrutil status

收到系統提示

System Integrity Protection status:enabled

說明rootless默認打開,此時無法通過sudo命令,對/system/sbin/usr 這三個目錄進行修改。

打開、關閉Rootless機制
  • 重啓Mac
  • 開機時後按下 Command+R,進入恢復模式。
  • 在上面的菜單實用工具中找到並打開 Terminal
  • 輸入如下命令:
csrutil disable

此時rootless已經關閉,退出恢復模式,正常進入系統。在終端輸入

csrutil status

系統提示

System Integrity Protection status:disabled

rootless已關閉
可通過 sudo chown -R $(whoami) /usr/local 進行權限修改
開啓rootless
在恢復模式的Terminal輸入如下命令:

csrutil enable

建議修改完成之後,爲了系統安全,將rootless重新開啓。

2.2 坑2:缺少 autoconfautomakelibtool

本以爲修改文件權限後就會一帆風順,可惜天不遂人願。看大牛的播客都是一次過,到自己這裏就問題比較多。😔

發現問題,解決問題。
一頓度娘,找到的都是使用url去下載一個.tar.gz的包,然後解壓,安裝。
然並卵啊,還是一頓報錯,頭大

隨後,還是使用brew進行的安裝,一次搞定

brew install autoconf
brew install automake
brew install libtool

最後,我終於成功了!美滋滋😄

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.7.8+hotfix.3, on Mac OS X 10.14.5 18F132, locale
    zh-Hans-CN)
 
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.1)
[✓] Xcode - develop for iOS and macOS (Xcode 10.2.1)
[✓] iOS tools - develop for iOS devices
[✓] Android Studio (version 3.4)
[!] VS Code (version 1.36.1)
    ✗ Flutter extension not installed; install from
      https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter
[✓] Connected device (1 available)

! Doctor found issues in 1 category.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章