flutter安装和编译以及调试大杂烩

关于编译:

General Compilation Tips

  • For local development and testing, it’s generally preferable to use --unopt builds. These builds will have additional logging and checks enabled, and generally use build and link flags that lead to faster compilation and better debugging symbols. If you are trying to do performance testing with a local build, do not use the --unopt flag.
  • Link Time Optimization: Optimized builds also perform Link Time Optimization of all binaries. This makes the linker take a lot of time and memory to produce binaries. If you need optimized binaries but don’t want to perform LTO, add the --no-lto flag.
  • Android and iOS expect both a host and android (or ios) build. It is critical to recompile the host build after upgrading the Dart SDK (e.g. via a gclient sync after merging up to head), since artifacts from the host build need to be version matched to artifacts in the Android/iOS build.
  • Web, Desktop, and Fuchsia builds have only one build target (i.e. host or fuchsia).
  • Make sure to exclude the out directory from any backup scripts, as many large binary artifacts are generated. This is also generally true for all of the directories outside of the engine/src/flutter directory.

Using a custom Dart SDK

When targeting the host and desktop, on CI we use a pre-built Dart SDK vended by the Dart team. To build and use the SDK from the Dart sources downloaded by gclient sync, after editing those source files, pass the flag --no-prebuilt-dart-sdk to //flutter/tools/gn.

Compiling for Windows

You can only build selected binaries on Windows (mainly gen_snapshot and the desktop embedding).

On Windows, ensure that the engine checkout is not deeply nested. This avoid the issue of the build scripts working with excessively long paths.

  1. Make sure you have Visual Studio installed (non-Googlers only). Debugging Tools for Windows 10 must be installed.
  2. git pull upstream main in src/flutter to update the Flutter Engine repo.
  3. Ensure long path support is enabled on your machine. Launch PowerShell as an administrator and run:
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem" -Name "LongPathsEnabled" -Value 1 -Force
  1. If you are not a Google employee, you must set the following environment variables to point the depot tools at Visual Studio:
DEPOT_TOOLS_WIN_TOOLCHAIN=0
GYP_MSVS_OVERRIDE_PATH="C:\Program Files (x86)\Microsoft Visual Studio\2019\Community" # (or your location for Visual Studio)
WINDOWSSDKDIR="C:\Program Files (x86)\Windows Kits\10" # (or your location for Windows Kits)

Also, be sure that Python27 is before any other python in your Path.

  1. gclient sync to update your dependencies.
  2. switch to src/ directory.
  3. python .\flutter\tools\gn --unoptimized to prepare your build files.
    • If you are only building gen_snapshotpython .\flutter\tools\gn [--unoptimized] --runtime-mode=[debug|profile|release] [--android].
  4. ninja -C .\out\<dir created by previous step> to build.
    • If you used a non-debug configuration, use ninja -C .\out\<dir created by previous step> gen_snapshot. Release and profile are not yet supported for the desktop shell.
python .\flutter\tools\gn --unoptimized --no-prebuilt-dart-sdk

Compiling for the Web

For building the engine for the Web we use the felt tool.

To test Flutter with a local build of the Web engine, add --local-web-sdk=wasm_release to your flutter command, e.g.:

flutter run --local-web-sdk=wasm_release -d chrome
flutter test --local-web-sdk=wasm_release test/path/to/your_test.dart

Compiling for the Web on Windows

Compiling the web engine might take a few extra steps on Windows. Use cmd.exe and “run as administrator”.

  1. Make sure you have Visual Studio installed. Set the following environment variables. For Visual Studio use the path of the version you installed.
    • GYP_MSVS_OVERRIDE_PATH = "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community"
    • GYP_MSVS_VERSION = 2017
  2. Make sure, depot_tools, ninja and python are installed and added to the path. Also set the following environment variable for depot tools:
    • DEPOT_TOOLS_WIN_TOOLCHAIN = 0
    • Tip: if you get a python error try to use Python 2 instead of 3
  3. git pull upstream main in src/flutter to update the Flutter Engine repo.
  4. gclient sync to update your dependencies.
    • Tip: If you get a git authentication errors on this step try Git Bash instead
  5. python .\flutter\tools\gn --unoptimized --full-dart-sdk to prepare your build files.
  6. ninja -C .\out\<dir created by previous step> to build.

To test Flutter with a local build of the Web engine, add --local-web-sdk=wasm_release to your flutter command, e.g.:

flutter run --local-web-sdk=wasm_release -d chrome
flutter test --local-web-sdk=wasm_release test/path/to/your_test.dart

For testing the engine again use felt tool this time with felt_windows.bat.

felt_windows.bat test

Troubleshooting Compile Errors

Version Solving Failed

From time to time, as the Dart versions increase, you might see dependency errors such as:

The current Dart SDK version is 2.7.0-dev.0.0.flutter-1ef444139c.

Because ui depends on <a pub package> 1.0.0 which requires SDK version >=2.7.0 <3.0.0, version solving failed.

Running gclient sync does not update the tags, there are two solutions:

  1. under engine/src/third_party/dart run git fetch --tags origin
  2. or run gclient sync with with tags parameter: gclient sync --with_tags

See also: Debugging the engine, which includes instructions on running a Flutter app with a local engine.

Comments

No comments yet. Why don’t you start the discussion?

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注