跳转至正文

设置 Android 开发环境

配置开发环境,以便在 Android 设备上运行、构建和部署 Flutter 应用。

了解如何配置开发环境,以便在 Android 设备上运行、构建和部署 Flutter 应用。

选择你的开发平台

#

本页说明默认面向在 Windows 设备上设置 Android 开发。

若要为其他操作系统遵循说明,请选择以下之一。

设置 Android 工具链

#

借助 Android Studio,你可以在实体 Android 设备或 Android 模拟器上运行 Flutter 应用。

若尚未完成,请安装并设置最新稳定版 Android Studio

  1. Install prerequisites libraries

  2. 安装必备库

    若在 Linux 上开发,请先安装 Android Studio 所需的 32 位库必备集合

  3. Install Android Studio

  4. 安装 Android Studio

    若尚未完成,请安装并设置最新稳定版 Android Studio

    若已安装 Android Studio,请确保其为最新版本

  5. Install Android SDK and tools

  6. 安装 Android SDK 与工具

    1. Launch Android Studio.

    2. 启动 Android Studio

    3. Open the SDK Manager settings dialog.

    4. 打开 SDK Manager 设置对话框。

      1. If the Welcome to Android Studio dialog is open, click the More Actions button that follows the New Project and Open buttons, then click SDK Manager from the dropdown menu.

      2. Welcome to Android Studio 对话框已打开,点击 New ProjectOpen 按钮后的 More Actions 按钮,然后从下拉菜单点击 SDK Manager

      3. If you have a project open, go to Tools > SDK Manager.

      4. 若已打开项目,前往 Tools > SDK Manager

    5. If the SDK Platforms tab is not open, switch to it.

    6. SDK Platforms 标签页未打开,请切换到该标签页。

    7. Verify that the first entry with an API Level of 36 has been selected.

    8. 确认已选中 API Level36 的第一项。

      Status 列显示 Update availableNot installed

      1. Select the checkbox for that entry or row.

      2. 选中该项或该行的复选框。

      3. Click Apply.

      4. 点击 Apply

      5. When the Confirm Change dialog displays, click OK.

      6. Confirm Change 对话框出现时,点击 OK

        SDK Component Installer 对话框会显示进度指示器。

      7. When the installation finishes, click Finish.

      8. 安装完成后,点击 Finish

    9. Switch to the SDK Tools tab.

    10. 切换到 SDK Tools 标签页。

    11. Verify that the following SDK Tools have been selected:

    12. 确认已选中以下 SDK Tools:

      • Android SDK Build-Tools
      • Android SDK Command-line Tools
      • Android Emulator
      • Android SDK Platform-Tools
      • CMake
      • NDK (Side by side)
    13. If the Status column for any of the preceding tools displays Update available or Not installed:

    14. 若上述任一工具的 Status 列显示 Update availableNot installed

      1. Select the checkbox for the necessary tools.

      2. 选中所需工具的复选框。

      3. Click Apply.

      4. 点击 Apply

      5. When the Confirm Change dialog displays, click OK.

      6. Confirm Change 对话框出现时,点击 OK

        SDK Component Installer 对话框会显示进度指示器。

      7. When the installation finishes, click Finish.

      8. 安装完成后,点击 Finish

  7. Agree to the Android licenses

  8. 同意 Android 许可

    在安装所有必备项后、使用 Flutter 之前,请同意 Android SDK 平台的许可。

    1. Open your preferred terminal.

    2. 打开你常用的终端。

    3. Run the following command to review and sign the SDK licenses.

    4. 运行以下命令以查看并签署 SDK 许可。

      flutter doctor --android-licenses
      
    5. Read and accept any necessary licenses.

    6. 阅读并接受所有必要的许可。

      若你此前未接受各项 SDK 许可,在针对 Android 开发前需要查看并同意它们。

      在同意每项许可条款前,请仔细阅读。

      成功接受所有必要许可后,你应看到类似以下的输出:

      All SDK package licenses accepted.
      

设置 Android 设备

#

你可以在实体 Android 设备上调试 Flutter 应用,或在 Android 模拟器上运行它们。

要配置开发环境以在 Android 模拟器上运行 Flutter 应用,请按以下步骤操作:

  1. Set up your development device

  2. 设置你的开发设备

    在开发计算机上启用 VM 加速

  3. Set up a new emulator

  4. 设置新模拟器

    1. Start Android Studio.

    2. 启动 Android Studio

    3. Open the Device Manager settings dialog.

    4. 打开 Device Manager 设置对话框。

      1. If the Welcome to Android Studio dialog is open, click the More Actions button that follows the New Project and Open buttons, then select Virtual Device Manager from the dropdown menu.

      2. Welcome to Android Studio 对话框已打开,点击 New ProjectOpen 按钮后的 More Actions 按钮,然后从下拉菜单选择 Virtual Device Manager

      3. If you have a project open, go to Tools > Device Manager.

      4. 若已打开项目,前往 Tools > Device Manager

    5. Click the Create Virtual Device button that appears as a + icon.

    6. 点击显示为 + 图标的 Create Virtual Device 按钮。

      Virtual Device Configuration 对话框会出现。

    7. Select either Phone or Tablet under Form Factor.

    8. Form Factor 下选择 PhoneTablet

    9. Select a device definition. You can browse or search for the device.

    10. 选择设备定义。你可以浏览或搜索设备。

    11. Click Next.

    12. 点击 Next

    13. If the option is provided, select either x86 Images or ARM Images depending on if your development computer is an x64 or Arm64 device.

    14. 若提供该选项,根据开发计算机是 x64 还是 Arm64 设备,选择 x86 ImagesARM Images

    15. Select one system image for the Android version you want to emulate.

    16. 为要模拟的 Android 版本选择一个系统镜像。

      1. If the desired image has a Download icon to the left of the system image name, click it.

      2. 若所需镜像在系统镜像名称左侧有 Download 图标,请点击它。

        SDK Component Installer 对话框会显示进度指示器。

      3. When the download completes, click Finish.

      4. 下载完成后,点击 Finish

    17. Click Additional settings in the top tab bar and scroll to Emulated Performance.

    18. 点击顶部标签栏中的 Additional settings,滚动到 Emulated Performance

    19. From the Graphics acceleration dropdown menu, select an option that mentions Hardware.

    20. Graphics acceleration 下拉菜单中,选择提及 Hardware 的选项。

      这将启用硬件加速,提升渲染性能。

    21. Verify your virtual device configuration. If it is correct, click Finish.

    22. 验证虚拟设备配置。若正确,点击 Finish

      要了解更多虚拟设备信息,请参阅创建和管理虚拟设备

  5. Try running the emulator

  6. 尝试运行模拟器

    Device Manager 对话框中,点击所需虚拟设备右侧的 Run 图标。

    模拟器应启动并显示所选 Android 操作系统版本和设备的默认画布。

要配置开发环境以在实体 Android 设备上运行 Flutter 应用,请按以下步骤操作:

  1. Configure your device

  2. 配置你的设备

    配置设备端开发者选项中的说明,在设备上启用 Developer optionsUSB debugging

  3. Enable wireless debugging

  4. 启用无线调试

    要使用无线调试,请按使用 Wi-Fi 连接设备中的说明,在设备上启用 Wireless debugging

  5. Install platform prerequisites

  6. 安装平台必备项

    若在 Windows 上开发,请先按安装 OEM USB 驱动中的说明,为特定设备安装必要的 USB 驱动。

  7. Connect your device

  8. 连接你的设备

    将设备插入计算机。若设备提示你,请授权计算机访问你的 Android 设备。

  9. Verify the device connection

  10. 验证设备连接

    要验证 Flutter 是否识别已连接的 Android 设备,请在你常用的终端运行 flutter devices

    flutter devices
    

    应能找到你的设备并将其显示为已连接设备。

验证你的设置

#
  1. Check for toolchain issues

  2. 检查工具链问题

    要检查 Android 开发设置是否存在问题,请在你常用的终端运行 flutter doctor 命令:

    flutter doctor
    

    若在 Android toolchainAndroid Studio 部分看到任何错误或待完成任务,

    请完成所有提到的任务,然后再次运行 flutter doctor 以验证变更。

  3. Check for Android devices

  4. 检查 Android 设备

    为确保你正确设置了模拟器和/或实体 Android 设备,请在你常用的终端运行 flutter emulatorsflutter devices

    flutter emulators && flutter devices
    

    根据你设置的是模拟器还是设备,至少应有一项输出平台标记为 android 的条目。

  5. Troubleshoot setup issues

  6. 排查设置问题

    若需要帮助解决任何设置问题,请参阅安装与设置故障排除

    若仍有问题或疑问,可通过 Flutter 社区 渠道联系。

开始为 Android 开发

#

恭喜!既然你已为 Flutter 设置好 Android 开发,你可以在 Android 上测试的同时继续 Flutter 学习之旅,或开始改进与 Android 的集成。