flutter 环境搭建

2021/12/18 flutter

# 安装 Flutter SDK

此页面下载 SDK (opens new window)

然后设置环境变量

# 检查环境

运行

flutter doctor
1

查看输出

PS C:\Users\Yongpeng> flutter doctor
Running "flutter pub get" in flutter_tools...                       3.3s
Doctor summary (to see all details, run flutter doctor -v):
[] Flutter (Channel stable, 2.8.1, on Microsoft Windows [Version 10.0.22000.376], locale zh-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.dev/docs/get-started/install/windows#android-setup for detailed instructions).
      If the Android SDK has been installed to a custom location, please use
      `flutter config --android-sdk` to update to that location.

[] Chrome - develop for the web (Cannot find Chrome executable at .\Google\Chrome\Application\chrome.exe)
    ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.
[!] Android Studio (not installed)
[] VS Code (version 1.63.2)
[] Connected device (1 available)

! Doctor found issues in 3 categories.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

即我们需要安装 Android SDK

# 配置 Android SDK

前往官网(Download Android Studio and SDK tools  |  Android Developers (opens new window))下载 Android Studio 即可

中途遇到了 HAXM 安装问题

Running Intel® HAXM installer
Intel HAXM installation failed!
For more details, please check the installation log: C:\Users\Yongpeng\AppData\Local\Temp\haxm_install-20211218_2105.log
Intel® HAXM installation failed. To install Intel® HAXM follow the instructions found at: https://github.com/intel/haxm/wiki/Installation-Instructions-on-Windows
1
2
3
4

查看wiki (opens new window)发现与 WSL2 冲突

于是关闭 hyper-v 即可 bcdedit /set hypervisorlaunchtype off

然后成功安装 HMAX

运行 Android Studio,并进入 ‘Android Studio Setup Wizard’,安装最新的 Android SDK, Android SDK Platform-Tools 以及 Android SDK Build-Tools。记得补上 Command-Line-Tools

运行 flutter doctor 确保 Flutter 已经定位了 Android Studio 的安装位置。如果未定位到,运行 flutter config --android-studio-dir <directory> 设置 Android Studio 的安装目录。

# 配置 Android 调式设备

在 Android 设备上运行或测试 Flutter 应用之前,需要一个运行 Android 4.1(API 版本 16)或者更高的设备。

  1. 在设备上打开 Developer optionsUSB debugging 选项,可以在 Android documentation (opens new window) 上查看更详细的方法介绍。
  2. 如果在 Windows 平台上使用,需要安装 Google USB Driver (opens new window) 可在 Android Studio 里面安装。
  3. 通过 USB 接口连接手机和电脑,在设备上弹出的授权弹窗,允许授权。
  4. 在命令行中,使用 flutter devices 命令来确认 Flutter 能够识别出所连接的 Android 设备。

# 配置虚拟 Android 调式设备

根据以下步骤来将 Flutter 应用运行或测试于 Android 模拟器上:

  1. 打开 Android Studio,点击 AVD Manager 按钮,选择 Create Virtual Device…
  2. 选择相应的设备并选择 Next 选项。
  3. 选择一个或多个你想要模拟的 Android 版本的系统镜像,然后选择 Next 选项。推荐选择 x86 或者 x86_64 镜像。
  4. 在 Emulated Performance 下选择 Hardware - GLES 2.0 选项来开启 硬件加速 (opens new window)
  5. 确保 AVD 选项配置正确,并选择 Finish 选项。更多详细信息请查看 Managing AVDs (opens new window)
  6. 在 Android Virtual Device Manager 中,点击工具栏中的 Run 选项,模拟器会启动并为你所选择的系统版本和设备显示出相应的界面。

# 显示

flutter doctor --android-licenses
1

一路 y 同意

重新运行 flutter doctor 即可

PS C:\Users\******> flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.8.1, on Microsoft Windows [Version 10.0.22000.376], locale zh-CN)
[✓] Android toolchain - develop for Android devices (Android SDK version 32.0.0)
[✗] Chrome - develop for the web (Cannot find Chrome executable at
    .\Google\Chrome\Application\chrome.exe)
    ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.
[✓] Android Studio (version 2020.3)
[✓] VS Code (version 1.63.2)
[✓] Connected device (2 available)

! Doctor found issues in 1 category.
1
2
3
4
5
6
7
8
9
10
11
12

我用 edge 故没有用 Chrome

为桌面开启支持

flutter config --enable-windows-desktop
flutter config --enable-macos-desktop
flutter config --enable-linux-desktop
1
2
3

可以看到支持的设备,这里我用的 windows,故只能看到 windows

flutter devices
1
2 connected devices:

Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.22000.376]
Edge (web)        • edge    • web-javascript • Microsoft Edge 96.0.1054.57
1
2
3
4
Last Updated: 2023-10-29T08:26:04.000Z