flutter-environment-setup-windows
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGoal
目标
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
操作指引
-
Configure Flutter SDK and Environment Variables Extract the Flutter SDK to a secure, user-writable directory (e.g.,). Do not place it in
C:\develop\flutter. Execute the following PowerShell command to append the FlutterC:\Program Files\directory to the user'sbin:PATHpowershell$flutterBinPath = "C:\develop\flutter\bin" $currentUserPath = [Environment]::GetEnvironmentVariable("Path", [EnvironmentVariableTarget]::User) if ($currentUserPath -notmatch [regex]::Escape($flutterBinPath)) { [Environment]::SetEnvironmentVariable("Path", "$currentUserPath;$flutterBinPath", [EnvironmentVariableTarget]::User) } -
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 -
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.
- If (A) Windows Desktop: Proceed to Step 5. Disable Android and Web if unnecessary:
-
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".
-
Build and Package Windows Desktop Applications To compile the Windows application, execute:cmd
flutter build windowsTo package the application manually, gather the following files from:build\windows\runner\Release\- The executable ()
<project_name>.exe - All files (e.g.,
.dll)flutter_windows.dll - The directory
data - Visual C++ redistributables (,
msvcp140.dll,vcruntime140.dll) placed adjacent to thevcruntime140_1.dll..exe
Optional: To rename the generated executable, modify theinBINARY_NAME:windows/CMakeLists.txtcmake# Change this to change the on-disk name of your application. set(BINARY_NAME "CustomAppName") - The executable (
-
Generate Self-Signed Certificates for MSIX Packaging (OpenSSL) If the user requires local testing of an MSIX package, generate acertificate. Ensure OpenSSL is in the
.pfx, then execute:PATHcmdopenssl 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.crtInstruct the user to installinto the local machine's Certificate Store under "Trusted Root Certification Authorities".CERTIFICATE.pfx -
Validate-and-Fix Feedback Loop Execute the Flutter diagnostic tool to verify the environment:cmd
flutter doctor -v- Condition: If is reported, instruct the user to open Android Studio -> Tools -> SDK Manager -> SDK Tools, and check "Android SDK Command-line Tools".
cmdline-tools component is missing - Condition: If Visual Studio toolchain issues are reported, verify the workload is fully installed.
Microsoft.VisualStudio.Workload.NativeDesktop - Condition: If is not recognized, verify the PowerShell
flutterinjection succeeded and restart the terminal process.PATH
- Condition: If
- 配置Flutter SDK和环境变量
将Flutter SDK解压到用户可写的安全目录中(例如),请勿放在
C:\develop\flutter目录下。 执行以下PowerShell命令将Flutter的C:\Program Files\目录追加到用户的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)
}- 安装Windows工具集 要编译Windows桌面应用,必须安装Visual Studio(而非VS Code),安装时选择“使用C++进行桌面开发”工作负载。 如果需要通过命令行自动化安装,使用以下工作负载ID:
cmd
vs_setup.exe --add Microsoft.VisualStudio.Workload.NativeDesktop --includeRecommended- 决策逻辑:目标平台配置 请先询问用户: “你要为这个环境配置哪些目标平台?(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,确保两个平台都已启用。
- 在Windows上配置Android工具集
- 安装Android Studio,通过SDK Manager安装Android SDK Command-line Tools。
- 针对真机调试:在设备上开启“开发者选项”和“USB调试”,安装Windows对应的OEM USB驱动。
- 针对模拟器:在AVD Manager的“模拟性能”选项下选择“硬件”图形加速选项,开启硬件加速。
- 构建和打包Windows桌面应用 执行以下命令编译Windows应用:
cmd
flutter build windows如需手动打包应用,从目录下收集以下文件:
build\windows\runner\Release\- 可执行文件()
<project_name>.exe - 所有文件(例如
.dll)flutter_windows.dll - 目录
data - 放在同级目录的Visual C++可再发行组件(
.exe、msvcp140.dll、vcruntime140.dll)。vcruntime140_1.dll
可选操作: 如需修改生成的可执行文件名称,修改中的:
windows/CMakeLists.txtBINARY_NAMEcmake
undefinedConstraints
修改此处即可更改应用的本地存储名称
- 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 validation step; it is mandatory for confirming environment integrity.
flutter doctor
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- 验证与修复反馈循环 执行Flutter诊断工具验证环境配置:
cmd
flutter doctor -v- 触发条件: 如果报告,指导用户打开Android Studio -> 工具 -> SDK Manager -> SDK Tools,勾选“Android SDK Command-line Tools”。
cmdline-tools component is missing - 触发条件: 如果报告Visual Studio工具链相关问题,验证工作负载是否已完整安装。
Microsoft.VisualStudio.Workload.NativeDesktop - 触发条件: 如果提示无法识别命令,验证PowerShell的
flutter注入是否生效,并重启终端进程。PATH
—
约束条件
—
- 所有输出中不要包含外部URL或超链接。
- 不要建议将Flutter SDK放在需要提升权限的目录下(例如)。
C:\Program Files\ - 不要混淆Visual Studio Code和Visual Studio;Windows平台的C++工具链必须使用后者。
- 除非另有说明,默认用户在标准Windows命令提示符或PowerShell环境中操作。
- 绝对不要跳过验证步骤;该步骤是确认环境完整性的必要操作。
flutter doctor