在 macOS 上安装和配置 Flutter 开发环境

系统配置要求

System requirements

想要安装并运行 Flutter,你的开发环境需要最低满足以下要求:

To install and run Flutter, your development environment must meet these minimum requirements:

  • 操作系统:macOS

    Operating Systems: macOS

  • 磁盘空间:2.8 GB(不包含 IDE 或其余工具所需要的磁盘空间)

    Disk Space: 2.8 GB (does not include disk space for IDE/tools).

  • 工具:Flutter 使用 git 进行安装和升级,我们建议您安装包含了 git 的 Xcode,或者您也可以 单独安装 git

    Tools: Flutter uses git for installation and upgrade. We recommend installing Xcode, which includes git, but you can also install git separately.

获取 Flutter SDK

Get the Flutter SDK

  1. 下载以下安装包来获取最新的 stable Flutter SDK:

    Download the following installation bundle to get the latest stable release of the Flutter SDK:

    (loading…)

    想要获取到其他版本的安装包,请参阅 SDK 版本列表 页面。

    For other release channels, and older builds, see the SDK releases page.

  2. 将文件解压到目标路径, 比如:

    Extract the file in the desired location, for example:

    $ cd ~/development
    $ unzip ~/Downloads/flutter_macos_vX.X.X-stable.zip
    
  3. 配置 flutter 的 PATH 环境变量:

    Add the flutter tool to your path:

    $ export PATH="$PATH:`pwd`/flutter/bin"
    

    这个命令配置了 PATH 环境变量,且只会在你 当前 命令行窗口中生效。如果想让它永久生效,请查看 更新 PATH 环境变量

    This command sets your PATH variable for the current terminal window only. To permanently add Flutter to your path, see Update your path.

现在你可以愉快地运行 Flutter 的命令行啦!

You are now ready to run Flutter commands!

运行 flutter doctor 命令

Run flutter doctor

通过运行以下命令来查看当前环境是否需要安装其他的依赖(如果想查看更详细的输出,增加一个 -v 参数即可):

Run the following command to see if there are any dependencies you need to install to complete the setup (for verbose output, add the -v flag):

$ flutter doctor

这个命令会检查你当前的配置环境,并在命令行窗口中生成一份报告。安装 Flutter 会附带安装 Dart SDK,所以不需要再对 Dart 进行单独安装。你需要仔细阅读上述命令生成的报告,看看别漏了一些需要安装的依赖,或者需要之后执行的命令(这个会以 加粗的文本 显示出来)。

This command checks your environment and displays a report to the terminal window. The Dart SDK is bundled with Flutter; it is not necessary to install Dart separately. Check the output carefully for other software you might need to install or further tasks to perform (shown in bold text).

比如你可能会看到下面这样的输出:

For example:

[-] Android toolchain - develop for Android devices
    • Android SDK at /Users/obiwan/Library/Android/sdk
    ✗ Android SDK is missing command line tools; download from https://goo.gl/XxQghQ
    • Try re-installing or updating your Android SDK,
      visit https://flutter.cn/setup/#android-setup for detailed instructions.

之后的部分会向你描述如果执行这些命令来完成整体的配置过程。

The following sections describe how to perform these tasks and finish the setup process.

Once you have installed any missing dependencies, run the flutter doctor command again to verify that you’ve set everything up correctly.

直接从 Github 上(而不是归档)下载

Downloading straight from GitHub instead of using an archive

该建议仅适用于高级用例

This is only suggested for advanced use cases.

你也可以不从归档,而是用 Git 直接下载。例如,可以运行下方的命令,以下载稳定分支的 SDK:

You can also use git directly instead of downloading the prepared archive. For example, to download the stable branch:

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

更新环境变量,并运行 flutter doctor。这个命令将会告诉你,是否还缺少运行 Flutter 所需要安装的其他依赖项(例如 Android SDK)。

Update your path, and run flutter doctor. That will let you know if there are other dependencies you need to install to use Flutter (e.g. the Android SDK).

如果你不使用归档,Flutter 将会下载必要的开发二进制文件(如果你使用的归档,那么这些文件已经包含在内了)。你也许会想要提前下载这些开发二进制文件(例如,您可能希望设置系统构建环境,或是您的网络可用性不佳)。那么你可以运行以下命令:

If you did not use the archive, Flutter will download necessary development binaries as they are needed (if you used the archive, they are included in the download). You may wish to pre-download these development binaries (for example, you may wish to do this when setting up hermetic build environments, or if you only have intermittent network availability). To do so, run the following command:

$ flutter precache

更多额外下载选项,请参阅 flutter help precache

For additional download options, see flutter help precache.

更新 PATH 环境变量

Update your path

正如 获取 Flutter SDK 一文中所展示你可以仅仅在当前命令行会话中更新你的 PATH 环境变量。但你也许想让这个配置永久生效,这样就可以在任意一个命令行会话中使用 flutter 命令了。

You can update your PATH variable for the current session only at the command line, as shown in Get the Flutter SDK. You’ll probably want to update this variable permanently, so you can run flutter commands in any terminal session.

以下配置环境变量让其永久生效的方法在不同的机器上有略微的差别。基本上方法都是在某一个文件中增加一句命令,让它在每次新的命令行窗口打开时都执行一遍。比如:

The steps for modifying this variable permanently for all terminal sessions are machine-specific. Typically you add a line to a file that is executed whenever you open a new window. For example:

  1. 首先决定你想要将 Flutter SDK 下载或者通过 git clone 命令到哪一个目录内,获取并记录这个目录的路径,你将要在第 3 步中用到它。

    Determine the path of your clone of the Flutter SDK. You need this in Step 3.

  2. 打开或者创建 shell 的 rc 文件,比如,在 Linux 和 macOS Mojave 或 Mojave 之前的系统里,是默认使用 Bash 的,所以需要修改 $HOME/.bashrc 文件。 macOS Catalina 操作系统默认使用 Z Shell,所以需要修改 $HOME/.zshrc 文件。请知晓,如果你使用不同的 shell,文件目录或文件名可能会有所不同。

    Open (or create) the rc file for your shell. Typing echo $SHELL in your Terminal tells you which shell you’re using. If you’re using Bash, edit $HOME/.bash_profile or $HOME/.bashrc. If you’re using Z shell, edit $HOME/.zshrc. If you’re using a different shell, the file path and filename will be different on your machine.

  3. 在文件中增加下列这行命令,并将其中的 [PATH_OF_FLUTTER_GIT_DIRECTORY] 更改为你第一步获取到的路径:

    Add the following line and change [PATH_OF_FLUTTER_GIT_DIRECTORY] to be the path of your clone of the Flutter git repo:

    $ export PATH="$PATH:[PATH_OF_FLUTTER_GIT_DIRECTORY]/bin"
    
  4. 运行 source $HOME/.bash_profile 来刷新当前命令行窗口。

    Run source $HOME/.<rc file> to refresh the current window, or open a new terminal window to automatically source the file.

  5. 通过运行以下命令来验证 flutter/bin 文件夹是否已经添加到 PATH 环境变量中:

    Verify that the flutter/bin directory is now in your PATH by running:

    $ echo $PATH
    

    验证 flutter 命令是否可用,可以执行下面的命令检测:

    Verify that the flutter command is available by running:

    $ which flutter
    

平台配置

Platform setup

macOS 可以允许开发 iOS、Android 和 Web(技术预览版正式发布)三个平台的 Flutter 应用,你可以任选一个平台完成初始配置,以此来搭建并运行起来你的第一个 Flutter 应用。

macOS supports developing Flutter apps in iOS, Android, and the web (technical preview release). Complete at least one of the platform setup steps now, to be able to build and run your first Flutter app.

设置 iOS 开发环境

iOS setup

安装 Xcode

Install Xcode

开发 iOS 平台上的 Flutter 应用,你需要一个安装了 Xcode 的 Mac 设备。

To develop Flutter apps for iOS, you need a Mac with Xcode installed.

  1. 通过 直接下载 或者通过 Mac App Store 来安装最新稳定版 Xcode;

    Install the latest stable version of Xcode (using web download or the Mac App Store).

  2. 通过在命令行中运行以下命令来配置 Xcode command-line tools:

    Configure the Xcode command-line tools to use the newly-installed version of Xcode by running the following from the command line:

    $ sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
    $ sudo xcodebuild -runFirstLaunch
    

    当你安装了最新版本的 Xcode,大部分情况下,上面的路径都是一样的。但如果你安装了不同版本的 Xcode,你可能要更改一下上述命令中的路径。

    This is the correct path for most cases, when you want to use the latest version of Xcode. If you need to use a different version, specify that path instead.

  3. 运行一次 Xcode 或者通过输入命令 sudo xcodebuild -license 来确保已经同意 Xcode 的许可协议。

    Make sure the Xcode license agreement is signed by either opening Xcode once and confirming or running sudo xcodebuild -license from the command line.

旧版本可能也能够正常工作,但是不建议在 Flutter 开发环境中使用。旧版本的 Xcode 不支持定位代码,还可能无法正常工作。

Versions older than the latest stable version may still work, but are not recommended for Flutter development. Using old versions of Xcode to target bitcode is not supported, and is likely not to work.

安装了 Xcode 之后,你就可以在 iOS 真机或者模拟器上运行 Flutter 应用了。

With Xcode, you’ll be able to run Flutter apps on an iOS device or on the simulator.

配置 iOS 模拟器

Set up the iOS simulator

如果想要在 iOS 模拟器中运行和测试 Flutter 应用,按照以下步骤即可:

To prepare to run and test your Flutter app on the iOS simulator, follow these steps:

  1. 在你的 Mac 中,通过 Spotlight 或者以下命令来运行模拟器:

    On your Mac, find the Simulator via Spotlight or by using the following command:

    $ open -a Simulator
    
  2. 通过模拟器菜单中的 Hardware > Device 选项检查当前模拟器是否是 64 位机型(iPhone 5S 或之后的机型)。

    Make sure your simulator is using a 64-bit device (iPhone 5s or later) by checking the settings in the simulator’s Hardware > Device menu.

  3. Depending on your development machine’s screen size, simulated high-screen-density iOS devices might overflow your screen. Grab the corner of the simulator and drag it to change the scale. You can also use the Window > Physical Size or Window > Pixel Accurate options if your computer’s resolution is high enough.

    • If you are using a version of Xcode older than 9.1, you should instead set the device scale in the Window > Scale menu.

    根据你当前开发机器的屏幕尺寸,模拟器模拟出来的高密度屏幕的设备可能会占满你的屏幕,你可以通过菜单中的 Window > Scale 选项来更改模拟器的缩放比例。

创建并运行一个简单的 Flutter 应用

Create and run a simple Flutter app

通过以下步骤来创建你的第一个 Flutter 应用并进行测试:

To create your first Flutter app and test your setup, follow these steps:

  1. 通过运行以下命令来创建一个新的 Flutter 应用:

    Create a new Flutter app by running the following from the command line:

    $ flutter create my_app
    
  2. 上述命令创建了一个 my_app 的目录,包含了 Flutter 初始的应用模版,切换路径到这个目录内:

    A my_app directory is created, containing Flutter’s starter app. Enter this directory:

    $ cd my_app
    
  3. 确保模拟器已经处于运行状态,输入以下命令来启动应用:

    To launch the app in the Simulator, ensure that the Simulator is running and enter:

    $ flutter run
    

Deploy to iOS devices

如果你想把 Flutter 应用部署到 iOS 的真机上,你还需要一些别的工具和一个 Apple 开发者账号。另外,你还需要在 Xcode 上针对你的机器做一些设置。

To deploy your Flutter app to a physical iOS device you’ll need to set up physical device deployment in Xcode and an Apple Developer account. If your app is using Flutter plugins, you will also need the third-party CocoaPods dependency manager.

  1. 如果你的应用不依赖 Flutter plugins 与原生 iOS 代码交互,你可以跳过这一步。通过运行以下命令 安装和设置CocoaPods

    You can skip this step if your apps do not depend on Flutter plugins with native iOS code. Install and set up CocoaPods by running the following commands:

    $ sudo gem install cocoapods
    
  2. 按照下面 Xcode 签名流程来配置你的项目:

    Follow the Xcode signing flow to provision your project:

    1. 通过在命令行中于你当前 Flutter 项目目录下运行 open ios/Runner.xcworkspace 命令来打开默认的 Xcode 工程。

      Open the default Xcode workspace in your project by running open ios/Runner.xcworkspace in a terminal window from your Flutter project directory.

    2. 在运行按钮的下拉列表里选择你想要部署到的设备;

      Select the device you intend to deploy to in the device drop-down menu next to the run button.

    3. 在左侧的导航面板中选择 Runner 项目;

      Select the Runner project in the left navigation panel.

    4. Runner 项目的设置页面,请确保勾选你的开发团队。在不同的 Xcode 版本里,这一部分的操作界面不同:

      In the Runner target settings page, make sure your Development Team is selected under Signing & Capabilities > Team.

      当选择了一个团队之后,Xcode 会创建和下载一个开发证书,并在你的账户里为你的社保注册,并在需要的时候创建和下载一个配置文件。

      When you select a team, Xcode creates and downloads a Development Certificate, registers your device with your account, and creates and downloads a provisioning profile (if needed).

      • 在开始你的第一个 iOS 项目开发之前,你需要先在 Xcode 中登陆你的 Apple 开发者账号 Xcode account add 任何 Apple ID 都可以进行开发和测试。如果想将应用上架 App Store,你需要加入 Apple Developer Program,你可以在 Choosing a Membership 页面中查看详细的说明。

        To start your first iOS development project, you might need to sign into Xcode with your Apple ID. Xcode account add Development and testing is supported for any Apple ID. Enrolling in the Apple Developer Program is required to distribute your app to the App Store. For details about membership types, see Choosing a Membership.

      • 当你第一次将设备连接到开发机用于开发时,你需要分别在 Mac 和开发机上进行信任设备的操作。当你第一次连接时,会有个弹窗,点击 Trust 即可。

        The first time you use an attached physical device for iOS development, you need to trust both your Mac and the Development Certificate on that device. Select Trust in the dialog prompt when first connecting the iOS device to your Mac.

        Trust Mac

        然后在 iOS 开发机上进入 Settings 应用,选择 General > Device Management 然后信任相应的证书。对于首次打开的用户,请选择 General > Profiles > Device Management

        Then, go to the Settings app on the iOS device, select General > Device Management and trust your Certificate. For first time users, you may need to select General > Profiles > Device Management instead.

      • 如果 Xcode 的自动签名失败了,你可以检查以下项目中 General > Identity > Bundle Identifier 里的值是否是唯一的。

        If automatic signing fails in Xcode, verify that the project’s General > Identity > Bundle Identifier value is unique.

        Check the app's Bundle ID

  3. 执行 flutter run 命令,或者在 Xcode 里点击运行,来运行你的应用。

    Start your app by running flutter run or clicking the Run button in Xcode.

设置 Android 开发环境

Android setup

安装 Android Studio

Install Android Studio

  1. 下载并安装 Android Studio

    Download and install Android Studio.

  2. 运行 Android Studio,并进入 ‘Android Studio Setup Wizard’,这会安装最新的 Android SDK, Android SDK Platform-Tools 以及 Android SDK Build-Tools,这些都是在开发 Android Flutter 应用时所需要的。

    Start Android Studio, and go through the ‘Android Studio Setup Wizard’. This installs the latest Android SDK, Android SDK Command-line Tools, and Android SDK Build-Tools, which are required by Flutter when developing for Android.

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

    Run flutter doctor to confirm that Flutter has located your installation of Android Studio. If Flutter cannot locate it, run flutter config --android-studio-dir <directory> to set the directory that Android Studio is installed to.

配置 Android 设备

Set up your Android device

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

To prepare to run and test your Flutter app on an Android device, you need an Android device running Android 4.1 (API level 16) or higher.

  1. 在设备上打开 Developer optionsUSB debugging 选项,你可以在 Android documentation 上查看更详细的方法介绍。

    Enable Developer options and USB debugging on your device. Detailed instructions are available in the Android documentation.

  2. 如果是在 Windows 平台上使用,需要安装 Google USB Driver

    Windows-only: Install the Google USB Driver.

  3. 通过 USB 接口连接手机和电脑,如果在设备上弹出需要授权弹窗,允许授权以便让电脑能够访问你的开发设备。

    Using a USB cable, plug your phone into your computer. If prompted on your device, authorize your computer to access your device.

  4. 在命令行中,使用 flutter devices 命令来确保 Flutter 能够识别出你所连接的 Android 设备。

    In the terminal, run the flutter devices command to verify that Flutter recognizes your connected Android device. By default, Flutter uses the version of the Android SDK where your adb tool is based. If you want Flutter to use a different installation of the Android SDK, you must set the ANDROID_SDK_ROOT environment variable to that installation directory.

配置 Android 模拟器

Set up the Android emulator

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

To prepare to run and test your Flutter app on the Android emulator, follow these steps:

  1. 激活机器上的 VM acceleration 选项。

    Enable VM acceleration on your machine.

  2. 打开 Android Studio,点击 AVD Manager 按钮,选择 Create Virtual Device…

    Launch Android Studio, click the AVD Manager icon, and select Create Virtual Device…

    • 在一些旧的 Android Studio 版本里,需要通过 Android Studio > Tools > Android > AVD Manager,然后选择 Create Virtual Device… 选项。(只有在 Android 项目中才会显示 Android 子选项。)

      In older versions of Android Studio, you should instead launch Android Studio > Tools > Android > AVD Manager and select Create Virtual Device…. (The Android submenu is only present when inside an Android project.)

    • 如果你以及还没打开某个项目,你可以选择 Configure > AVD Manager 然后选择 Create Virtual Device 选项

      If you do not have a project open, you can choose Configure > AVD Manager and select Create Virtual Device…

  3. 选择相应的设备并选择 Next 选项。

    Choose a device definition and select Next.

  4. 选择一个或多个你想要模拟的 Android 版本的系统镜像,然后选择 Next 选项。推荐选择 x86 或者 x86_64 镜像。

    Select one or more system images for the Android versions you want to emulate, and select Next. An x86 or x86_64 image is recommended.

  5. 在 Emulated Performance 下选择 Hardware - GLES 2.0 选项来开启 硬件加速

    Under Emulated Performance, select Hardware - GLES 2.0 to enable hardware acceleration.

  6. 确保 AVD 选项配置正确,并选择 Finish 选项。

    Verify the AVD configuration is correct, and select Finish.

    想要查看上述步骤的更多详细信息,请查看 Managing AVDs 页面。

    For details on the above steps, see Managing AVDs.

  7. 在 Android Virtual Device Manager 中,点击工具栏中的 Run 选项,模拟器会启动并为你所选择的系统版本和设备显示出相应的界面。

    In Android Virtual Device Manager, click Run in the toolbar. The emulator starts up and displays the default canvas for your selected OS version and device.

同意 Android 协议

Agree to Android Licenses

在使用 Flutter 前,你必须同意 Android SDK 平台的协议。你可以在安装完上述工具后执行这一步。

Before you can use Flutter, you must agree to the licenses of the Android SDK platform. This step should be done after you have installed the tools listed above.

  1. 确保你安装了 Java 8,并且正确设置了 JAVA_HOME 环境变量到 JDK 目录。

    Make sure that you have a version of Java 8 installed and that your JAVA_HOME environment variable is set to the JDK’s folder.

    高于 2.2 版本的 Android Studio 自带了 JDK,所以应无需手动操作。

    Android Studio versions 2.2 and higher come with a JDK, so this should already be done.

  2. 打开一个已经提升管理员权限的终端窗口,运行以下命令进行协议的确认。

    Open an elevated console window and run the following command to begin signing licenses.

    $ flutter doctor --android-licenses
    
  3. 仔细阅读每条协议后同意。

    Review the terms of each license carefully before agreeing to them.

  4. 当你同意所有协议后,再次运行 flutter doctor 以确认是否已经可以正常使用 Flutter。

    Once you are done agreeing with licenses, run flutter doctor again to confirm that you are ready to use Flutter.

macOS 体系

macOS setup

其他 macOS 需要的内容

Additional macOS requirements

对于开发 macOS 桌面端应用来说,除了 Flutter SDK 以外你还需要以下内容:

For macOS desktop development, you need the following in addition to the Flutter SDK:

开启桌面端支持

Enable desktop support

在命令行中执行以下命令以开启桌面端支持

At the command line, perform the following command to enable desktop support

$ flutter config --enable-macos-desktop

更多详情请查看 Flutter 的桌面端支持

For more information, see Desktop support for Flutter

Web 平台设置

Web setup

Flutter 现已在 stable 渠道支持构建 Web 应用。使用 Flutter 2 构建的应用会自动支持 Web 渠道,如果你需要为现有的应用加入 Web 支持,请阅读文档 使用 Flutter 构建 Web 应用

Flutter has support for building web applications in the stable channel. Any app created in Flutter 2 automatically builds for the web. To add web support to an existing app, follow the instructions on Building a web application with Flutter when you’ve completed the setup above.

下一步

Next step

编辑器设置。

Set up your preferred editor.