Start building Flutter Android apps on macOS

Verify system requirements

To install and run Flutter, your macOS environment must meet the following hardware and software requirements.

Hardware requirements

Your macOS Flutter development environment must meet the following minimal hardware requirements.

Requirement Minimum Recommended
CPU Cores 4 8
Memory in GB 8 16
Display resolution in pixels WXGA (1366 x 768) FHD (1920 x 1080)
Free disk space in GB 10.0 18.0

Software requirements

To write and compile Flutter code for Android, install the following packages.

Operating system

Flutter supports macOS 10.15 (Catalina) or later. This guide presumes your Mac runs the zsh as your default shell.

To verify your shell configuration, expand this section

Like most UNIX-like operating system, macOS can support multiple shells, like bash, zsh, and sh. As of the October 2019 release of macOS Catalina (macOS 10.15), Zsh or zsh is the default shell for macOS.

Check and set zsh as default

  1. To verify zsh was set as the default macOS shell, run the Directory Services command line utility.

     $ dscl . -read ~/ UserShell
    

    should print the following as its response.

     UserShell: /bin/zsh
    

    You can skip the remaining steps.

  2. If you need to install zsh, follow the procedure in this Wiki.

  3. If you need to change your default shell to zsh, run the chsh command.

     $ chsh -s `which zsh`
    

To learn more about macOS and zsh, check out Use zsh as the default shell on your Mac in the macOS documentation.

Some Flutter components require the Rosetta 2 translation process on Macs running Apple silicon. To run all Flutter components on Apple silicon, install Rosetta 2.

$ sudo softwareupdate --install-rosetta --agree-to-license

Development tools

Download and install the following packages.

  • Android Studio 2023.1 (Hedgehog) to debug and compile Java or Kotlin code for Android. Flutter requires the full version of Android Studio.
  • Git 2.27 or later to manage source code. To check if you have git installed, type git version in your Terminal. If you need to install git, type brew install git.

The developers of the preceding software provide support for those products. To troubleshoot installation issues, consult that product’s documentation.

当你在当前版本运行 flutter doctor 的时候,它可能会列出这些软件包的其他不同版本。如果出现这种情况,请安装它推荐的版本。

Text editor or integrated development environment

You can build apps with Flutter using any text editor or integrated development environment (IDE) combined with Flutter’s command-line tools.

Using an IDE with a Flutter extension or plugin provides code completion, syntax highlighting, widget editing assists, debugging, and other features.

Popular options include:

安装 Flutter SDK

你可以使用 VS Code Flutter 扩展或自行下载安装 Flutter 压缩包,来安装 Flutter SDK。

使用 VS Code 安装 Flutter

在使用以下流程来安装 Flutter 前,请确保你已经安装 Visual Studio Code 1.77 或更高版本,以及 Flutter extension for VS Code

根据 VS Code 的提示,安装 Flutter

  1. 启动 VS Code。

  2. 打开 命令面板 (Command Palette),按下快捷键 Command + Shift + P

  3. 命令面板 (Command Palette) 中输入 flutter

  4. 选择 Flutter: New Project

  5. VS Code 会提示你在计算机上找到 Flutter SDK。

    1. 如果你已经安装 Flutter SDK,请单击 Locate SDK

    2. 如果你没有安装 Flutter SDK, 请单击 Download SDK

      如果你没有按照 开发工具的必要条件 安装 Git ,单击该按钮后会导航至 Flutter 安装页面。

  6. 当提示 Which Flutter template? 的时候,请忽略它。可以按下 Esc。你可以在检查完开发配置后创建初始测试项目。

下载 Flutter SDK

  1. 当对话框 Select Folder for Flutter SDK 显示时,选择你想要安装 Flutter 的位置。

    VS Code 会打开你默认的位置。你可以选择其他位置。

    请考虑 ~/development/ 这样的位置

  2. 单击 Clone Flutter

    在下载 Flutter 时,VS Code 会弹出该通知:

    Downloading the Flutter SDK. This may take a few minutes.
    

    下载需要一些时间。如果你怀疑下载被挂起,请单击 Cancel 取消下载后,再重新尝试安装。

  3. Flutter 一旦完成下载, 输出 (Output) 面板就会显示。

    Checking Dart SDK version...
    Downloading Dart SDK from the Flutter engine ...
    Expanding downloaded archive...
    

    成功后,VS Code 会弹出该通知:

    Initializing the Flutter SDK. This may take a few minutes.
    

    初始化时,输出 (Output) 面板会显示下面的内容:

    Building flutter tool...
    Running pub upgrade...
    Resolving dependencies...
    Got dependencies.
    Downloading Material fonts...
    Downloading Gradle Wrapper...
    Downloading package sky_engine...
    Downloading flutter_patched_sdk tools...
    Downloading flutter_patched_sdk_product tools...
    Downloading windows-x64 tools...
    Downloading windows-x64/font-subset tools...
    

    该进程还会运行 flutter doctor -v。此时,请忽略该输出。因为 Flutter Doctor 可能会显示与本次快速启动无关的错误。

    Flutter 安装成功后,VS Code 会弹出该通知:

    Do you want to add the Flutter SDK to PATH so it's accessible
    in external terminals?
    
  4. VS Code 可能会显示一则 Google Analytics 的通知。

    如果你同意,请单击 OK

  5. 在所有 Terminal 窗口中启用 flutter

    1. 关闭,然后重新打开所有 Terminal 窗口。

    2. 重新启动 VS Code。

下载并安装 Flutter

从归档列表中下载 Flutter SDK 压缩包,将压缩包移动到你想要的位置,然后解压 SDK,以此来安装 Flutter。

  1. 下载以下 Flutter SDK 最新 stable 版本的压缩包。

    Intel 处理器   Apple Silicon 处理器
    (获取中…)   (获取中…)

    关于其他发布渠道和旧版本,请查阅 Flutter SDK 归档列表

    Flutter SDK 应该会下载至 macOS 默认下载目录: ~/Downloads/

  2. 创建一个文件夹,用于安装 Flutter。

    可以考虑在 ~/development/ 中创建一个目录。

  3. 将 Flutter SDK 压缩文件 (zip) 解压到你想要存储的目录中。可以使用以下指令进行解压。

    $ unzip ~/development/flutter_sdk_v1.0.0.zip -d ~/development/
    

    完成后,Flutter SDK 应该会位于 ~/development/flutter 目录中。

Add Flutter to your PATH

To run Flutter commands in the Terminal, add Flutter to the PATH environment variable. This guide presumes your Mac runs the latest default shell, zsh. Zsh uses the .zshenv file for environment variables.

  1. Launch your preferred text editor.

  2. If it exists, open the Zsh environmental variable file ~/.zshenv in your text editor. If it doesn’t, create ~/.zshenv.

  3. Copy the following line and paste it at the end of your ~/.zshenv file.

    export PATH=$HOME/development/flutter/bin:$PATH
    
  4. Save your ~/.zshenv file.

  5. To apply this change, restart all open terminal sessions.

If you use another shell, check out this tutorial on setting your PATH.

如果你已经安装了所有必要条件和 Flutter SDK,你应该就可以在 macOS 上为 Android 开发 Flutter。

配置 Android 开发

在 Android Studio 中配置 Android toolchain

help 帮助

  1. 启动 Android Studio

    显示 Welcome to Android Studio 的对话框。

  2. 按照 Android Studio Setup Wizard 操作。

  3. 安装以下组件:

    • Android SDK Platform, API 34.0.0
    • Android SDK Command-line Tools
    • Android SDK Build-Tools
    • Android SDK Platform-Tools
    • Android Emulator
  1. 启动 Android Studio

  2. 打开 Settings 对话框,查看 SDK Manager

    1. 如果你已经打开了一个项目,请打开 Tools > SDK Manager

    2. 如果显示 Welcome to Android Studio 的对话框,请单击 Open 按钮后面的 More Options 图标,然后从下拉菜单中单击 SDK Manager

  3. 单击 SDK Platforms

  4. 检查 Android API 34.0.0 是否已经选中。

    如果 Status 栏显示 Update availableNot installed

    1. 选择 Android API 34.0.0

    2. 单击 Apply

    3. 当显示 Confirm Change 对话框时,单击 OK

      显示 SDK Quickfix Installation 完成进度的对话框。

    4. 安装完成后,单击 Finish

      安装最新的 SDK 后, Status 栏可能会显示 Update available。这意味着某些额外的系统镜像可能尚未安装。你可以忽略它然后继续。

  5. 单击 SDK Tools

  6. 检查以下 SDK 工具是否已经选择:

    • Android SDK Command-line Tools
    • Android SDK Build-Tools
    • Android SDK Platform-Tools
    • Android Emulator
  7. 如果上述任何工具的 Status 栏显示 Update availableNot installed

    1. 选择所需的工具。

    2. 单击 Apply

    3. 当显示 Confirm Change 的对话框时,单击 OK

      显示 SDK Quickfix Installation 完成进度的对话框。

    4. 安装完成后,单击 Finish

配置目标 Android 设备

配置安卓模拟器

help 帮助

要配置 Flutter 应用在 Android 模拟器中运行,请按照以下步骤操作:

  1. 在你的开发电脑上启用 VM acceleration

  2. 启动 Android Studio

  3. 打开 Settings 对话框,查看 SDK Manager

    1. 如果你已经打开了一个项目,请打开 Tools > Device Manager

    2. 如果显示 Welcome to Android Studio 的对话框,请单击 Open 按钮后面的 More Options 图标,然后在下拉菜单中单击 Device Manager

  4. 单击 Virtual

  5. 单击 Create Device

    显示 Virtual Device Configuration 的对话框。

  6. Category 下选择 PhoneTablet

  7. 选择设备,你可以浏览或搜索设备。

  8. 单击 Next

  9. Click x86 Images(Intel CPU 的 Mac)或者 ARM Images(Apple CPU 的 Mac).

    单击 x86 Images(Intel CPU 的 Mac)或者 ARM Images(Apple CPU 的 Mac)。

  10. 单击需要模拟的 Android 版本系统镜像。

    1. 如果所需镜像的 Release Name 右侧有一个 Download 图标,请单击该图标。

      显示 SDK Quickfix Installation 完成进度的对话框

    2. 下载完成后,单击 Finish

  11. 单击 Next

    Virtual Device Configuration 会显示它的 Verify Configuation 步骤。

  12. 如果要重命名 Android 虚拟设备 (AVD),请更改 AVD Name 框中的值。

  13. 单击 Show Advanced Settings

  14. 滚动至 Emulated Performance

  15. Graphics 下拉菜单中,选择 Hardware - GLES 2.0

    这样就会开启 硬件加速 (hardware acceleration).

  16. 检查你的 AVD 配置。如果已经完备,请单击 Finish

    想要了解更多有关 AVD 的信息,请查阅 Managing AVDs

  17. Device Manager 对话框中,单击所需 AVD 右侧的 Run 图标。模拟器启动并显示所选操作系统的版本和设备默认的画布。

配置目标 Android 设备

help 帮助

你需要一台运行 Android API level 21 或更高版本的安卓设备,来配置 Flutter 应用在真机 Android 设备上运行。

  1. 按照 Android 文档 中的说明,在设备上启用 开发者选项USB 调试

  2. [可选] 如果要利用无线调试,请按照 Android 文档 中的说明在设备上启用 无线调试

  3. 将设备插入你的 macOS 电脑。如果设备发出提示,请授权电脑访问你的设备。

  4. 检查 Flutter 是否能识别连接的 Android 设备。

    在 Terminal 中运行:

    $ flutter devices
    

    默认情况下,Flutter 使用 adb 工具所在的 Android SDK 版本。如果要在 Flutter 中使用不同的 Android SDK 安装路径,请设置环境变量 ANDROID_SDK_ROOT 为该安装目录。

同意 Android 许可证

help 帮助

在使用 Flutter 之前,按照指南安装所有必要的条件之后,再同意 Android SDK 平台的许可证。

  1. 打开一个高权限(管理员)的控制台窗口。

  2. 运行以下指令启用签名许可证。

    $ flutter doctor --android-licenses
    

    如果你在其他时候已经同意了 Android Studio 许可证,该指令将会返回:

    [========================================] 100% Computing updates...
    All SDK package licenses accepted.
    

    你可以跳过下一个步骤。

  3. 请仔细阅读每项许可条款后,再同意。

许可证问题故障排除

如何解决查找 Java 安装错误的问题

你可以遇到了 Android SDK 定位 Java SDK 的问题。

$ flutter doctor --android-licenses

ERROR: JAVA_HOME is set to an invalid directory: /Applications/Android\ Studio.app/Contents/jre/Contents/Home

Please set the JAVA_HOME variable in your environment to match the
location of your Java installation.

Android sdkmanager tool was found, but failed to run
(/Users/atsansone/Library/Android/sdk/cmdline-tools/latest/bin/sdkmanager): "exited code 1".
Try re-installing or updating your Android SDK,
visit https://flutter.dev/docs/get-started/install/macos#android-setup for detailed instructions.

这是由于环境变量 JAVA_HOME 设置方式导致的, flutter doctor 指令就会返回此错误。当你在向 JAVA_HOME 添加路径时,可以在 AndroidStudio 之间的空格处添加反斜杠,或者用引号将整个路径包含进来。切记,不能 同时 这样做。

在合适的 shell 资源文件中查找 JAVA_HOME 路径。

将下面这样:

export JAVA_HOME="/Applications/Android\ Studio.app/Contents/jre/Contents/Home"

改成:

export JAVA_HOME="/Applications/Android Studio.app/Contents/jre/Contents/Home"

不要在 AndroidStudio 之间加入反斜杠。

要加载当前更新后的环境变量,请重新加载 shell。本例使用 zsh 资源文件。

source ~/.zshrc

检查你的开发配置

help 帮助

运行 Flutter doctor

flutter doctor 指令将检查 macOS 完整的 Flutter 开发环境的所有组件。

  1. Open 你的 Terminal.

    打开你的 Terminal。

  2. 要检查所有组件的安装情况,请运行以下指令。

    $ flutter doctor
    

由于你选择为 Android 进行开发,因此不需要 所有 组件。如果你遵循本指南,指令结果应该类似于:

Running flutter doctor...
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.19.0, on macOS A.B chipset, locale en)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[!] Chrome - develop for the web
[!] Xcode - develop for iOS and macOS (Xcode not installed)
[✓] Android Studio (version 2023.1 (Hedgehog) or later)
[✓] VS Code (version 1.86)
[✓] Connected device (1 available)
[✓] Network resources

! Doctor found issues in 2 categories.

解决 Flutter doctor 的问题

flutter doctor 指令返回错误时,可能是 Flutter、VS Code、Android Studio、连接的设备或者网络资源出错。

如果 flutter doctor 指令返回这些组件中的任何一个错误,请使用 verbose 标志再次运行。

$ flutter doctor -v

查看输出结果,了解可能需要安装的其他软件或者需要执行的其他任务。

如果你更改了 Flutter SDK 或其他相关组件的配置,请再次运行 flutter doctor 来检查安装。

管理 Flutter SDK

想要了解管理 Flutter SDK 安装的更多信息,请查阅以下内容。