前提:已经正确安装Android Studio和 Xcode
一、获取Flutter SDK
1.下载 :https://flutter.dev/docs/development/tools/sdk/releases?tab=macos#macos
2.解压安装包
二、配置环境
1.打开环境配置文件:
open ~/.bash_profile
若无bash_profile文件,则新建:
touch .bash_profile
2.添加环境配置:
export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
export PATH=SDK解压的路径/flutter/bin:$PATH
例如:export PATH=/Users/xxx/flutter/bin:$PATH
注意:
更新环境变量
您在命令行只能更新当前会话的PATH变量,如Clone Flutter repo所示。 但是,您可能需要的是永久更新此变量,以便您可以运行flutter
命令在任何终端会话中。
对于所有终端会话永久修改此变量的步骤是和特定计算机系统相关的。通常,您会在打开新窗口时将设置环境变量的命令添加到执行的文件中。例如
- 确定您Flutter SDK的目录,您将在步骤3中用到。
- 打开(或创建)
$HOME/.bash_profile
. 文件路径和文件名可能在您的机器上不同. - 添加以下行并更改
[PATH_TO_FLUTTER_GIT_DIRECTORY]
为克隆Flutter的git repo的路径:
export PUB_HOSTED_URL=https://pub.flutter-io.cn //国内用户需要设置
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn //国内用户需要设置
export PATH=PATH_TO_FLUTTER_GIT_DIRECTORY/flutter/bin:$PATH
注意:PATH_TO_FLUTTER_GIT_DIRECTORY
为你flutter的路径,比如“~/document/code”
export PATH=~/document/code/flutter/bin:$PATH
- 运行
source $HOME/.bash_profile
刷新当前终端窗口.
注意: 如果你使用的是zsh,终端启动时 ~/.bash_profile
将不会被加载,解决办法就是修改 ~/.zshrc
,在其中添加:source ~/.bash_profile
5.通过运行flutter/bin
命令验证目录是否在已经在PATH中:
echo $PATH
三、检查环境
1.运行:flutter -h
问题一:运行flutter -h时,报错:
VersionCheckError: Command exited with code 128: git fetch __flutter_version_check__ stable Standard error: fatal: unable to access 'https://github.com/flutter/flutter.git/': LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443
解决方案:
在Android studio中安装flutter即可
再次运行flutter -h
`
2.运行:flutter doctor
问题二:
![image.png](https://upload-images.jianshu.io/upload_images/9149864-e726601df2a7711a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 以上提示Android某些证书在SDK30.0.3的版本中不被允许 解决方案:
运行:flutter doctor --android-licenses
根据提示全部选择y,回车
再次运行:flutter doctor
`
问题三:输入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
解决方案:
1.终端输入:open ~/.bash_profile打开这个问题,没有则 touch ~/.bash_profile创建这个问题
2.在文件输入:export JAVA_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home,即配置你的Java环境变量
3.保存后,在终端输入: source .bash_profile,让环境变量立马生效
4.然后再运行 flutter doctor --android-licenses根据提示一直选y,
5.再运行flutter doctor
问题四:输入flutter doctor异常提示:
cmdline-tools component is missing
Run `path/to/sdkmanager --install "cmdline-tools;latest"`