上次配置過Mac到flutter環(huán)境,但是由于最近系統(tǒng)更新了,什么都沒了又得從新配置,發(fā)現(xiàn)自己竟然好多都忘記了,看來還是得把它記下來才行
在Mac上安裝并運(yùn)行Flutter 最低要求:
- 操作系統(tǒng):macOS(64-bit)
- 磁盤空間:700MB(不包括Xcode或者Android Studio的磁盤空間)
- 工具:Flutter 依賴 這些命令行工具
bash curl git 2.x mkdir rm unzip which
(可以使用檢測一下沒有都安裝上免得后期出問題)
mac安裝git可以參考http://www.lxweimin.com/p/7edb6b838a2e
一、設(shè)置 Flutter 鏡像
配置flutter國內(nèi)鏡像
// 直接到用戶目錄 下 運(yùn)行 open -e .bash_profile 在文件中增加如下
export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
注意這個(gè)鏡像地址不能保證永遠(yuǎn)可以用,得隨時(shí)關(guān)注Useing Flutter in China這個(gè)官網(wǎng)
二、獲取Flutter SDK
2.1 下載flutter sdk
推薦最好下載 stable channel這個(gè)穩(wěn)定版本
2.2 解壓
## development 這個(gè)根據(jù)自己實(shí)際得目錄更改
$ cd ~/development
## 下面~/ 后得目錄和下載得flutter版本根據(jù)自己實(shí)際名稱更改
$ unzip ~/Downloads/flutter_macos_v1.2.1-stable.zip
具體操作:
taodeMacBook-Pro:~ tao$ cd ~/dev-flutter/
taodeMacBook-Pro:dev-flutter tao$ ls
android-sdk
taodeMacBook-Pro:dev-flutter tao$ unzip ~/Downloads/flutter_macos_v1.7.8+hotfix.4-stable.zip
taodeMacBook-Pro:dev-flutter tao$ ls
android-sdk flutter
taodeMacBook-Pro:dev-flutter tao$
taodeMacBook-Pro:dev-flutter tao$ ls flutter/
AUTHORS README.md examples
CODE_OF_CONDUCT.md analysis_options.yaml flutter_console.bat
CONTRIBUTING.md bin flutter_root.iml
LICENSE dartdoc_options.yaml packages
PATENTS dev version
taodeMacBook-Pro:dev-flutter tao$
2.3 添加flutter相關(guān)工具到path中
export PATH="$PATH:`pwd`/flutter/bin"
該命令只是一次性的,對當(dāng)前對terminal 設(shè)置里一個(gè)flutter的環(huán)境變量,等關(guān)閉里電腦下次又得重新設(shè)置,想要設(shè)置個(gè)永久性的還是要
將解壓得路徑配置到.bash_profile
中去
輸入命令打開.bash_profile
taodeMacBook-Pro:dev-flutter tao$ open ~/.bash_profile
taodeMacBook-Pro:dev-flutter tao$
在最后增加如下內(nèi)容:
export PATH=/Users/tao/dev-flutter/flutter/bin:$PATH
export ANDROID_HOME="/Users/tao/Library/Android/sdk"
export PATH=${PATH}:${ANDROID_HOME}/tools
export PATH=${PATH}:${ANDROID_HOME}/platform-tools
export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cnexport PATH="/usr/local/opt/gettext/bin:$PATH"
其中export PATH=/Users/tao/dev-flutter/flutter/bin:$PATH
這個(gè)是配置的flutter
解壓的目錄(需要指定到flutter下面到bin目錄下)
其中export ANDROID_HOME="/Users/tao/Library/Android/sdk"
配置里Android sdk 路徑
taodeMacBook-Pro:dev-flutter tao$ flutter doctor
╔════════════════════════════════════════════════════════════════════════════╗
║ A new version of Flutter is available! ║
║ ║
║ To update to the latest version, run "flutter upgrade". ║
╚════════════════════════════════════════════════════════════════════════════╝
Doctor summary (to see all details, run flutter doctor -v):
[!] Flutter (Channel stable, v1.7.8+hotfix.4, on Mac OS X 10.15 19A602, locale
zh-Hans-CN)
? Downloaded executables cannot execute on host.
See https://github.com/flutter/flutter/issues/6207 for more information
[?] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[!] Xcode - develop for iOS and macOS (Xcode 11.1)
? 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:
brew install cocoapods
pod setup
[?] iOS tools - develop for iOS devices
[?] Android Studio (version 3.5)
[?] IntelliJ IDEA Ultimate Edition (version 2018.3.4)
[!] Connected device
! No devices available
! Doctor found issues in 3 categories.
taodeMacBook-Pro:dev-flutter tao$
運(yùn)行flutter doctor
進(jìn)行環(huán)境檢測,會發(fā)現(xiàn)用很多依賴缺失,根據(jù)提示一一安裝
但是還有一個(gè)提示我的mac和我下載的flutter好像不兼容
Doctor summary (to see all details, run flutter doctor -v):
[!] Flutter (Channel stable, v1.7.8+hotfix.4, on Mac OS X 10.15 19A602, locale
zh-Hans-CN)
? Downloaded executables cannot execute on host.
See https://github.com/flutter/flutter/issues/6207 for more information
直接運(yùn)行flutter upgrade
安裝個(gè)最新版本的就好了
taodeMacBook-Pro:dev-flutter tao$ flutter upgrade
Upgrading Flutter from /Users/tao/dev-flutter/flutter...
../../third_party/dart/runtime/bin/snapshot_utils.cc: 149: error: Failed to memory map snapshot: /Users/tao/dev-flutter/flutter/bin/cache/dart-sdk/bin/snapshots/kernel-service.dart.snapshot
version=2.4.0 (Wed Jun 19 11:53:45 2019 +0200) on "macos_x64"
thread=5891, isolate=(null)(0x0)
【注意??命令中請使用 sudo 否則有些會提示錯(cuò)誤】
像這個(gè) 執(zhí)行個(gè)flutter doctor
都報(bào)錯(cuò)
taodeMacBook-Pro:repos tao$ flutter doctor
Failed to write the version file to the artifact cache: "FileSystemException:
Cannot open file, path = '/Users/tao/dev-flutter/flutter/version' (OS Error:
Permission denied, errno = 13)".
Please ensure you have permissions in the artifact cache directory.
Failed to write the version file
廢話少說繼續(xù)吧...
## 在執(zhí)行一次
taodeMacBook-Pro:dev-flutter tao$ sudo flutter doctor -v
Woah! You appear to be trying to run flutter as root.
We strongly recommend running the flutter tool without superuser privileges.
/
??
Downloading Dart SDK from Flutter engine d004bcd4d619fc3574761d63d7cf7b7291332c79...
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 121M 100 121M 0 0 4763k 0 0:00:26 0:00:26 --:--:-- 3986k
## 這里還會有很多下載,就不貼了
## 最后會顯示flutter及dart 版本信息和缺失的依賴
taodeMacBook-Pro:dev-flutter tao$ sudo flutter doctor
Password:
Woah! You appear to be trying to run flutter as root.
We strongly recommend running the flutter tool without superuser privileges.
/
??
Downloading Dart SDK from Flutter engine b863200c37df4ed378042de11c4e9ff34e4e58c9...
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 263M 100 263M 0 0 5236k 0 0:00:51 0:00:51 --:--:-- 5379k
Building flutter tool...
Doctor summary (to see all details, run flutter doctor -v):
[?] Flutter (Channel stable, v1.9.1+hotfix.6, on Mac OS X 10.15 19A602, locale
zh-Hans-CN)
[?] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[!] Xcode - develop for iOS and macOS (Xcode 11.1)
? 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. For more info, see
https://github.com/flutter/flutter/issues/14293.
To re-install CocoaPods, run:
sudo gem install cocoapods
[?] Android Studio (version 3.5)
[?] IntelliJ IDEA Ultimate Edition (version 2018.3.4)
[!] Connected device
! No devices available
! Doctor found issues in 2 categories.
看提示好像還差一個(gè)cocoapods
再手工裝一個(gè)
注意??Mac 中文件查找使用 which directoryName
而不建議使用 find ~/ -name 'directoryName'
sudo gem install cocoapods
但是會發(fā)現(xiàn)執(zhí)行了如上命令幾次依然提示通用錯(cuò)誤,解決方法是
taodeMacBook-Pro:dev-flutter tao$ cd ~/.cocoapods/repos
taodeMacBook-Pro:repos tao$ git clone https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git master
fatal: 目標(biāo)路徑 'master' 已經(jīng)存在,并且不是一個(gè)空目錄。
taodeMacBook-Pro:repos tao$ ls
master
taodeMacBook-Pro:repos tao$ rm -rf master/
taodeMacBook-Pro:repos tao$
taodeMacBook-Pro:repos tao$
taodeMacBook-Pro:repos tao$ git clone https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git master
正克隆到 'master'...
手動(dòng)刪除老版本,克隆新版本
這個(gè)方法最后證實(shí)是失敗的....
brew reinstall libimobiledevice
brew install --HEAD libimobiledevice
brew upgrade cocoapods
全部都又執(zhí)行了一次,最后依然報(bào)錯(cuò)麻麻但,不曉得咋搞,ruby我東重新裝過了
【最后我去官網(wǎng)從新下載了個(gè)flutter_macos_v1.9.1+hotfix.5-stable.zip
】
然后解壓測試??了媽蛋垃圾問題(推測是Mac版本兼容問題,出錯(cuò)都版本是10.14.1 而目前最新都版本為10.15.1)
taodeMacBook-Pro:dev-flutter tao$ unzip ~/Downloads/flutter_macos_v1.9.1+hotfix.5-stable.zip
taodeMacBook-Pro:dev-flutter tao$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[?] Flutter (Channel stable, v1.9.1+hotfix.5, on Mac OS X 10.15.1 19B88, locale zh-Hans-CN)
[?] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[?] Xcode - develop for iOS and macOS (Xcode 11.1)
[?] Android Studio (version 3.5)
[?] IntelliJ IDEA Ultimate Edition (version 2018.3.4)
[!] Connected device
! No devices available
! Doctor found issues in 1 category.
還有輸入flutter doctor
會提示
這個(gè)千萬別點(diǎn)擊 移到廢紙簍;那樣flutter運(yùn)行就會出問題了
三、IOS開發(fā)環(huán)境設(shè)置
3.1 Xcode 安裝
1、直接去Apple store 下載 對應(yīng)得Xcode 然后安裝這個(gè)沒有什么說的
點(diǎn)擊下載得圖標(biāo)
在啟動(dòng)臺里得右下角你會看見一個(gè)正在下載得xcode
安裝完成再安裝些必須的組件
2、配置Xcode 命令行工具以使用新安裝得Xcode版本
$ sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
注意/Applications/Xcode.app/Contents/Developer
對應(yīng)得是自己得Xcode 得安裝路徑
3、 打開Xcode 許可協(xié)議,執(zhí)行如下命令
$ sudo xcodebuild -license
3.2 設(shè)置iOS 模擬器
使用命令開啟iOS模擬器
$ sudo open -a Simulator
四、Android開發(fā)環(huán)境設(shè)置
官網(wǎng)下載 android studio :https://developer.android.google.cn/studio)
下載完成直接點(diǎn)著左邊得Android Studio 拖到右邊得Applications文件里去,他就會提示你安裝了
接下來就是下一步下一步直到安裝結(jié)束
安裝完成后記得配置安裝dart和flutter 插件
五、新建項(xiàng)目
控制臺執(zhí)行 flutter create proj_name
taodeMacBook-Pro:flutter-project tao$ flutter create my_first_app_test
my_first_app_test/.idea/workspace.xml (created)
Running "flutter pub get" in my_first_app_test... 3.1s
Wrote 65 files.
All done!
[?] Flutter is fully installed. (Channel stable, v1.9.1+hotfix.5, on Mac OS X
10.15.1 19B88, locale zh-Hans-CN)
[?] Android toolchain - develop for Android devices is fully installed. (Android
SDK version 29.0.2)
[?] Xcode - develop for iOS and macOS is fully installed. (Xcode 11.1)
[?] Android Studio is fully installed. (version 3.5)
[?] IntelliJ IDEA Ultimate Edition is fully installed. (version 2018.3.4)
[!] Connected device is not available.
Run "flutter doctor" for information about installing additional components.
In order to run your application, type:
$ cd my_first_app_test
$ flutter run
Your application code is in my_first_app_test/lib/main.dart.
然后flutter run
直接來跑一哈
## 啟動(dòng)iOS模擬器
taodeMacBook-Pro:flutter-project tao$ open -a Simulator
taodeMacBook-Pro:flutter-project tao$ ls
my_app my_first_app my_first_app_test
taodeMacBook-Pro:flutter-project tao$ cd my_first_app_test/
taodeMacBook-Pro:my_first_app_test tao$ flutter run