美文网首页
2021-11-01 Flutter Engine编译与调试

2021-11-01 Flutter Engine编译与调试

作者: 我是小胡胡分胡 | 来源:发表于2021-11-01 21:27 被阅读0次

    1 ,安装 Chromium 的depot_tools

    我的目录是在/opt下面

    cd /opt
    git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
    

    修改环境变量

    vi ~/.bash_profile
    export PATH=$PATH:/opt/depot_tools
    

    2, 下载 buildroot

    我测试flutter engine编译的目录是/Volumes/huc/dartvm/flutter_engine

    在此目录下面创建名为.gclient的文件, 提前到github上fork engine项目到github.com/hc2088/engine.git自己的账号下:

    solutions = [
    {
      "managed": False,
      "name": "src/flutter",
      "url": "git@github.com:hc2088/engine.git",
      "custom_deps": {},
      "deps_file": "DEPS",
      "safesync_url": "",
    },
    ]
    

    执行

    cd /Volumes/huc/dartvm/flutter_engine/
    gclient sync 
    

    执行完成之后,会生成一个src目录,对应的就是buildroot 的git仓库

    cd src
     git remote -v
    origin  https://github.com/flutter/buildroot.git (fetch)
    origin  https://github.com/flutter/buildroot.git (push)
    

    src/flutter 对应就是flutter engine的git仓库

    cd src/flutter
    git remote -v
    origin  git@github.com:hc2088/engine.git (fetch)
    origin  git@github.com:hc2088/engine.git (push)
    

    3,确认flutter engine与flutter 版本是否一致

    我的flutter 装在/Volumes/huc/dartvm/flutter目录下

    which flutter 
    /Volumes/huc/dartvm/flutter/bin/flutter
    cd /Volumes/huc/dartvm/flutter
    cat bin/internal/engine.version 
    3a0a63c2d2098fffe4dc2fa161e265420bdfbb25
    

    修改flutter engine仓库的版本

    cd  /Volumes/huc/dartvm/flutter_engine/src/flutter
    git reset --hard 3a0a63c2d2098fffe4dc2fa161e265420bdfbb25
    

    4, 下载 依赖

    在src目录下,重新拉取依赖

    pwd
    /Volumes/huc/dartvm/flutter_engine/src
    gclient sync
    

    这一步会把dart sdk的源代码项目下载到下面目录:

    src/third_party/dart  
    
    pwd
    /Volumes/huc/dartvm/flutter_engine/src/third_party/dart
    
    git remote -v
    origin  https://dart.googlesource.com/sdk.git (fetch)
    origin  https://dart.googlesource.com/sdk.git (push)
    
    
    image.png

    注意执行这一步,会比较慢:Syncing projects done完成之后,不要终止进程,继续等后面任务执行完:

    huchu58:src huchu$ gclient sync
    23>WARNING: subprocess '"git" "-c" "core.deltaBaseCacheLimit=2g" "clone" "--no-checkout" "--progress" "https://dart.googlesource.com/sdk.git" "/Volumes/huc/dartvm/flutter_engine/src/third_party/_gclient_dart_0aepvjce"' in /Volumes/huc/dartvm/flutter_engine failed; will retry after a short nap...
    53>WARNING: subprocess '"git" "-c" "core.deltaBaseCacheLimit=2g" "fetch" "origin" "--no-tags"' in /Volumes/huc/dartvm/flutter_engine/src/third_party/pkg/vector_math failed; will retry after a short nap...
    Syncing projects:  50% (65/129) src/third_party/pkg/vector_math
    [0:01:40] Still working on:
    [0:01:40]   src/third_party/dart
    //......
    Syncing projects: 100% (129/129), done.                                         
    ///这里需要等
    Hook 'python3 src/flutter/tools/pub_get_offline.py' took 21.91 secs
    Running hooks:  88% ( 8/ 9) Download prebuilt Dart SDK
    Hook 'python3 src/flutter/tools/download_dart_sdk.py' took 195.20 secs
    Running hooks: 100% (9/9), done.  
    

    5, 准备构建文件

    使用预先构建的dart sdk: (默认的是--prebuilt-dart-sdk)

    cd src
    ./flutter/tools/gn --ios --unoptimized --prebuilt-dart-sdk
    ./flutter/tools/gn --unoptimized --prebuilt-dart-sdk
    

    使用dart sdk源代码,现编译的dart sdk:
    改成--no-prebuilt-dart-sdk

    huchu58:src huchu$ ./flutter/tools/gn --ios --unoptimized --no-prebuilt-dart-sdk
    GOMA usage was specified but can't be found, falling back to local builds. Set the GOMA_DIR environment variable to fix GOMA.
    Generating GN files in: out/ios_debug_unopt
    Generating Xcode projects took 405ms
    Generating compile_commands took 79ms
    Done. Made 564 targets from 213 files in 9560ms
    huchu58:src huchu$ ./flutter/tools/gn --unoptimized --no-prebuilt-dart-sdk
    GOMA usage was specified but can't be found, falling back to local builds. Set the GOMA_DIR environment variable to fix GOMA.
    Generating GN files in: out/host_debug_unopt
    Generating Xcode projects took 447ms
    Generating compile_commands took 91ms
    Done. Made 931 targets from 260 files in 5531ms
    
    

    6, 构建 engine

    huchu58:src huchu$ ninja -C out/ios_debug_unopt && ninja -C out/host_debug_unopt
    ninja: Entering directory `out/ios_debug_unopt'
    [1362/1362] STAMP obj/default.stamp
    ninja: Entering directory `out/host_debug_unopt'
    [6208/6208] STAMP obj/default.stamp
    

    输出:

    src/out/host_debug_unopt
    src/out/ios_debug_unopt
    

    7, flutter app集成自定义engine

    创建flutter app

    cd ~/Desktop
    flutter create xxapp
    
    flutter run --local-engine=ios_debug_unopt --local-engine-src-path=/Volumes/huc/dartvm/flutter_engine/src
    

    使用--config-local,不编译只是修改xcode LOCAL_ENGINE 和相关插件的配置,加快速度。

    flutter build ios --local-engine=ios_debug_unopt --config-only --local-engine-src-path=/Volumes/huc/dartvm/flutter_engine/src
    

    flutter app native端确认用的flutter engine

    打开Runner.xcworkspace工程:
    /Users/huchu/Desktop/xxapp/ios/Runner.xcworkspace

    问题:怎么都不进断点 -[FlutterEngine runWithEntrypoint:]

    8,xcode集成原理

    run 项目时候,xcode先后调用以下脚本:

    /bin/sh "$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" build
    /bin/sh "$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" embed_and_thin
    

    这里的FLUTTER_ROOT是/Volumes/huc/dartvm/flutter
    主要作用是确认dart 是/Volumes/huc/dartvm/flutter/bin/dart
    读取在flutter build/run这一步配置好的相关变量值:

    image.png
    然后会生成flutter_export_environment.sh 文件,flutter_export_environment.sh 使得变量有效:
    #!/bin/sh
    # This is a generated file; do not edit or check into version control.
    export "FLUTTER_ENGINE=/Volumes/huc/dartvm/flutter_engine/src"
    export "LOCAL_ENGINE=ios_debug_unopt"
    #其他内容这里就不粘贴了
    ....
    

    可以看到/Volumes/huc/dartvm/flutter/packages/flutter_tools/bin/xcode_backend.dart文件有用到:

    如果没有用自定flutter_engine

    final String frameworkPath = '${environmentEnsure('FLUTTER_ROOT')}/bin/cache/artifacts/engine/$artifactVariant';
    String flutterFramework = '$frameworkPath/Flutter.xcframework';
    

    对应使用的engine 就是 flutter 自己同步的

    image.png

    咱们这里用了--local-engine

    final String? localEngine = environment['LOCAL_ENGINE'];
     flutterFramework = '${environmentEnsure('FLUTTER_ENGINE')}/out/$localEngine/Flutter.xcframework';
    

    该路径指定了使用我们自定义的flutter_engine的产物:

    image.png
    flutter App dart端如何确认dart sdk
    修改pubspec.yaml
    dependency_overrides:
      sky_engine:
        path: /Volumes/huc/dartvm/flutter_engine/src/out/ios_debug_unopt/gen/dart-pkg/sky_engine
    

    默认为:

    /Volumes/huc/dartvm/flutter/bin/cache/pkg/sky_engine
    

    相关文章

      网友评论

          本文标题:2021-11-01 Flutter Engine编译与调试

          本文链接:https://www.haomeiwen.com/subject/meetzltx.html