运行 flutter 桌面应用程序时出现 CMake 错误

CMake error while running flutter desktop application

当我尝试启动 flutter 桌面应用程序时,我遇到了这个错误:

"CMake Error at CMakeLists.txt:2 (project):

Generator

Visual Studio 16 2019

could not find any instance of Visual Studio.

Building Windows application...

Exception: Unable to generate build files"

我做了什么...

1- 安装 Visual Studio 2022 年(使用 C++ 和通用 Windows 平台开发的桌面开发)

2- 运行 这些命令:

flutter config --enable-windows-desktop

flutter create .

flutter run -d windows

我也尝试使用开发频道:

flutter channel dev

flutter upgrade

flutter config--enable-windows-uwp-desktop

这些是我在 运行 flutter devicesflutter doctor

时所拥有的

flutter 设备

4 connected devices: Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.22000.318] Windows (UWP) (desktop) • winuwp • windows-uwp-x64 • Chrome (web) • chrome • web-javascript • Google Chrome 94.0.4606.81 Edge (web) • edge • web-javascript • Microsoft Edge 95.0.1020.44

扑博士

[√] Flutter (Channel dev, 2.6.0-11.0.pre, on Microsoft Windows [Version 10.0.22000.318], locale en-US)

[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)

[√] Chrome - develop for the web

[√] Visual Studio - develop for Windows (Visual Studio Community 2022 17.0.0)

[√] Android Studio (version 2020.3)

[√] VS Code (version 1.61.1)

[√] Connected device (4 available)

• No issues found!

2022 年 2 月 3 日更新:Flutter 的最新版本 2.10 稳定版已修复此问题以及对 Windows.[=20= 的稳定桌面支持]

更新:flutter beta 通道(2.9.0-0.1.pre 之后)带有修复。

根据this issue on the main flutter repo, Flutter prior to version 2.9 does not support Visual Studio 2022. If you want to build while targeting Windows you'll either have to install VS 2019 alongside 2022 or use this workaround

The current workaround is : download your appropriate flutter version, edit _cmakeVisualStudioGeneratorIdentifier in https://github.com/flutter/flutter/blob/master/packages/flutter_tools/lib/src/windows/build_windows.dart#L25-L28 to your appropriate CMake Visual Studio Generator. You can get the currently available CMake Visual Studio Generators on this page : https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html#id13.

By default the _cmakeVisualStudioGeneratorIdentifier comes with CMake Visual Studio 2019 Generator. If you are going to use CMake Visual Studio 2022 Generator - firstly ensure your visual studio 2022 distribution contains Cmake 3.21 or later, refer https://cmake.org/cmake/help/latest/generator/Visual%20Studio%2017%202022.html for details.

要在 Windows 上应用这些更改:

  • 编辑 path\to\flutter\packages\flutter_tools\lib\src\windows\build_windows.dart,将第 28 行的常量从 'Visual Studio 16 2019' 更改为 'Visual Studio 17 2022'
  • 删除path\to\flutter\bin\cache\中的flutter_tools.stampflutter_tools.snapshot(让flutter用你上面改的新源代码重新生成它的构建工具)
  • 在项目路径中,运行 flutter clean 删除引用错误 Visual Studio 版本
  • 的缓存 CMake 文件
  • 在项目路径中,运行flutter run -d windows重新开始构建(成功,这次)

转到 flutter/packages/flutter_tools/lib/src/windows 并打开 build_windows.dart 文件并在第 24 行更改:

 const int kCurrentUwpTemplateVersion = Visual Studio 17 2022;

保存文件并运行颤振。 Select windows 设备。现在 运行 没问题了。

  1. 下载Visual Studio 17 2022
  2. 从项目中删除CMakeLists.txt(路径:your_prject_name/windows/CMakeLists.txt)
  3. 运行 在终端 flutter clean
  4. 然后运行flutter create .

我终于找到了解决办法 flutter 正在 project_folder\build\windows\runner\Debug\data\app.so 中寻找 app.so 文件; 但 app.so 文件实际上在 project_folder\build\windows\runner
要解决此问题,只需将 app.so 文件从 project_folder\build\windows\runner\ 移动到 project_folder\build\windows\runner\Debug\data\ 即可

就我而言,只有 运行 flutter clean && flutter pub 解决了问题..