macOS搭建Flutter开发环境

电脑配置.png

使用镜像

  • 将如下环境变量加入到用户环境变量中
export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn

配置过Java环境变量的朋友应该比较清楚如何配置,这里也简单的贴一下过程:

  • 终端输入如下命令,回车则会打开配置文件
open -e .bash_profile
命令.png
  • 将路径贴到bash_profile文件中


    效果.png
  • 生效

执行如下命令

source .bash_profile
image.png
  • 检查是否生效,使用如下命令,输出了设置的地址,则代表成功
image.png

获取Flutter SDK

我这里是通过github下载的,在终端输入如下命令:

git clone -b stable https://github.com/flutter/flutter.git

下载过程:

promote:~ xxx$ git clone -b stable https://github.com/flutter/flutter.git
Cloning into 'flutter'...
remote: Enumerating objects: 12, done.
remote: Counting objects: 100% (12/12), done.
remote: Compressing objects: 100% (12/12), done.
remote: Total 231804 (delta 0), reused 3 (delta 0), pack-reused 231792
Receiving objects: 100% (231804/231804), 96.54 MiB | 317.00 KiB/s, done.
Resolving deltas: 100% (177395/177395), done.
Checking out files: 100% (4207/4207), done.
  • 更新环境变量

在之前的bash_profile中添加flutter路径
如果原先就存在PATH则用";"作为分隔符拼接到后面
仍要执行source .bash_profile生效(这里的xxx是你电脑用户名)

export PATH=${PATH}:/Users/xxx/flutter/bin:$PATH
  • 运行 flutter doctor命令
    该命令检查你的环境并在命令行窗口中显示报告
promote:~ xxx$ flutter doctor
Downloading Dart SDK from Flutter engine af51afceb8886cc11e25047523c4e0c7e1f5d408...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  200M  100  200M    0     0  4028k      0  0:00:51  0:00:51 --:--:-- 5102k
Building flutter tool...
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure
you trust this source!
Downloading Material fonts...                                       1.2s
Downloading Gradle Wrapper...                                       0.2s
Downloading android-arm-profile/darwin-x64 tools...                 1.4s
Downloading android-arm-release/darwin-x64 tools...                 0.9s
Downloading android-arm64-profile/darwin-x64 tools...               1.1s
Downloading android-arm64-release/darwin-x64 tools...               1.0s
Downloading android-x64-profile/darwin-x64 tools...                 2.1s
Downloading android-x64-release/darwin-x64 tools...                 3.6s
Downloading package sky_engine...                                   1.5s
Downloading common tools...                                         8.7s
Downloading common tools...                                         5.7s
Downloading darwin-x64 tools...                                     7.5s
Downloading libimobiledevice...                                     0.2s
Downloading usbmuxd...                                              0.2s
Downloading libplist...                                             0.2s
Downloading openssl...                                              0.5s
Downloading ideviceinstaller...                                     0.1s
Downloading ios-deploy...                                           0.2s
Downloading libzip...                                               0.2s
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.12.13+hotfix.9, on Mac OS X 10.15.2 19C57,
    locale zh-Hans-CN)
 
[!] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
    ✗ Android license status unknown.
      Try re-installing or updating your Android SDK Manager.
      See https://developer.android.com/studio/#downloads or visit
      https://flutter.dev/setup/#android-setup for detailed instructions.
[✓] Xcode - develop for iOS and macOS (Xcode 11.3)
[!] Android Studio (version 3.5)
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
[!] IntelliJ IDEA Ultimate Edition (version 2018.2)
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
[✓] Connected device (1 available)

! Doctor found issues in 3 categories.
promote:~ xxx$ 

第一次运行flutter doctor命令,它会下载它自己的依赖项并自行编译,缺失的依赖需要安装一下,安装完成后再运行flutter doctor命令来验证是否安装成功

这里flutter doctor命令执行时,最后报! Doctor found issues in 3 categories,共三个问题,在输出日志里可以看到这三个问题的具体描述

这里使用Android Studio,对于IntelliJ IDEA的问题忽略不解决,这样还有两个问题

对于如下问题,是Android Studio缺少插件

✗ Flutter plugin not installed; this adds Flutter specific functionality.
✗ Dart plugin not installed; this adds Dart specific functionality.

在Android Studio的Plugins里搜索然后安装(仅下载Flutter即可,包含Dart)

image.png
  • 对于最后一个问题
✗ Android license status unknown

终端执行:

flutter doctor --android-licenses

报错:

promote:~ xxx$ flutter doctor --android-licenses
Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema
    at com.android.repository.api.SchemaModule$SchemaModuleVersion.(SchemaModule.java:156)
    at com.android.repository.api.SchemaModule.(SchemaModule.java:75)
    at com.android.sdklib.repository.AndroidSdkHandler.(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:583)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    ... 5 more

对于上面的报错,都在讲已安装的java版本过高...实在找不到其他解决办法,本人也只能重新安装java版本为1.8了

  • 删除java
    java当前版本: java -version
promote:~ xxx$ java -version
java version "12.0.2" 2019-07-16
sudo rm -fr /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin
sudo rm -fr /Library/referencesPanes/JavaControlPanel.prefpane
sudo rm -rf /Library/Java/JavaVirtualMachines/jdk-12.0.2.jdk
  • 安装java1.8 官网下载或者清华大学开源镜像站
官网.png
镜像站.png

下载好JavaSDK后双击安装

设置环境变量

终端输入如下命令查找安装路径: 
/usr/libexec/java_home -V
image.png
JAVA_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
PATH=$JAVA_HOME/bin:$PATH:.;export PATH=${PATH}:/Users/xxx/flutter/bin:$PATH
CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:.

export JAVA_HOME
export PATH
export CLASSPATH

export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn

PS: 不要忘记执行 source .bash_profile

  • 再次执行flutter doctor
[!] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
    ! Some Android licenses not accepted.  To resolve this, run: flutter doctor
      --android-licenses

已有提示:

run: flutter doctor --android-licenses

然后在终端里根据提示一路y下去,直到提示:

All SDK package licenses accepted

end...

image.png

从某个地方看到有人讲: 可以通过环境变量将JAVA_HOME的位置指向Android Studio 里自带的Java去,AS自带Java8,通过这种方式就可以解决了,有兴趣的可以试试

参考:
卸载Java
安装Java
搭建Flutter开发环境
安装Flutter踩坑记
最新Mac安装Flutter全过程

你可能感兴趣的:(macOS搭建Flutter开发环境)