关联文章

  1. 在 Windows 中同时安装多个 JDK

  2. 使用 Android SDK 命令行工具搭建 Flutter 开发环境

安装 Flutter SDK

Flutter 到现在为止, 还处于高速发展的初始阶段, Flutter SDK 的各个大版本之间, 存在一些不兼容的情况, 所以建议选择使用 stable 发行通道的 Flutter SDK 最新版本。

下载 Flutter SDK

Flutter SDK 下载地址:

解压 Flutter SDK

将压缩包解压, 然后把其中的 flutter 目录整个放在你想放置 Flutter SDK 的路径中(例如 D:\AppData, 解压后路径为: D:\AppData\flutter), 如下图所示:

(直接将压缩包 `解压到当前位置`)

请勿将 Flutter特殊字符空格的路径下。

请勿将 Flutter 安装在需要高权限的文件夹内,例如 C:\Program Files\, 电脑只有单个硬盘的可以考虑解压到C:\Users\<用户名>\AppData\Local\ 文件夹下。

Flutter 设定镜像配置

在国内使用 Flutter, 需要找一个与官方同步的可信的镜像站点, 让 Flutter 命令行工具到该镜像站点下载其所需的资源。为此设置两个环境变量:PUB_HOSTED_URLFLUTTER_STORAGE_BASE_URL, 然后再运行 Flutter 命令行工具。

可以使用 setx 命令添加 系统环境变量, 命令格式为: setx "<环境变量名>" "<环境变量对应的完整路径>"

使用 管理员权限 运行 CMD窗口PowerShell窗口, 执行以下命令:

1
2
setx "PUB_HOSTED_URL" "https://pub.flutter-io.cn"  /m
setx "FLUTTER_STORAGE_BASE_URL" "https://storage.flutter-io.cn" /m

setx 命令用于添加环境变量, 结尾的 /m 选项表示添加为 系统环境变量, 没有该选项则添加为 用户环境变量

运行成功后, 系统变量 中将添加 PUB_HOSTED_URLFLUTTER_STORAGE_BASE_URL 两个环境变量, 如下图所示:

更新 path 环境变量

Windows 控制台中运行 Flutter 命令,需要将 Flutter 的运行文件路径加入到 PATH 环境变量。

  1. 打开 环境变量 设置窗口

  2. 选中 用户变量系统变量 的 Path 变量名, 加入 flutter\bin 目录的完整路径(此处完整路径为: D:\AppData\flutter\bin), 如下图所示:

运行开发环境检测程序

更新 Path 环境变量后, 打开一个新的控制台窗口, 执行 flutter doctor(可以添加参数 -v显示详细信息) 命令检测当前的 Flutter 运行环境, 第一次运行过程如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
PS D:\AppData> flutter doctor -v

╔════════════════════════════════════════════════════════════════════════════╗
║ Welcome to Flutter! - https://flutter.dev ║
║ ║
║ The Flutter tool uses Google Analytics to anonymously report feature usage ║
║ statistics and basic crash reports. This data is used to help improve ║
║ Flutter tools over time. ║
║ ║
║ Flutter tool analytics are not sent on the very first run. To disable
║ reporting, type 'flutter config --no-analytics'. To display the current ║
║ setting, type 'flutter config'. If you opt out of analytics, an opt-out ║
║ event will be sent, and then no further information will be sent by the ║
║ Flutter tool. ║
║ ║
║ By downloading the Flutter SDK, you agree to the Google Terms of Service. ║
║ Note: The Google Privacy Policy describes how data is handled in this ║
║ service. ║
║ ║
║ Moreover, Flutter includes the Dart SDK, which may send usage metrics and ║
║ crash reports to Google. ║
║ ║
║ Read about data we send with crash reports: ║
║ https://flutter.dev/docs/reference/crash-reporting ║
║ ║
║ See Google s privacy policy: ║
║ https://policies.google.com/privacy ║
╚════════════════════════════════════════════════════════════════════════════╝


Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
Running "flutter pub get" in flutter_tools...
Resolving dependencies in flutter\packages\flutter_tools...
* _fe_analyzer_shared 50.0.0 (was 50.0.0) (52.0.0 available)
* analyzer 5.2.0 (was 5.2.0) (5.4.0 available)
* archive 3.3.2 (was 3.3.2) (3.3.6 available)
* args 2.3.1 (was 2.3.1) (2.3.2 available)
* async 2.10.0 (was 2.10.0)
* boolean_selector 2.1.1 (was 2.1.1)
* browser_launcher 1.1.1 (was 1.1.1)
* built_collection 5.1.1 (was 5.1.1)
* built_value 8.4.2 (was 8.4.2) (8.4.3 available)
* checked_yaml 2.0.1 (was 2.0.1) (2.0.2 available)
* clock 1.1.1 (was 1.1.1)
* collection 1.17.0 (was 1.17.0) (1.17.1 available)
* completion 1.0.0 (was 1.0.0) (1.0.1 available)
* convert 3.1.1 (was 3.1.1)
* coverage 1.6.1 (was 1.6.1) (1.6.2 available)
* crypto 3.0.2 (was 3.0.2)
* csslib 0.17.2 (was 0.17.2)
* dds 2.5.0 (was 2.5.0) (2.7.4 available)
* dds_service_extensions 1.3.1 (was 1.3.1) (1.3.2 available)
* devtools_shared 2.18.0 (was 2.18.0) (2.20.1 available)
* dwds 16.0.2 (was 16.0.2) (17.0.0 available)
* fake_async 1.3.1 (was 1.3.1)
* file 6.1.4 (was 6.1.4)
* file_testing 3.0.0 (was 3.0.0)
* fixnum 1.0.1 (was 1.0.1) (1.1.0 available)
* flutter_template_images 4.2.0 (was 4.2.0)
* frontend_server_client 3.1.0 (was 3.1.0) (3.2.0 available)
* glob 2.1.1 (was 2.1.1)
* html 0.15.1 (was 0.15.1)
* http 0.13.5 (was 0.13.5)
* http_multi_server 3.2.1 (was 3.2.1)
* http_parser 4.0.2 (was 4.0.2)
* intl 0.17.0 (was 0.17.0) (0.18.0 available)
* io 1.0.3 (was 1.0.3) (1.0.4 available)
* js 0.6.5 (was 0.6.5) (0.6.7 available)
* json_annotation 4.7.0 (was 4.7.0) (4.8.0 available)
* json_rpc_2 3.0.2 (was 3.0.2)
* logging 1.1.0 (was 1.1.0) (1.1.1 available)
* matcher 0.12.13 (was 0.12.13) (0.12.14 available)
* meta 1.8.0 (was 1.8.0) (1.9.0 available)
* mime 1.0.2 (was 1.0.2) (1.0.4 available)
* multicast_dns 0.3.2+2 (was 0.3.2+2) (0.3.2+3 available)
* mustache_template 2.0.0 (was 2.0.0)
* native_stack_traces 0.5.2 (was 0.5.2) (0.5.5 available)
* node_preamble 2.0.1 (was 2.0.1)
* package_config 2.1.0 (was 2.1.0)
* path 1.8.2 (was 1.8.2) (1.8.3 available)
* petitparser 5.1.0 (was 5.1.0)
* platform 3.1.0 (was 3.1.0)
* pool 1.5.1 (was 1.5.1)
* process 4.2.4 (was 4.2.4)
* pub_semver 2.1.3 (was 2.1.3)
* pubspec_parse 1.2.1 (was 1.2.1)
* shelf 1.4.0 (was 1.4.0)
* shelf_packages_handler 3.0.1 (was 3.0.1)
* shelf_proxy 1.0.2 (was 1.0.2)
* shelf_static 1.1.1 (was 1.1.1)
* shelf_web_socket 1.0.3 (was 1.0.3)
* source_map_stack_trace 2.1.1 (was 2.1.1)
* source_maps 0.10.11 (was 0.10.11)
* source_span 1.9.1 (was 1.9.1)
* sse 4.1.1 (was 4.1.1) (4.1.2 available)
* stack_trace 1.11.0 (was 1.11.0)
* stream_channel 2.1.1 (was 2.1.1)
* string_scanner 1.2.0 (was 1.2.0)
* sync_http 0.3.1 (was 0.3.1)
* term_glyph 1.2.1 (was 1.2.1)
* test 1.22.0 (was 1.22.0) (1.22.2 available)
* test_api 0.4.16 (was 0.4.16) (0.4.18 available)
* test_core 0.4.20 (was 0.4.20) (0.4.22 available)
* typed_data 1.3.1 (was 1.3.1)
* usage 4.1.0 (was 4.1.0)
* uuid 3.0.7 (was 3.0.7)
* vm_service 9.4.0 (was 9.4.0) (10.1.2 available)
* vm_snapshot_analysis 0.7.2 (was 0.7.2)
* watcher 1.0.2 (was 1.0.2)
* web_socket_channel 2.2.0 (was 2.2.0) (2.3.0 available)
* webdriver 3.0.1 (was 3.0.1) (3.0.2 available)
* webkit_inspection_protocol 1.2.0 (was 1.2.0)
* xml 6.2.2 (was 6.2.2)
* yaml 3.1.1 (was 3.1.1)
Changed 81 dependencies in flutter\packages\flutter_tools!
[✓] Flutter (Channel stable, 3.19.0, on Microsoft Windows [版本 10.0.22631.3155], locale zh-CN)
• Flutter version 3.19.0 on channel stable at D:\AppData\flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision bae5e49bc2 (2 days ago), 2024-02-13 17:46:18 -0800
• Engine revision 04817c99c9
• Dart version 3.3.0
• DevTools version 2.31.1

[✗] Windows Version (Unable to confirm if installed Windows version is 10 or greater)

[✗] Android toolchain - develop for Android devices
✗ Unable to locate Android SDK.
Install Android Studio from: https://developer.android.com/studio/index.html
On first launch it will assist you in installing the Android SDK components.
(or visit https://flutter.dev/docs/get-started/install/windows#android-setup for detailed instructions).
If the Android SDK has been installed to a custom location, please use
`flutter config --android-sdk` to update to that location.


[✗] Chrome - develop for the web (Cannot find Chrome executable at .\Google\Chrome\Application\chrome.exe)
! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.

[✗] Visual Studio - develop for Windows
✗ Visual Studio not installed; this is necessary for Windows development.
Download at https://visualstudio.microsoft.com/downloads/.
Please install the "Desktop development with C++" workload, including all of its default components

[!] Android Studio (not installed)
• Android Studio not found; download from https://developer.android.com/studio/index.html
(or visit https://flutter.dev/docs/get-started/install/windows#android-setup for detailed instructions).

[✓] VS Code, 64-bit edition (version 1.74.3)
• VS Code at C:\Program Files\Microsoft VS Code
• Flutter extension can be installed from:
🔨 https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[✓] Connected device (2 available)
• Windows (desktop) • windows • windows-x64 • Microsoft Windows [版本 10.0.22621.1194]
• Edge (web) • edge • web-javascript • Microsoft Edge 109.0.1518.70

[✓] HTTP Host Availability
• All required HTTP hosts are available

! Doctor found issues in 5 categories.

显示出来的检查结果中:

  • 绿色的 [√] 表示该项通过检测, 没有问题

  • 黄色的 [!] 表示该项存在部分问题

  • 红色的 [✗] 表示该项找不到或有严重错误

检测结果的具体说明见下图:

创建新项目

上面显示的运行环境检测结果表明 Flutter SDK 已经安装, 已连接设备中的 • Edge (web) • edge • web-javascript • Microsoft Edge 109.0.1518.70 表示使用Flutter 创建的程序已经可以通过 Edge浏览器 进行 Web平台的运行调试。

  1. 在电脑添加一个存放 Flutter 项目的文件夹(例如 D:\sources\flutter_repos), 进入该文件夹, 打开一个新的控制台窗口, 执行命令创建新项目: flutter create <项目名称>, 执行过程如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
PS D:\sources\flutter_repos> flutter create demo_app
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
Creating project demo_app...
Running "flutter pub get" in demo_app...
Resolving dependencies in demo_app...
+ async 2.10.0
+ boolean_selector 2.1.1
+ characters 1.2.1
+ clock 1.1.1
+ collection 1.17.0 (1.17.1 available)
+ cupertino_icons 1.0.5
+ fake_async 1.3.1
+ flutter 0.0.0 from sdk flutter
+ flutter_lints 2.0.1
+ flutter_test 0.0.0 from sdk flutter
+ js 0.6.5 (0.6.7 available)
+ lints 2.0.1
+ matcher 0.12.13 (0.12.14 available)
+ material_color_utilities 0.2.0
+ meta 1.8.0 (1.9.0 available)
+ path 1.8.2 (1.8.3 available)
+ sky_engine 0.0.99 from sdk flutter
+ source_span 1.9.1
+ stack_trace 1.11.0
+ stream_channel 2.1.1
+ string_scanner 1.2.0
+ term_glyph 1.2.1
+ test_api 0.4.16 (0.4.18 available)
+ vector_math 2.1.4
Changed 24 dependencies in demo_app!
Wrote 127 files.

All done!
You can find general documentation for Flutter at: https://docs.flutter.dev/
Detailed API documentation is available at: https://api.flutter.dev/
If you prefer video documentation, consider: https://www.youtube.com/c/flutterdev

In order to run your application, type:

$ cd demo_app
$ flutter run

Your application code is in demo_app\lib\main.dart.

多个单词组成的 Flutter项目名称使用下划线 _ 进行连接, 不能使用短横线 -

Web平台 运行项目

  1. 根据创建项目成功后的提示, 在控制台窗口输入命令:cd demo_app, 进入该项目文件夹, 过程如下所示:
1
2
PS D:\sources\flutter_repos> cd .\demo_app\
PS D:\sources\flutter_repos\demo_app>
  1. 使用命令运行 Flutter 项目: flutter run,
1
2
3
4
5
6
7
8
PS D:\sources\flutter_repos\demo_app> flutter run
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
Multiple devices found:
Windows (desktop) • windows • windows-x64 • Microsoft Windows [版本 10.0.22621.1194]
Edge (web) • edge • web-javascript • Microsoft Edge 109.0.1518.70
[1]: Windows (windows)
[2]: Edge (edge)
Please choose one (To quit, press "q/Q"):

此处提示选择运行项目的设备, 输入序号 2, 回车

此处并没有安装 Windows 开发工具链, 输入序号 1 时会出错。

运行成功后, 会自动打开 Edge 浏览器, 运行结果如下图所示:

配置 Android toolchain ( Android 开发工具链)

Android toolchain 用于生成 Android 平台的应用程序, 需要安装的软件包括: JDK, Android SDK, Android 模拟器 三部分。如果原来已经安装有 Android 的开发环境(Android Studio模拟器), 只需要添加需要的 Android SDK 组件即可。

安装 JDK

新版本的 Flutter SDK 进行 Android应用开发 时需要 JDK 11以上版本, 如果还没有安装过 JDK, 或者版本不符, 可以使用 Android Studio 2020.3.1(或更新版本) 自带的 OpenJDK 11, 刚好合适。

如果同时还在做 Spring Boot 或是相关开发, 有些项目可能需要使用 JDK 1.8 , 这种情况可以参考文章: 在 Windows 中同时安装多个 JDK

安装配置 Android SDK

如果电脑原来已经安装有 Android Studio, 可以打开 SDK Manager, 添加 Flutter SDK 3.19.x 需要安装的以下几个 Android SDK 组件:

  1. Android SDK Command-line Tools(latest)

  2. Android SDK Build-Tools 30.0.3 (revision: 30.0.3)

  3. Android SDK Platform 34 (revision: 2)

Flutter 3.19.0 或更高版本需要 Android SDK Platform 34 (revision: 2), Flutter 3.16.x 需要安装 Android SDK Platform 33 (revision: 2)

不同版本的 Flutter SDK 需要的 Android SDK Build-ToolsAndroid SDK Platform 版本号会有不同

确保 Android SDK Command-line Tools(latest) 已经安装, 第一次运行时其他两个组件缺失或版本不正确时, 将会自动安装

如果电脑还没有并且不想安装 Android Studio, 也可以参考文章: 使用 Android SDK 命令行工具搭建 Flutter 开发环境, 通过 Android SDK Command-line Tools(latest) 安装单独的 Android SDK

  • 速度最快的 Android SDK 安装办法: 把其他已经能运行 Flutter 项目的电脑中的 Android SDK 文件夹复制到自己的电脑。

添加ANDROID_HOME 环境变量

Android SDK 的路径添加到环境变量中, 使用 管理员权限 运行 CMD窗口PowerShell窗口, 执行以下命令:

1
setx "ANDROID_HOME" "D:\AppData\Android\Sdk"  /m

D:\AppData\Android\Sdk 替换为自己电脑实际的 Android SDK 路径

同意 Android 协议

确保电脑已经正确安装了 Android SDK, 并添加了 ANDROID_HOME 环境变量, 打开一个新的控制台窗口, 再次运行 flutter doctor -v 命令检测运行环境, 运行结果如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
PS D:\sources\flutter_repos> flutter doctor -v
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
[✓] Flutter (Channel stable, 3.19.0, on Microsoft Windows [版本 10.0.22631.3155], locale zh-CN)
• Flutter version 3.19.0 on channel stable at D:\AppData\flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision bae5e49bc2 (2 days ago), 2024-02-13 17:46:18 -0800
• Engine revision 04817c99c9
• Dart version 3.3.0
• DevTools version 2.31.1

[✗] Windows Version (Unable to confirm if installed Windows version is 10 or greater)

[!] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
• Android SDK at D:\AppData\Android\Sdk
• Platform android-34, build-tools 30.0.3
• ANDROID_HOME = D:\AppData\Android\Sdk
• Java binary at: D:\AppData\openjdk17.0.2\bin\java
• Java version OpenJDK Runtime Environment (build 17.0.2+8-86)
! Some Android licenses not accepted. To resolve this, run: flutter doctor --android-licenses

[✗] Chrome - develop for the web (Cannot find Chrome executable at .\Google\Chrome\Application\chrome.exe)
! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.

[✗] Visual Studio - develop for Windows
✗ Visual Studio not installed; this is necessary for Windows development.
Download at https://visualstudio.microsoft.com/downloads/.
Please install the "Desktop development with C++" workload, including all of its default components

[!] Android Studio (not installed)
• Android Studio not found; download from https://developer.android.com/studio/index.html
(or visit https://flutter.dev/docs/get-started/install/windows#android-setup for detailed instructions).

[✓] VS Code, 64-bit edition (version 1.74.3)
• VS Code at C:\Program Files\Microsoft VS Code
• Flutter extension can be installed from:
🔨 https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[✓] Connected device (2 available)
• Windows (desktop) • windows • windows-x64 • Microsoft Windows [版本 10.0.22621.1194]
• Edge (web) • edge • web-javascript • Microsoft Edge 109.0.1518.70

[✓] HTTP Host Availability
• All required HTTP hosts are available

! Doctor found issues in 5 categories.

之前显示为红色 [✗]Android toolchain 现在已经变成了黄色的 [!]

1
2
3
4
5
6
7
8
[!] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
• Android SDK at D:\AppData\Android\Sdk
• Platform android-34, build-tools 30.0.3
• ANDROID_HOME = D:\AppData\Android\Sdk
• Java binary at: D:\AppData\openjdk17.0.2\bin\java
• Java version OpenJDK Runtime Environment (build 17.0.2+8-86)
! Some Android licenses not accepted. To resolve this, run: flutter doctor --android-licenses

该项最后一行提示, 需要运行命令并同意 Android 的协议, 在控制台窗口输入命令: flutter doctor --android-licenses, 全部提问回答: y, 命令执行过程如下:

1
2
3
4
5
6
7
8
9
PS D:\sources\flutter_repos> flutter doctor --android-licenses
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
[=======================================] 100% Computing updates...
6 of 7 SDK package licenses not accepted.
Review licenses that have not been accepted (y/N)? y

1/6: License android-googletv-license:

<总共`6项`协议,内容很长, 要输入`6`次 `y`, 过程省略>

同意协议后再次检测Flutter运行环境

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
PS D:\sources\flutter_repos> flutter doctor -v
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
[✓] Flutter (Channel stable, 3.19.0, on Microsoft Windows [版本 10.0.22631.3155], locale zh-CN)
• Flutter version 3.19.0 on channel stable at D:\AppData\flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision bae5e49bc2 (2 days ago), 2024-02-13 17:46:18 -0800
• Engine revision 04817c99c9
• Dart version 3.3.0
• DevTools version 2.31.1

[✗] Windows Version (Unable to confirm if installed Windows version is 10 or greater)

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
• Android SDK at D:\AppData\Android\Sdk
• Platform android-34, build-tools 30.0.3
• ANDROID_HOME = D:\AppData\Android\Sdk
• Java binary at: D:\AppData\openjdk17.0.2\bin\java
• Java version OpenJDK Runtime Environment (build 17.0.2+8-86)
• All Android licenses accepted.

检测结果显示 Android toolchain 已经没有问题。

使用模拟器

Flutter 项目 运行时使用的 Android 模拟器Android 项目 开发时是一样的, 可以使用 Android Studio 自带的模拟器, 也可以使用其他第三方的模拟器。Android Studio 自带模拟器的安装和使用在此不再做说明。这里选择的第三方模拟器是 雷电模拟器夜神模拟器, 其他的第三方模拟器使用方法类似。

因为不使用 Android Studio 进行 Flutter 项目开发, 启动 Android Studio 自带的模拟器很不方便, 所以选择使用其他第三方的Android 模拟器

第三方的Android 模拟器运行时需要生成镜像文件, 占用较大的硬盘空间, 安装时请根据电脑选择 合适的安装路径

(推荐)安装 雷电模拟器

雷电模拟器 下载地址: https://www.ldmnq.com/, 下载后运行安装程序, 可以根据需要点击 d自定义安装, 更改安装的路径, 雷电模拟器安装后即可使用, 不使用额外的设置与配置。

使用 夜神模拟器

夜神模拟器下载地址: https://www.yeshen.com/, 下载后运行安装程序, 可以根据需要点击右下角自定义安装, 更改安装的路径, 如下图所示:

这里的夜神模拟器安装路径改成了: C:\Program Files\Nox

替换 夜神模拟器nox_adb.exe

夜神模拟器 通过 nox_adb.exeAndroid 开发工具进行连接, 连接时, 要求 nox_adb.exe 必须与 Android SDKadb.exe 版本相同。

  1. 打开 Android SDKplatform-tools 文件夹(此处为 D:\AppData\Android\Sdk\platform-tools)

  2. 将该文件夹中的 adb.exe 复制一份, 并命名为 nox_adb.exe (此处建议显示文件的扩展名, 以免文件命名错误)

  3. 将上一步的 nox_adb.exe 复制到 夜神模拟器 安装路径中的bin文件夹(此处为: C:\Program Files\Nox\bin)中, 覆盖掉同名的nox_adb.exe 文件。

连接 夜神模拟器

  1. 先启动 夜神模拟器, 然后在 夜神模拟器 安装路径中的bin文件夹(C:\Program Files\Nox\bin)打开一个新的控制台窗口

  2. 使用命令显示当前可连接设备: nox_adb.exe devices, 执行过程如下:

1
2
3
PS C:\Program Files\Nox\bin> .\nox_adb.exe devices
List of devices attached
127.0.0.1:62001 device

127.0.0.1:62001 为当前运行中的模拟器的连接地址

  1. 使用命令连接模拟器: nox_adb.exe connect <设备地址>, 执行过程如下所示:
1
2
PS C:\Program Files\Nox\bin> .\nox_adb.exe connect 127.0.0.1:62001
already connected to 127.0.0.1:62001

夜神模拟器Widnows 10/11 自带的 Hyper-V 冲突, 不能同时使用。

夜神模拟器 连接成功后, 同样适用于 Android 项目的开发调试。

在模拟器中运行Flutter项目

在之前已经创建好的 Flutter 项目文件夹中打开控制台窗口, 执行 flutter run 命令运行项目, 执行过程如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
PS D:\sources\flutter_repos\demo_app> flutter run
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
Launching lib\main.dart on SM G955N in debug mode...
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
Running Gradle task 'assembleDebug'... 736.7s
✓ Built build\app\outputs\flutter-apk\app-debug.apk.
Installing build\app\outputs\flutter-apk\app-debug.apk... 4.2s
Syncing files to device SM G955N... 111ms

Flutter run key commands.
r Hot reload. 🔥🔥🔥
R Hot restart.
h List all available interactive commands.
d Detach (terminate "flutter run" but leave application running).
c Clear the screen
q Quit (terminate the application on the device).

💪 Running with sound null safety 💪

An Observatory debugger and profiler on SM G955N is available at: http://127.0.0.1:51335/UEbv3xKoFAk=/
E/libEGL ( 3429): called unimplemented OpenGL ES API
The Flutter DevTools debugger and profiler on SM G955N is available at:
http://127.0.0.1:9100?uri=http://127.0.0.1:51335/UEbv3xKoFAk=/

当前已连接设备中包含有 Android设备 时, 该设备会优先运行, 此时不需要进行运行目标设备的选择。

第一次在 Android设备 上运行 Flutter 项目时, 可能会需要很长的时间, 如果没有出现错误信息, 请耐心等待

夜神模拟器显示的运行结果如下图所示:

第一次运行常见的错误

Flutter 使用 Gradle 构建 Android 项目, 第一次在 Android 模拟器上运行 Flutter 项目时, 需要下载对应版本的 Gradle。由于Gradle 的服务器在国外, 所以经常会出现无法连接服务器, 项目构建失败, 第一次常见的错误可能如下图所示:

错误原因: 可能是因为无法下载构建项目所需要的 Gradle 文件

解决办法:

  1. 打开 C:\Users\<用户名>\.gradle\wrapper\dists 文件夹(<用户名>替换为当前电脑的实际用户名)
  1. 查看当前 Flutter 项目需要的 Gradle版本
  1. 使用国内镜像下载合适版本的 Gradle, 下载地址:https://mirrors.cloud.tencent.com/gradle/
  1. 将下载到的 .zip 文件夹放到最终文件夹中, 例如: C:\Users\<用户名>\.gradle\wrapper\dists\gradle-7.5-all\6qsw290k5lz422uaf8jf6m7co, 每个不同版本Gradle 文件夹路径中的特殊字符(例如6qsw290k5lz422uaf8jf6m7co)都不一样。
  1. 重新启动 Flutter 项目调试

其他错误

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
Launching lib\main.dart on TAS AL00 in debug mode...

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':gradle:compileGroovy'.
> Could not resolve all files for configuration ':gradle:compileClasspath'.
> Could not resolve com.squareup:javawriter:2.5.0.
Required by:
project :gradle > com.android.tools.build:gradle:7.3.0 > com.android.tools.build:builder:7.3.0
> Could not resolve com.squareup:javawriter:2.5.0.
> Could not get resource 'https://repo.maven.apache.org/maven2/com/squareup/javawriter/2.5.0/javawriter-2.5.0.pom'.
> Could not GET 'https://repo.maven.apache.org/maven2/com/squareup/javawriter/2.5.0/javawriter-2.5.0.pom'.
> Connect to repo.maven.apache.org:443 [repo.maven.apache.org/146.75.112.215] failed: Connection timed out: no further information
> Could not resolve org.ow2.asm:asm:9.1.
Required by:
project :gradle > com.android.tools.build:gradle:7.3.0 > com.android.tools.build:gradle-api:7.3.0
> Could not resolve org.ow2.asm:asm:9.1.
> Could not get resource 'https://repo.maven.apache.org/maven2/org/ow2/asm/asm/9.1/asm-9.1.pom'.
> Could not GET 'https://repo.maven.apache.org/maven2/org/ow2/asm/asm/9.1/asm-9.1.pom'.
> Connect to repo.maven.apache.org:443 [repo.maven.apache.org/146.75.112.215] failed: Connection timed out: no further information

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 3m 17s
[!] Gradle threw an error while downloading artifacts from the network.
Retrying Gradle Build: #1, wait time: 100ms

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':gradle:compileGroovy'.
> Could not resolve all files for configuration ':gradle:compileClasspath'.
> Could not resolve com.squareup:javawriter:2.5.0.
Required by:
project :gradle > com.android.tools.build:gradle:7.3.0 > com.android.tools.build:builder:7.3.0
> Could not resolve com.squareup:javawriter:2.5.0.
> Could not get resource 'https://repo.maven.apache.org/maven2/com/squareup/javawriter/2.5.0/javawriter-2.5.0.pom'.
> Could not GET 'https://repo.maven.apache.org/maven2/com/squareup/javawriter/2.5.0/javawriter-2.5.0.pom'.
> Connect to repo.maven.apache.org:443 [repo.maven.apache.org/151.101.108.215] failed: Connection timed out: no further information
> Could not resolve org.ow2.asm:asm:9.1.
Required by:
project :gradle > com.android.tools.build:gradle:7.3.0 > com.android.tools.build:gradle-api:7.3.0
> Could not resolve org.ow2.asm:asm:9.1.
> Could not get resource 'https://repo.maven.apache.org/maven2/org/ow2/asm/asm/9.1/asm-9.1.pom'.
> Could not GET 'https://repo.maven.apache.org/maven2/org/ow2/asm/asm/9.1/asm-9.1.pom'.
> Connect to repo.maven.apache.org:443 [repo.maven.apache.org/151.101.108.215] failed: Connection timed out: no further information

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 2m 13s
[!] Gradle threw an error while downloading artifacts from the network.
Exception: Gradle task assembleDebug failed with exit code 1

Exited (1).

错误原因: Gradle 因为网络问题无法从服务器下载构建项目所需要的 jar

解决办法: 有条件的话 更换网络, 或 搭个梯子, 或是配置 Gradle 国内镜像, 并重试直到成功(或者换个其他人少上网的时间重试)

可以尝试将其他人已经能成功运行 Flutter 项目的 C:\Users\<用户名>\.gradle 文件夹复制到自己电脑。

配置 Gradle 国内镜像

Android设备 上运行 Flutter项目程序时, 默认使用 Gradle 管理项目依赖, 第一次运行时, 除了检查 Android SDK 组件并下载正确的版本外, 还会安装和 Flutter SDK 匹配的 Gradle, Flutter SDK 3.7.1 使用的 Gradle 版本是 gradle-7.5-all

Gradle 添加成功后才会下载当前项目所需要的其他包和组件, 由于 Gradle 默认使用的源是国外的服务器, 所以网络状况不好时通常出现类似以下的错误信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
PS D:\sources\flutter_repos\demo_app> flutter run
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
Using hardware rendering with device Android SDK built for x86 64. If you notice graphics artifacts, consider enabling software rendering
with "--enable-software-rendering".
Launching lib/main.dart on Android SDK built for x86 64 in debug mode...

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugKotlin'.
> Could not resolve all dependencies for configuration ':app:kotlinCompilerClasspath'.
> Could not load module metadata from /home/laohoo/.gradle/caches/modules-2/metadata-2.97/descriptors/org.jetbrains.kotlin/kotlin-script-runtime/1.6.10/a8be1fe3b3911d3d3425fe720cf42835/descriptor.bin

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 1s
Running Gradle task 'assembleDebug'... 2,548ms
Exception: Gradle task assembleDebug failed with exit code 1

解决办法

C:\Users\<用户名>\.gradle 文件夹(<用户名>替换为当前电脑实际的用户名)下新建名为 init.gradle 的文件, 文件内容编辑如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
allprojects{
repositories {
def ALIYUN_REPOSITORY_URL = 'https://maven.aliyun.com/repository/public/'
def ALIYUN_JCENTER_URL = 'https://maven.aliyun.com/repository/jcenter/'
def ALIYUN_GOOGLE_URL = 'https://maven.aliyun.com/repository/google/'
def ALIYUN_GRADLE_PLUGIN_URL = 'https://maven.aliyun.com/repository/gradle-plugin/'
all { ArtifactRepository repo ->
if(repo instanceof MavenArtifactRepository){
def url = repo.url.toString()
if (url.startsWith('https://repo1.maven.org/maven2/')) {
project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPOSITORY_URL."
remove repo
}
if (url.startsWith('https://jcenter.bintray.com/')) {
project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_JCENTER_URL."
remove repo
}
if (url.startsWith('https://dl.google.com/dl/android/maven2/')) {
project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_GOOGLE_URL."
remove repo
}
if (url.startsWith('https://plugins.gradle.org/m2/')) {
project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_GRADLE_PLUGIN_URL."
remove repo
}
}
}
maven { url ALIYUN_REPOSITORY_URL }
maven { url ALIYUN_JCENTER_URL }
maven { url ALIYUN_GOOGLE_URL }
maven { url ALIYUN_GRADLE_PLUGIN_URL }
}
buildscript{
repositories {
def ALIYUN_REPOSITORY_URL = 'https://maven.aliyun.com/repository/public/'
def ALIYUN_JCENTER_URL = 'https://maven.aliyun.com/repository/jcenter/'
def ALIYUN_GOOGLE_URL = 'https://maven.aliyun.com/repository/google/'
def ALIYUN_GRADLE_PLUGIN_URL = 'https://maven.aliyun.com/repository/gradle-plugin/'
all { ArtifactRepository repo ->
if(repo instanceof MavenArtifactRepository){
def url = repo.url.toString()
if (url.startsWith('https://repo1.maven.org/maven2/')) {
project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPOSITORY_URL."
remove repo
}
if (url.startsWith('https://jcenter.bintray.com/')) {
project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_JCENTER_URL."
remove repo
}
if (url.startsWith('https://dl.google.com/dl/android/maven2/')) {
project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_GOOGLE_URL."
remove repo
}
if (url.startsWith('https://plugins.gradle.org/m2/')) {
project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_GRADLE_PLUGIN_URL."
remove repo
}
}
}
maven { url ALIYUN_REPOSITORY_URL }
maven { url ALIYUN_JCENTER_URL }
maven { url ALIYUN_GOOGLE_URL }
maven { url ALIYUN_GRADLE_PLUGIN_URL }
}
}
}

在第一次在 Android设备 中运行 Flutter 项目, 下载了 Gradle 之后, 才会创建 C:\Users\<用户名>\.gradle 文件夹, 该文件夹是隐藏文件夹

再次运行项目, Gradle 就可以通过国内镜像下载项目需要的依赖, 提高下载的速度。

=== END ===