flutter⽣成源代码_Flutter源码剖析(⼀):源码获取与构建概述
本⽂介绍了Flutter源码的获取与构建,后⾯会另有⽂章介绍Flutter源码的版本管理、开发环境搭建等主题。
准备⼯作
Flutter源码分为两个部分:flutter/flutter是框架层,为开发者提供各种接⼝,主要是dart代码。
flutter/engine是引擎层,负责Flutter的渲染以及宿主的交互。
源码下载
flutter/flutter可以直接通过git下载,但是flutter/engine需要通过gclient⼯具获取,因为engine有很多依赖,gclient可以很好地处理这些依赖,简化源码管理流程。
⾸先,新建⼀个⽬录,下载flutter框架代码:$ mkdir flutter_source_code
$ cd flutter_source_code
Cloning into 'flutter'...
remote: Enumerating objects: 12, done.
remote: Counting objects: 100% (12/12), done.
remote: Compressing objects: 100% (12/12), done.
remote: Total 272396 (delta 0), reused 6 (delta 0), pack-reused 272384
Receiving objects: 100% (272396/272396), 116.98 MiB | 2.48 MiB/s, done.
Resolving deltas: 100% (210440/210440), done.
Cloning into 'depot_tools'...
remote: Sending approximately 34.14 MiB ...
remote: Total 40539 (delta 27803), reused 40539 (delta 27803)
Receiving objects: 100% (40539/40539), 34.14 MiB | 5.04 MiB/s, done.
Resolving deltas: 100% (27803/27803), done.
设置环境变量(每次构建之前都要设置,也可以写⼊系统配置):export PATH=$PATH:`pwd`/depot_tools
开始拉取代码(这⼀步⽐较耗时)$ gclient sync [18:04:43]
......
remote: Enumerating objects: 25, done.
remote: Counting objects: 100% (25/25), done.
remote: Compressing objects: 100% (22/22), done.
remote: Total 209672 (delta 10), reused 13 (delta 3), pack-reused 209647
Receiving objects: 100% (209672/209672), 196.61 MiB | 3.74 MiB/s, done.
Resolving deltas: 100% (153791/153791), done.
Syncing projects: 31% (33/104) src/third_party/vulkan
[0:03:59] Still working on:
[0:03:59] src/ios_tools
[0:03:59] src/third_party/angle
[0:03:59] src/third_party/dart
[0:03:59] src/third_party/icu
......
[0:12:48] Still working on:
[0:12:48] src/third_party/dart
Syncing projects: 100% (104/104), done.
Running hooks: 100% ( 9/ 9) dart package config
________ running 'vpython src/flutter/tools/run_third_party_dart.py' in '/Users/vimerzhao/WorkProject/flutter_source_code' (1.7s)
+ charcode 1.1.3
+ collection 1.14.13
+ meta 1.2.3
+ package_config 1.9.3
+ path 1.7.0
+ pub_semver 1.4.4
+ source_span 1.7.0
+ string_scanner 1.0.5
+ term_glyph 1.1.0
+ yaml 2.2.1
Changed 10 dependencies!
需要注意的是,Syncing projects: 100% (104/104), done之后,会继续下载⼀些⼤⽂件,可能命令⾏没有输出,⼀定不能强制退出,可以通过资源管理器查看⽹络的流量,确定cipd是否在下载:
(因为git不是很擅长下载⼤⽂件,所以产⽣了cipd这个程序来做这些⼯作)
此时的⽬录结构:$ tree -L 1
.
├── depot_tools # 源码管理⼯具
├── flutter # flutter framework⽬录
└── src # flutter engine以及相关依赖所在⽬录
framework的版本和engine的版本是⼀⼀对应的,framework的分⽀规则如下:stable是当前的稳定分⽀,⽆特殊情况,推荐开发者使⽤该分⽀作为flutter sdk
master包含最新的特性,但是不稳定
每个版本会打上对应的tag
截⽌到 2020-10-29 ,最新的较稳定版本是 1.22.0,于是我们也先切到这个版本(不是必选的,但是个⼈认为:基于⼀个明确的版本编译和修改源代码似乎更合适)。$ cd flutter
$ git checkout 1.22.0
$ cat bin/internal/engine.version
5babba6c4d25fa237bbf755ab85c9a0c50b3c6ec
engine.version这个⽂件指定了framework对应的engine版本,接下来,我们进⼊engine⽬录切换到这次commit。$ cd ../src/flutter
$ git reset --hard 5babba6c4d25fa237bbf755ab85c9a0c50b3c6ec
HEAD is now at 5babba6c4 Flutter 1.22.0-12.3.pre engine cherrypicks (#21466)
此时,我们需要执⾏以下命令:$ gclient sync --with_branch_heads --with_tags
Syncing projects: 100% (104/104), done.
......
Running hooks: 100% ( 8/ 8) dart package config
......
Running hooks: 100% (8/8), done.
后⾯这两个参数的含义⽐较晦涩,参考Chromium的说明,其含义是:Checkout all the submodules at their branch DEPS revisions
因为切换分⽀之后,某些依赖的版本可能有更改,所以需要再次sync⼀下,直接在src/flutter⽬录执⾏即可。
以上就完成了源码环境的搭建,下⾯正式开始编译。
源码编译
⾸先我们退回到src⽬录,然后通过gn⽣成ninja需要的元数据:$ cd ..
$ pwd
/Users/vimerzhao/WorkProject/flutter_source_code/src
$ ./flutter/tools/gn --unoptimized --android --runtime-mode debug --android-cpu arm
Generating GN files in: out/android_debug_unopt
Generating Xcode projects took 75ms
Done. Made 438 targets from 197 files in 1960ms
对于编译参数,Compiling the engine · flutter/flutter Wiki有详细介绍,在此不做赘述,这⾥我构建的是⼀个未优化、Android平台、debug版本、arm 32位的 engine。
此时,查看out⽬录,可以看到:$ ls out/
android_debug_unopt compile_commands.json
compile_commands.json可以作为IDE的索引⽂件,提供类/函数/变量的跳转等能⼒,后⾯会说到。
然后就可以开始正式的编译了:$ ninja -C out/android_debug_unopt
ninja: Entering directory `out/android_debug_unopt'
[38/3844] ACTION //flutter/shell/platform/android:flutter_shell_java(//build/toolchain/android:clang_arm)
警告: ../../third_party/android_tools/sdk/build-tools/30.0.1/core-lambda-
stubs.jar(java/lang/invoke/LambdaMetafactory.class): 主版本 53 ⽐ 52 新, 此编译器
⽀持最新的主版本。
建议升级此编译器。
注: 某些输⼊⽂件使⽤或覆盖了已过时的 API。
注: 有关详细信息, 请使⽤ -Xlint:deprecation 重新编译。
1 个警告
[3844/3844] STAMP obj/default.stamp
$ ls out/android_debug_unopt [19:35:15]
< flutter_embedding_debug.jar libflutter.so
armeabi_v7a_debug.jar flutter_embedding_debug.jar.md5.stamp libflutter.so.TOC
armeabi_v7a_debug.pom flutter_embedding_debug.pom obj
build.ninja flutter_icu toolchain.ninja
build.ninja.d flutter_patched_sdk vm_outline_strong.dill
clang_x64 gen vm_platform_strong.dill
flutter.jar gyp-mac-tool vm_platform_strong.dill.d
flutter_embedding_debug-sources.jar icudtl.dat zip_archives
其中,flutter_embedding_debug.jar是Android嵌⼊层代码,libflutter.so是flutter的引擎层代码,通过这两个⽂件,可以在Android⼯程混合接⼊Flutter代码。
源码使⽤
创建⼀个⼯程:$ pwd
/Users/vimerzhao/WorkProject/flutter_source_code
$ ls
depot_tools flutter src
$ ./flutter/bin/flutter create flutter_demo
Downloading Dart SDK from Flutter
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 172M 100 172M 0 0 4381k 0 0:00:40 0:00:40 --:--:-- 4723k
Building
Downloading 0.5s
Downloading 0.1s
Downloading package 0.3s
Downloading flutter_patched_ 2.7s
免费平台源码资源网Downloading flutter_patched_sdk_ 2.1s
Downloading 8.2s
0.0s
0.7s
0.0s
0.2s
......
Creating project
flutter_demo/workspacedata (created)
flutter_demo/workspace/xcshareddata/IDEWorkspaceChecks.plist (created)
......
flutter_demo/.idea/runConfigurations/l (created)
flutter_demo/.idea/libraries/l (created)
flutter_demo/.idea/l (created)
flutter_demo/.l (created)
flutter_demo/.l (created)
Running "flutter pub get" in 2.3s
Wrote 71 files.
All done!
......
Run "flutter doctor" for information about installing additional components.
In order to run your application, type:
$ cd flutter_demo
$ flutter run
使⽤flutter并指定本地engine运⾏(不指定则会拉远程的、已经构建好的engine)。$ ../flutter/bin/flutter run --local-engine-src-path ~/WorkProject/flutter_source_code/src --local-engine=android_debug_unopt
No Flutter engine build found at /Users/vimerzhao/WorkProject/flutter_source_code/src/out/host_debug_unopt.
$ ../flutter/bin/flutter run --local-engine-src-path ~/WorkProject/flutter_source_code/src --local-engine=host_debug_unopt
Launching lib/main.dart on DUK AL20 in
Oops; flutter has exited unexpectedly: "Invalid argument(s): Cannot find executable for
/Users/vimerzhao/WorkProject/flutter_source_code/src/out/host_debug_unopt/dart-sdk/bin/dart.".
A crash report has been written to /Users/vimerzhao/WorkProject/flutter_source_code/flutter_demo/flutter_02.log.
...
FAILURE: Build failed with an exception
* Where:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。