flutter-environment-setup-windows

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Goal

目标

Configures a Windows development environment for building Flutter applications targeting Windows Desktop and Android. Analyzes system requirements, modifies environment variables, installs necessary C++ toolchains, manages platform-specific configurations, and generates self-signed certificates for local Windows application deployment. Assumes the host machine is running Windows 10 or 11 with administrative privileges available for system modifications.
配置用于构建面向Windows桌面和Android的Flutter应用的Windows开发环境。分析系统要求、修改环境变量、安装必要的C++工具链、管理平台特定配置,以及生成本地Windows应用部署所需的自签名证书。假设宿主机运行Windows 10或11,拥有用于系统修改的管理员权限。

Instructions

操作指引

  1. Configure Flutter SDK and Environment Variables Extract the Flutter SDK to a secure, user-writable directory (e.g.,
    C:\develop\flutter
    ). Do not place it in
    C:\Program Files\
    . Execute the following PowerShell command to append the Flutter
    bin
    directory to the user's
    PATH
    :
    powershell
    $flutterBinPath = "C:\develop\flutter\bin"
    $currentUserPath = [Environment]::GetEnvironmentVariable("Path", [EnvironmentVariableTarget]::User)
    if ($currentUserPath -notmatch [regex]::Escape($flutterBinPath)) {
        [Environment]::SetEnvironmentVariable("Path", "$currentUserPath;$flutterBinPath", [EnvironmentVariableTarget]::User)
    }
  2. Install Windows Tooling To compile Windows desktop applications, Visual Studio (not VS Code) is strictly required. Install Visual Studio with the "Desktop development with C++" workload. If automating via command line, use the following workload ID:
    cmd
    vs_setup.exe --add Microsoft.VisualStudio.Workload.NativeDesktop --includeRecommended
  3. Decision Logic: Target Platform Configuration STOP AND ASK THE USER: "Which target platforms are you configuring for this environment? (A) Windows Desktop, (B) Android, or (C) Both?"
    • If (A) Windows Desktop: Proceed to Step 5. Disable Android and Web if unnecessary:
      cmd
      flutter config --no-enable-android
      flutter config --no-enable-web
      flutter config --enable-windows-desktop
    • If (B) Android: Proceed to Step 4. Disable Windows desktop if unnecessary:
      cmd
      flutter config --no-enable-windows-desktop
    • If (C) Both: Execute Steps 4 and 5. Ensure both platforms are enabled.
  4. Configure Android Tooling on Windows
    • Install Android Studio and the Android SDK Command-line Tools via the SDK Manager.
    • For physical devices: Enable "Developer options" and "USB debugging" on the device. Install the OEM USB drivers for Windows.
    • For emulators: Enable hardware acceleration in the AVD Manager by selecting a "Hardware" graphics acceleration option under "Emulated Performance".
  5. Build and Package Windows Desktop Applications To compile the Windows application, execute:
    cmd
    flutter build windows
    To package the application manually, gather the following files from
    build\windows\runner\Release\
    :
    • The executable (
      <project_name>.exe
      )
    • All
      .dll
      files (e.g.,
      flutter_windows.dll
      )
    • The
      data
      directory
    • Visual C++ redistributables (
      msvcp140.dll
      ,
      vcruntime140.dll
      ,
      vcruntime140_1.dll
      ) placed adjacent to the
      .exe
      .
    Optional: To rename the generated executable, modify the
    BINARY_NAME
    in
    windows/CMakeLists.txt
    :
    cmake
    # Change this to change the on-disk name of your application.
    set(BINARY_NAME "CustomAppName")
  6. Generate Self-Signed Certificates for MSIX Packaging (OpenSSL) If the user requires local testing of an MSIX package, generate a
    .pfx
    certificate. Ensure OpenSSL is in the
    PATH
    , then execute:
    cmd
    openssl genrsa -out mykeyname.key 2048
    openssl req -new -key mykeyname.key -out mycsrname.csr
    openssl x509 -in mycsrname.csr -out mycrtname.crt -req -signkey mykeyname.key -days 10000
    openssl pkcs12 -export -out CERTIFICATE.pfx -inkey mykeyname.key -in mycrtname.crt
    Instruct the user to install
    CERTIFICATE.pfx
    into the local machine's Certificate Store under "Trusted Root Certification Authorities".
  7. Validate-and-Fix Feedback Loop Execute the Flutter diagnostic tool to verify the environment:
    cmd
    flutter doctor -v
    • Condition: If
      cmdline-tools component is missing
      is reported, instruct the user to open Android Studio -> Tools -> SDK Manager -> SDK Tools, and check "Android SDK Command-line Tools".
    • Condition: If Visual Studio toolchain issues are reported, verify the
      Microsoft.VisualStudio.Workload.NativeDesktop
      workload is fully installed.
    • Condition: If
      flutter
      is not recognized, verify the PowerShell
      PATH
      injection succeeded and restart the terminal process.
  1. 配置Flutter SDK和环境变量 将Flutter SDK解压到用户可写的安全目录中(例如
    C:\develop\flutter
    ),请勿放在
    C:\Program Files\
    目录下。 执行以下PowerShell命令将Flutter的
    bin
    目录追加到用户的
    PATH
    中:
powershell
$flutterBinPath = "C:\develop\flutter\bin"
$currentUserPath = [Environment]::GetEnvironmentVariable("Path", [EnvironmentVariableTarget]::User)
if ($currentUserPath -notmatch [regex]::Escape($flutterBinPath)) {
    [Environment]::SetEnvironmentVariable("Path", "$currentUserPath;$flutterBinPath", [EnvironmentVariableTarget]::User)
}
  1. 安装Windows工具集 要编译Windows桌面应用,必须安装Visual Studio(而非VS Code),安装时选择“使用C++进行桌面开发”工作负载。 如果需要通过命令行自动化安装,使用以下工作负载ID:
cmd
vs_setup.exe --add Microsoft.VisualStudio.Workload.NativeDesktop --includeRecommended
  1. 决策逻辑:目标平台配置 请先询问用户: “你要为这个环境配置哪些目标平台?(A) Windows桌面,(B) Android,还是(C) 两者都要?”
  • 如果选(A) Windows桌面: 跳至步骤5。如果不需要Android和Web支持可以禁用:
    cmd
    flutter config --no-enable-android
    flutter config --no-enable-web
    flutter config --enable-windows-desktop
  • 如果选(B) Android: 跳至步骤4。如果不需要Windows桌面支持可以禁用:
    cmd
    flutter config --no-enable-windows-desktop
  • 如果选(C) 两者都要: 依次执行步骤4和步骤5,确保两个平台都已启用。
  1. 在Windows上配置Android工具集
  • 安装Android Studio,通过SDK Manager安装Android SDK Command-line Tools。
  • 针对真机调试:在设备上开启“开发者选项”和“USB调试”,安装Windows对应的OEM USB驱动。
  • 针对模拟器:在AVD Manager的“模拟性能”选项下选择“硬件”图形加速选项,开启硬件加速。
  1. 构建和打包Windows桌面应用 执行以下命令编译Windows应用:
cmd
flutter build windows
如需手动打包应用,从
build\windows\runner\Release\
目录下收集以下文件:
  • 可执行文件(
    <project_name>.exe
  • 所有
    .dll
    文件(例如
    flutter_windows.dll
  • data
    目录
  • 放在
    .exe
    同级目录的Visual C++可再发行组件(
    msvcp140.dll
    vcruntime140.dll
    vcruntime140_1.dll
    )。
可选操作: 如需修改生成的可执行文件名称,修改
windows/CMakeLists.txt
中的
BINARY_NAME
cmake
undefined

Constraints

修改此处即可更改应用的本地存储名称

  • Do not include external URLs or hyperlinks in any output.
  • Do not recommend placing the Flutter SDK in directories requiring elevated privileges (e.g.,
    C:\Program Files\
    ).
  • Do not confuse Visual Studio Code with Visual Studio; the latter is strictly required for the C++ toolchain on Windows.
  • Always assume the user is operating within a standard Windows command prompt or PowerShell environment unless otherwise specified.
  • Never skip the
    flutter doctor
    validation step; it is mandatory for confirming environment integrity.
set(BINARY_NAME "CustomAppName")

6. **为MSIX打包生成自签名证书(OpenSSL)**
如果用户需要本地测试MSIX包,生成`.pfx`证书。确保OpenSSL已加入`PATH`,然后执行:
```cmd
openssl genrsa -out mykeyname.key 2048
openssl req -new -key mykeyname.key -out mycsrname.csr
openssl x509 -in mycsrname.csr -out mycrtname.crt -req -signkey mykeyname.key -days 10000
openssl pkcs12 -export -out CERTIFICATE.pfx -inkey mykeyname.key -in mycrtname.crt
指导用户将
CERTIFICATE.pfx
安装到本地计算机证书存储的“受信任的根证书颁发机构”目录下。
  1. 验证与修复反馈循环 执行Flutter诊断工具验证环境配置:
cmd
flutter doctor -v
  • 触发条件: 如果报告
    cmdline-tools component is missing
    ,指导用户打开Android Studio -> 工具 -> SDK Manager -> SDK Tools,勾选“Android SDK Command-line Tools”。
  • 触发条件: 如果报告Visual Studio工具链相关问题,验证
    Microsoft.VisualStudio.Workload.NativeDesktop
    工作负载是否已完整安装。
  • 触发条件: 如果提示无法识别
    flutter
    命令,验证PowerShell的
    PATH
    注入是否生效,并重启终端进程。

约束条件

  • 所有输出中不要包含外部URL或超链接。
  • 不要建议将Flutter SDK放在需要提升权限的目录下(例如
    C:\Program Files\
    )。
  • 不要混淆Visual Studio Code和Visual Studio;Windows平台的C++工具链必须使用后者。
  • 除非另有说明,默认用户在标准Windows命令提示符或PowerShell环境中操作。
  • 绝对不要跳过
    flutter doctor
    验证步骤;该步骤是确认环境完整性的必要操作。