在mac中安装flutter开发环境

1,019 阅读2分钟

本篇文章记录了在开始安装环境前你需要知道的大概思路和作者安装过程的坎坷

一、安装概览

你需要安装以下:

  • brew
  • xcode
  • androidstudio 或 vscode
  • android sdk
  • flutter sdk
  • dart sdk

二、详细过程

1、xcode

xcode是我们开发编译ios需要用到的,在App Store中直接下载安装即可。

2、brew

brew是mac的包管理工具,相当于ubuntu的apt-get 和 centos 的yum一样。mac默认是没有安装上这个工具包的,需要手动安装。

1、需要依赖于Command Line Tools

打开终端,输入命令 xcode-select --install

2、安装brew

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)

如果安装失败,八成是网络原因,重新执行brew安装命令

3、切换brew源

1、替换brew.git

homebrew托管于github,更新homebrew就是从git上拉取最新的版本。 有时候git的速度也很慢,会导致更新受阻,那么就需要给git仓库换一个远程地址。

cd "$(brew --repo)"
git remote set-url origin https://mirrors.aliyun.com/homebrew/brew.git
2、替换homebrew-core.git

替换Homebrew 核心软件仓库的地址。

cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-core.git
3、替换Homebrew Bottles 阿里镜像源

官方预先编译好的软件会被装在一个bottle里直接下载解压到系统里,无需本地编译。 Bottle是放在bintray上面的,在国内依然不快。可以通过换bottle的源地址来加速bottle的下载

对于bash用户:

echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile

对于zsh用户:

echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc

3、Androidstudio 和 vscode 的安装

4、Android SDK配置

下载 Android SDK,然后配置环境变量

export ANDROID_HOME=你的Android SDK安装目录
export PATH=$PATH:$ANDROID_HOME/tools
export PATH=$PATH:$ANDROID_HOME/platform-tools

更新配置的环境变量,命令:source ~/.bash_profile

5、flutter 环境变量配置

先去下载 flutter SDK ,并解压到自己想要放置的目录下,然后配置环境变量

export FLUTTER_ROOT=你的flutter安装目录
export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
export PATH=你的flutter安装目录/bin:$PATH

更新配置的环境变量,命令:source ~/.bash_profile

配置环境变量结束,就可以使用flutter命令了。

6、dart 环境变量配置

下载dart sdk ,然后解压到自定义目录,然后配置环境变量

export DART_HOME=/Users/xxx/sdk/flutter/bin/cache/dart-sdk/bin
export PATH="${DART_HOME}:${PATH}"

运行dart --version返回版本号即成功。

三、可能遇到的问题

安装完成后运行flutter doctor 看看环境是否已经准备齐全

1、Some Android licenses not accepted

按照提示运行flutter doctor --android-licens

2、cocopods 安装问题

CocoaPods是一个用Ruby写的、负责管理iOS项目中第三方开源库的工具,CocoaPods能让我们集中的、统一管理第三方开源库,为我们节省设置和更新第三方开源库的时间,我们需要提前安装好。

按照提示我使用su gem install cocopods 过程报错

ERROR:  Error installing cocoapods:

ERROR: Failed to build gem native extension.

    current directory: /Library/Ruby/Gems/2.3.0/gems/ffi-1.12.2/ext/ffi_c

/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/bin/ruby -I /Library/Ruby/Site/2.3.0 -r ./siteconf20200303-68332-1cf9u3w.rb extconf.rb

mkmf.rb can't find header files for ruby at /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/include/ruby.h

extconf failed, exit code 1

Gem files will remain installed in /Library/Ruby/Gems/2.3.0/gems/ffi-1.12.2 for inspection.

Results logged to /Library/Ruby/Gems/2.3.0/extensions/universal-darwin-18/2.3.0/ffi-1.12.2/gem_make.out

找到解决方法,可供参考。

最后,运行flutter doctor 没有错误项,环境即安装完毕了。