开发环境:

  • Dell Inspiron I5 7559

  • Intel Core i5 6th Gen 6300HQ (2.30GHz)

  • NVIDIA GeForce GTX 960M

  • Linux Mint 21 5.15.0-48-generic, locale en_US.UTF-8

  • OpenJDK Runtime Environment (build 11.0.16+8-post-Ubuntu-0ubuntu122.04)

  • Visual Studio Code 1.72.0

  • Flutter version 3.3.4 on channel stable

  • Git 2.34.1


前段时间把用了六年的 Dell Inspiron I5 7559 处理了一下: 拆掉失效的电池; 把买电脑时自带的 128GB 固态硬盘装回去; 把500GB的机械硬盘拆下来, 放到硬盘底座上当个移动硬盘用; 然后给它安装了一个 Linux Mint 21

本来想给 NVIDIA GeForce GTX 960M 显卡安装一个 NVIDIA 驱动的, 试着安装了几个版本不成功, 安装驱动后无法进入桌面, CRTL+ALT+F1 进入 tty 也不成功, 命令界面闪了一下就不见了。显卡驱动装不上, 那就只能拿它来弄其他的吧, 于是就尝试一下在 Linux 上安装 Flutter 的开发环境。

安装 Flutter SDK

Flutter SDK (stable channel) 下载地址:https://docs.flutter.dev/development/tools/sdk/releases

  1. $HOME 新建名为 AppData 的文件夹

  2. 下载最新版的 flutter_linux_3.3.4-stable.tar.xz$HOME/AppData 文件夹

  3. flutter_linux_3.3.4-stable.tar.xz文件 解压到当前位置(Extact here), 解压后 Flutter SDK 的路径为 $HOME/AppData/flutter

  4. 添加 Flutter 相关的环境变量

执行命令:xed ~/.bashrc, 在.bashrc 文件的最末尾添加以下内容:

1
2
3
export PATH="$PATH:$HOME/AppData/flutter/bin"
export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
  1. xedLinux Mint 自带的文本编辑器, 使用其他版本的 Linux时用相应的命令替换
  2. 可以执行 source ~/.bashrc 命令让修改后的环境变量立即生效
  1. Flutter 环境检查

执行 Flutter 环境检查命令: flutter doctor -v

Linux 环境下 flutter doctor 命令需要依赖 Git, 在命令执行前要先确保电脑已经安装 Git

命令执行结果如下所示:

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
laohoo@laohoo-Inspiron-7559:~/Desktop$ flutter doctor -v
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
Running "flutter pub get" in flutter_tools... 4.9s
[✓] Flutter (Channel stable, 3.3.4, on Linux Mint 21 5.15.0-48-generic, locale en_US.UTF-8)
• Flutter version 3.3.4 on channel stable at /home/laohoo/AppData/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision eb6d86ee27 (3 days ago), 2022-10-04 22:31:45 -0700
• Engine revision c08d7d5efc
• Dart version 2.18.2
• DevTools version 2.15.0
• Pub download mirror https://pub.flutter-io.cn
• Flutter download mirror https://storage.flutter-io.cn

[✗] 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/linux#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)
! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.

[✗] Linux toolchain - develop for Linux desktop
• Ubuntu clang version 14.0.0-1ubuntu1
• cmake version 3.22.1
• ninja version 1.10.1
• pkg-config version 0.29.2
✗ GTK 3.0 development libraries are required for Linux development.
They are likely available from your distribution (e.g.: apt install libgtk-3-dev)

[!] 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/linux#android-setup for detailed instructions).

[✓] VS Code (version 1.72.0)
• VS Code at /usr/share/code
• Flutter extension version 3.50.0

[✓] Connected device (1 available)
• Linux (desktop) • linux • linux-x64 • Linux Mint 21 5.15.0-48-generic

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

! Doctor found issues in 4 categories.

检查结果显示有4类问题:

  1. [✗] Android toolchain - develop for Android devices : 用于生成和运行 Android 应用程序的开发工具链, 目前还没有安装 Android SDK

  2. [✗] Chrome - develop for the web : 生成和运行 Web 项目时需要使用 Chrome 浏览器, Linux Mint 自带的 Firefox 不认

  3. [✗] Linux toolchain - develop for Linux desktop: 用于生成和运行 Linux桌面应用程序的开发工具链

  4. [!] Android Studio (not installed): 没有安装 Android Studio, 这个不打算安装了

并不是所有的工具链都要安装, 只安装需要的目标平台的工具链就要以了。

安装 Linux 工具链(Linux toolchain - develop for Linux desktop)

Linux 工具链的安装比较简单,直接执行以下命令即可:

sudo apt-get install clang cmake ninja-build pkg-config libgtk-3-dev liblzma-dev libstdc++-12-dev

命令执行过程(部分), 如下所示:

1
2
3
4
5
6
7
8
9
laohoo@laohoo-Inspiron-7559:~/Desktop$ sudo apt-get install clang cmake ninja-build pkg-config libgtk-3-dev liblzma-dev libstdc++-12-dev
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
liblzma-dev is already the newest version (5.2.5-2ubuntu1).
pkg-config is already the newest version (0.29.2-1ubuntu3).
clang is already the newest version (1:14.0-55~exp2).
ninja-build is already the newest version (1.10.1-1).
cmake is already the newest version (3.22.1-1ubuntu1.22.04.1).

Flutter 官方网站安装指导中缺少了 libstdc++-12-dev, 在部分系统上调试项目时, 会出现

可能出现的错误

在部分Linux系统上调试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
Launching lib/main.dart on Linux in debug mode...
CMake Error at /usr/share/cmake-3.22/Modules/CMakeTestCXXCompiler.cmake:62 (message):
The C++ compiler

"/usr/bin/clang++"

is not able to compile a simple test program.

It fails with the following output:

Change Dir: /home/bul/Desktop/app_test/build/linux/x64/debug/CMakeFiles/CMakeTmp

Run Build Command(s):/usr/bin/ninja cmTC_6702a && [1/2] Building CXX object CMakeFiles/cmTC_6702a.dir/testCXXCompiler.cxx.o
[2/2] Linking CXX executable cmTC_6702a
FAILED: cmTC_6702a
: && /usr/bin/clang++ CMakeFiles/cmTC_6702a.dir/testCXXCompiler.cxx.o -o cmTC_6702a && :
/usr/bin/ld: cannot find -lstdc++: No such file or directory
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:3 (project)


Building Linux application...
Exception: Unable to generate build files

错误的原因是官方的文档少安装了一个依赖: libstdc++-12-dev, 安装上去即可(上面的Linux工具链安装命令中已包含)。

1
sudo apt install lib32stdc++-12-dev

安装 Android toolchain

Android toolchain 需要的组件包括两大块: Android SDK, Android 模拟器

使用Command-line Tools安装 Android SDK

Android SDK Command-line Tools(latest) 下载地址:https://developer.android.google.cn/studio?hl=zh-cn#command-tools

  1. 下载、解压缩 Android SDK Command-line Tools(latest)

$HOME/AppData/ 文件夹中新建名为android_sdk 的文件夹, 下载最新的 Linux 版本的 Android SDK Command-line Tools(latest)文件 commandlinetools-linux-8512546_latest.zip (如有更新的版本, 请自行替换), 解压缩到以下位置: $HOME/AppData/android_sdk/cmdline-tools/latest , latest 文件夹内包含 binlib 文件夹。

  1. 修改用户环境变量

执行命令: xed ~/.bashrc, 添加 ANDROID_HOME 环境变量 和 Android SDK Command-line Tools(latest)路径, 如下所示:

1
2
3
4
5
6
export PATH="$PATH:$HOME/AppData/flutter/bin"
export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn

export PATH="$PATH:$HOME/AppData/android_sdk/cmdline-tools/latest/bin"
export ANDROID_HOME=$HOME/AppData/android_sdk
  1. 安装 Android SDK 软件包(组件)

配置好 Android SDK Command-line Tools(latest) 路径后, 可以用sdkmanager 命令来安装 Android SDK 软件包, 安装命令格式: sdkmanager <packages>

Flutter SDK最少需要三个组件: platform-tools, build-tools, platforms, 其中 build-toolsplatforms 的版本会跟随 Flutter SDK 版本而不同。

  1. 如果不能确定当前 Flutter SDK 需要的版本号, 可以任意安装某个版本, 当第一次运行程序时, 会自动检查并安装合适的版本。
  2. Flutter SDK 3.3.4 需要的版本为: "build-tools;30.0.3""platforms;android-31"

安装命令:

1
sdkmanager "platform-tools" "build-tools;30.0.3" "platforms;android-31" 

显示已安装和可用的Android SDK 组件: sdkmanager --list

1
2
3
4
5
6
7
8
laohoo@laohoo-Inspiron-7559:~/Desktop$ sdkmanager --list
Installed packages:=====================] 100% Computing updates...
Path | Version | Description | Location
------- | ------- | ------- | -------
build-tools;30.0.3 | 30.0.3 | Android SDK Build-Tools 30.0.3 | build-tools/30.0.3
platform-tools | 33.0.3 | Android SDK Platform-Tools | platform-tools
platforms;android-31 | 1 | Android SDK Platform 31 | platforms/android-31

执行 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
laohoo@laohoo-Inspiron-7559:~/Desktop$ flutter doctor -v
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
[✓] Flutter (Channel stable, 3.3.4, on Linux Mint 21 5.15.0-48-generic, locale en_US.UTF-8)
• Flutter version 3.3.4 on channel stable at /home/laohoo/AppData/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision eb6d86ee27 (3 days ago), 2022-10-04 22:31:45 -0700
• Engine revision c08d7d5efc
• Dart version 2.18.2
• DevTools version 2.15.0
• Pub download mirror https://pub.flutter-io.cn
• Flutter download mirror https://storage.flutter-io.cn

[!] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
• Android SDK at /home/laohoo/AppData/android_sdk
• Platform android-33, build-tools 31.0.0
• ANDROID_HOME = /home/laohoo/AppData/android_sdk
• Java binary at: /usr/bin/java
• Java version OpenJDK Runtime Environment (build 11.0.16+8-post-Ubuntu-0ubuntu122.04)
! 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)
! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.

[✓] Linux toolchain - develop for Linux desktop
• Ubuntu clang version 14.0.0-1ubuntu1
• cmake version 3.22.1
• ninja version 1.10.1
• pkg-config version 0.29.2

[!] 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/linux#android-setup for detailed instructions).

[✓] VS Code (version 1.72.0)
• VS Code at /usr/share/code
• Flutter extension version 3.50.0

[✓] Connected device (1 available)
• Linux (desktop) • linux • linux-x64 • Linux Mint 21 5.15.0-48-generic

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

! Doctor found issues in 3 categories.

Android 工具链变成感叹号, 表示还没有完全解决问题

1
2
3
4
5
6
7
[!] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
• Android SDK at /home/laohoo/AppData/android_sdk
• Platform android-33, build-tools 31.0.0
• ANDROID_HOME = /home/laohoo/AppData/android_sdk
• Java binary at: /usr/bin/java
• Java version OpenJDK Runtime Environment (build 11.0.16+8-post-Ubuntu-0ubuntu122.04)
! Some Android licenses not accepted. To resolve this, run: flutter doctor --android-licenses

! Some Android licenses not accepted. To resolve this, run: flutter doctor --android-licenses: 还没有接受 Andorid 的协议, 执行命令: flutter doctor --android-licenses

接受 Android licenses, 执行命令 flutter doctor --android-licenses, 全部问题回答 yes, 如下所示(内容太长, 只显示一小部分):

1
2
3
4
laohoo@laohoo-Inspiron-7559:~/Desktop$ flutter doctor --android-licenses
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
5 of 6 SDK package licenses not accepted. 100% Computing updates...
Review licenses that have not been accepted (y/N)? y

接受协议后再次执行 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
laohoo@laohoo-Inspiron-7559:~/Desktop$ flutter doctor -v
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
[✓] Flutter (Channel stable, 3.3.4, on Linux Mint 21 5.15.0-48-generic, locale en_US.UTF-8)
• Flutter version 3.3.4 on channel stable at /home/laohoo/AppData/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision eb6d86ee27 (3 days ago), 2022-10-04 22:31:45 -0700
• Engine revision c08d7d5efc
• Dart version 2.18.2
• DevTools version 2.15.0
• Pub download mirror https://pub.flutter-io.cn
• Flutter download mirror https://storage.flutter-io.cn

[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
• Android SDK at /home/laohoo/AppData/android_sdk
• Platform android-33, build-tools 31.0.0
• ANDROID_HOME = /home/laohoo/AppData/android_sdk
• Java binary at: /usr/bin/java
• Java version OpenJDK Runtime Environment (build 11.0.16+8-post-Ubuntu-0ubuntu122.04)
• All Android licenses accepted.

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

[✓] Linux toolchain - develop for Linux desktop
• Ubuntu clang version 14.0.0-1ubuntu1
• cmake version 3.22.1
• ninja version 1.10.1
• pkg-config version 0.29.2

[!] 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/linux#android-setup for detailed instructions).

[✓] VS Code (version 1.72.0)
• VS Code at /usr/share/code
• Flutter extension version 3.50.0

[✓] Connected device (1 available)
• Linux (desktop) • linux • linux-x64 • Linux Mint 21 5.15.0-48-generic

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

! Doctor found issues in 2 categories.

安装模拟器

使用命令 sdkmanager emulator 安装模拟器, 命令执行过程如下:

1
2
laohoo@laohoo-Inspiron-7559:~/Desktop$ sdkmanager emulator
[=======================================] 100% Unzipping... emulator/bin64/tune2

安装模拟器镜像前, 可以通过 sdkmanager --list 查看当前可用的模拟器镜像, 选择合适的镜像安装。

sdkmanager安装模拟器镜像过程如下所示:

1
2
laohoo@laohoo-Inspiron-7559:~/Desktop$ sdkmanager "system-images;android-31;default;x86_64"
[=======================================] 100% Unzipping... x86_64/data/misc/mod

配置模拟器硬件加速

Linux 使用 KVM 来支持模拟器的硬件加速, 在安装使用 KVM 之前先要检查电脑是否支持虚拟化功能

安装 cpu-checker 软件包: sudo apt install cpu-checker, 命令执行过程如下:

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
laohoo@laohoo-Inspiron-7559:~/Desktop$ sudo apt install cpu-checker
[sudo] password for laohoo:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
msr-tools
The following NEW packages will be installed:
cpu-checker msr-tools
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 17.1 kB of archives.
After this operation, 67.6 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://mirrors.aliyun.com/ubuntu jammy/main amd64 msr-tools amd64 1.3-4 [10.3 kB]
Get:2 http://mirrors.aliyun.com/ubuntu jammy/main amd64 cpu-checker amd64 0.7-1.3build1 [6,800 B]
Fetched 17.1 kB in 0s (187 kB/s)
Selecting previously unselected package msr-tools.
(Reading database ... 295693 files and directories currently installed.)
Preparing to unpack .../msr-tools_1.3-4_amd64.deb ...
Unpacking msr-tools (1.3-4) ...
Selecting previously unselected package cpu-checker.
Preparing to unpack .../cpu-checker_0.7-1.3build1_amd64.deb ...
Unpacking cpu-checker (0.7-1.3build1) ...
Setting up msr-tools (1.3-4) ...
Setting up cpu-checker (0.7-1.3build1) ...
Processing triggers for man-db (2.10.2-1) ...

检查是否支持虚拟化: egrep -c '(vmx|svm)' /proc/cpuinfo, 命令执行过程如下:

1
2
laohoo@laohoo-Inspiron-7559:~/Desktop$ egrep -c '(vmx|svm)' /proc/cpuinfo
8

输出值 1 或更大值表示支持虚拟化。输出值 0 表示您的 CPU 不支持硬件虚拟化。

确定电脑是否能够运行硬件加速的 KVM 虚拟机: kvm-ok, 命令执行过程如下:

1
2
3
laohoo@laohoo-Inspiron-7559:~/Desktop$ kvm-ok
INFO: /dev/kvm exists
KVM acceleration can be used

上述结果表示当前电脑能够运行硬件加速的 KVM 虚拟机

Linux 上安装 KVM

1
sudo apt-get install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils

创建Android模拟器

通过命令 avdmanager 来创建Android模拟器, 命令格式: avdmanager create avd -n 模拟器名 -k "模拟器镜像名"

  1. 模拟器名 用于运行模拟器时调用
  2. 模拟器镜像名 为之前已经安装的镜像

命令执行过程如下:

1
2
3
laohoo@laohoo-Inspiron-7559:~/Desktop$ avdmanager create avd -n huawei -k "system-images;android-31;default;x86_64"
Auto-selecting single ABI x86_64========] 100% Fetch remote repository...
Do you wish to create a custom hardware profile? [no]
  1. 此处为方便回答 no, yes 的选项太多了, 回答了70几项还没有结束, 最关键是还看不懂.
  2. huawei 是模拟器的名字
  3. "system-images;android-31;default;x86_64" 是模拟器镜像名

启动模拟器

查看当前已经建好的模拟器: avdmanager list avd, 命令执行结果如下:

1
2
3
4
5
6
7
aohoo@laohoo-Inspiron-7559:~/Desktop$ avdmanager list avd
Available Android Virtual Devices:
Name: huawei
Path: /home/laohoo/.android/avd/huawei.avd
Target: Default Android System Image
Based on: Android 12.0 (S) Tag/ABI: default/x86_64
Sdcard: 512 MB

启动模拟器的命令位于 Android SDKemulator 文件夹下, 需要先添加该文件夹的路径到环境变量中。

修改环境变量: xed ~/.bashrc

~/.bashrc 文件末尾修改后的内容如下:

1
2
3
4
5
6
export PATH="$PATH:$HOME/AppData/flutter/bin"
export PATH="$PATH:$HOME/AppData/android_sdk/cmdline-tools/latest/bin"
export PATH="$PATH:$HOME/AppData/android_sdk/emulator"
export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
export ANDROID_HOME=$HOME/AppData/android_sdk

启动模拟器的命令: emulator -avd 模拟器名, 命令执行过程如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
laohoo@laohoo-Inspiron-7559:~/Desktop$ emulator -avd huawei
INFO | Android emulator version 31.3.11.0 (build_id 9058569) (CL:N/A)
emulator: INFO: Found systemPath /home/laohoo/AppData/android_sdk/system-images/android-31/default/x86_64/
INFO | Duplicate loglines will be removed, if you wish to see each indiviudal line launch with the -log-nofilter flag.
WARNING | unexpected system image feature string, emulator might not function correctly, please try updating the emulator.
WARNING | cannot add library /home/laohoo/AppData/android_sdk/emulator/qemu/linux-x86_64/lib64/vulkan/libvulkan.so: failed
INFO | added library /home/laohoo/AppData/android_sdk/emulator/lib64/vulkan/libvulkan.so
MESA-INTEL: warning: Performance support disabled, consider sysctl dev.i915.perf_stream_paranoid=0

INFO | configAndStartRenderer: setting vsync to 60 hz
INFO | Sending adb public key [QAAAACPLTUN1QxgyNvTke2ABrlB0HlUVCDlT/YFb/fooM9bItTYvgmZ9+lCSgqgYyQxmkF+TOiBom0UMQ8iidd9qr0yLKd+12SVI5GxZLch4gE7KY+Dm/7MRLso5k6XsMWu4wusaWXIsShzHkMh86Tx16H1OB+GqPaJA5EIjcVHiovK2mRHBz8swQ5+2xJ5fqN4oOEzCRq71se4U5ykFBLmGRSY+BGwGgfqtRAWBZFhZCzL7Jy8ten9Q1cIkOyzLOvOFAZFqdEdMmtTPjBeug1VtTUcL16wGm+PH0MsO0rOH7M7Y7WxjsETt5qXw7BnY80eAyBNCURzxXMoAtX0YBxk9NL3dAuyZmYRz+btLQ7tpYOqpO535s31I+YvQLiXW0bS23lHCBl5CTyV7COZ90BZ2tQp2N37/65nE+jplU7U4NDvA45/+uHCCeFiMR6jegepNi3YUrl9pb6ti/O2+mD3s/YP9pQ/7TokunOG9h5IXVjtdela4jKyLJer3o70PDMNFZtiebQ/8QGT/m5NGp/1wrHMyeGDCYLc5qaEexjD9QzZQyM9xOfiMK/78RBqo7FX0ihL5uMfKAQ2FFD2IQsd8FsYgIS7kzSVZuS0l/8HyBYrsgCt0mBq2A54PvPTCK/UcMdrzj2SpM5yifdEJ/PAGWtD6kwEWa9o9siYu8dBb7Zibw4mnBAEAAQA= laohoo@unknown]
INFO | injectedQemuChannel!
INFO | Informing listeners of injection.
INFO | Rootcanal has been activated.
WARNING | *** No gRPC protection active, consider launching with the -grpc-use-jwt flag.***
INFO | Started GRPC server at 127.0.0.1:8554, security: Local, auth: none
INFO | Advertising in: /run/user/1000/avd/running/pid_17579.ini
INFO | setDisplayConfigs w 320 h 640 dpiX 160 dpiY 160
INFO | Wait for emulator (pid 17579) 20 seconds to shutdown gracefully before kill;you can set environment variable ANDROID_EMULATOR_WAIT_TIME_BEFORE_KILL(in seconds) to change the default value (20 seconds)

INFO | Info: Sandboxing disabled by user. ((null):0, (null))

INFO | Closing down rootcanal.

创建 Flutter 项目

创建Flutter项目的命令格式: flutter create <项目名称>

项目名称 由多个单词组成时, 单词之间用下划线_分隔, 不能使用短横线 -

创建项目的命令执行过程如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
laohoo@laohoo-Inspiron-7559:~/Sources/flutter_repos$ flutter create my_store_app
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
Creating project my_store_app...
Running "flutter pub get" in my_store_app... 1,992ms
Wrote 127 files.

All done!
In order to run your application, type:

$ cd my_store_app
$ flutter run

Your application code is in my_store_app/lib/main.dart.

运行项目

进入新创建的项目文件夹, 运行程序:

1
2
cd my_store_app
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
24
25
26
laohoo@laohoo-Inspiron-7559:~/Sources/flutter_repos/my_store_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...
Checking the license for package Android SDK Tools in /home/laohoo/AppData/android_sdk/licenses
License for package Android SDK Tools accepted.
Preparing "Install Android SDK Tools (revision: 26.1.1)".
"Install Android SDK Tools (revision: 26.1.1)" ready.
Installing Android SDK Tools in /home/laohoo/AppData/android_sdk/tools
"Install Android SDK Tools (revision: 26.1.1)" complete.
"Install Android SDK Tools (revision: 26.1.1)" finished.
Checking the license for package Android SDK Build-Tools 30.0.3 in /home/laohoo/AppData/android_sdk/licenses
License for package Android SDK Build-Tools 30.0.3 accepted.
Preparing "Install Android SDK Build-Tools 30.0.3 (revision: 30.0.3)".
"Install Android SDK Build-Tools 30.0.3 (revision: 30.0.3)" ready.
Installing Android SDK Build-Tools 30.0.3 in /home/laohoo/AppData/android_sdk/build-tools/30.0.3
"Install Android SDK Build-Tools 30.0.3 (revision: 30.0.3)" complete.
"Install Android SDK Build-Tools 30.0.3 (revision: 30.0.3)" finished.
Checking the license for package Android SDK Platform 31 in /home/laohoo/AppData/android_sdk/licenses
License for package Android SDK Platform 31 accepted.
Preparing "Install Android SDK Platform 31 (revision: 1)".
"Install Android SDK Platform 31 (revision: 1)" ready.
Installing Android SDK Platform 31 in /home/laohoo/AppData/android_sdk/platforms/android-31
"Install Android SDK Platform 31 (revision: 1)" complete.
"Install Android SDK Platform 31 (revision: 1)" finished.

此处安装的 Android SDK 组件版本不正确, 在第一次运行程序的时候, 会自动下载正确的版本

设置 Gradle 国内镜像

Gradle 导致的运行问题

flutter 默认使用 Gradle 管理项目, 第一次运行时, 除了检查 Android SDK 组件并下载正确的版本外, 还会安装和 Flutter SDK 匹配的 Gradle, Flutter SDK 3.3.4 使用的是 gradle-7.4

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
laohoo@laohoo-Inspiron-7559:~/Sources/flutter_repos/my_store_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

解决办法

$HOME/.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 }
}
}
}

在第一次运行 Flutter 项目, 下载了 Gradle 之后, 就会创建 $HOME/.gradle 文件夹, 该文件夹是隐藏文件夹。

再次运行项目, 命令执行过程如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
laohoo@laohoo-Inspiron-7559:~/Sources/flutter_repos/my_store_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...
Running Gradle task 'assembleDebug'... 93.9s
✓ Built build/app/outputs/flutter-apk/app-debug.apk.
Installing build/app/outputs/flutter-apk/app.apk... 1,034ms
Syncing files to device Android SDK built for x86 64... 211ms

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 Android SDK built for x86 64 is available at: http://127.0.0.1:42339/XnPsnajGgW8=/
The Flutter DevTools debugger and profiler on Android SDK built for x86 64 is available at: http://127.0.0.1:9101?uri=http://127.0.0.1:42339/XnPsnajGgW8=/

=== END ===