Skip to content

Add --trace flag to flutter build apk for build profiling#116

Draft
eseidel wants to merge 3 commits intoshorebird/devfrom
add-build-trace
Draft

Add --trace flag to flutter build apk for build profiling#116
eseidel wants to merge 3 commits intoshorebird/devfrom
add-build-trace

Conversation

@eseidel
Copy link

@eseidel eseidel commented Mar 14, 2026

Summary

  • Adds a --trace option to flutter build apk, flutter build ios, and flutter build ipa that produces a Chrome Trace Event Format JSON file
  • The trace shows where time is spent across three layers: flutter tool overhead, native build system (Gradle/Xcode), and flutter assemble targets
  • Output viewable in Perfetto at https://ui.perfetto.dev

Usage

flutter build apk --trace=build_trace.json
flutter build ios --trace=build_trace.json
flutter build ipa --trace=build_trace.json

Implementation

Core:

  • New BuildTracer utility class for collecting, merging, and writing Chrome Trace Event Format events
  • --trace-file option threaded through both Gradle plugin (Kotlin) and Xcode build phases (xcode_backend.dart) to flutter assemble
  • Wall-clock timestamps added to PerformanceMeasurement for accurate cross-process timing

Android path: gradle.dart → Gradle -Ptrace-file property → FlutterPlugin.ktBaseFlutterTaskHelper.ktflutter assemble --trace-file

iOS path: mac.dartTRACE_FILE Xcode build setting → xcode_backend.dartflutter assemble --trace-file

Three trace rows (tids) in Perfetto:

  1. flutter tool (tid 1): pre-build setup, post-build processing, overall build
  2. native build system (tid 2): Gradle or Xcode subprocess
  3. flutter assemble (tid 3): individual build targets (KernelSnapshot, AotElfRelease/AotAssembly, CopyAssets, etc.)

Test plan

  • Unit tests for BuildTracer and BuildTraceEvent (8 tests)
  • Unit test for writeTraceData in assemble
  • Existing assemble tests pass (19 tests)
  • Existing build_system tests pass (39 tests)
  • Existing build_info tests pass (18 tests)
  • dart analyze clean on all modified files
  • Manual: flutter build apk --trace=trace.json on a sample app, view in Perfetto
  • Manual: flutter build ios --trace=trace.json on a sample app, view in Perfetto

eseidel added 3 commits March 13, 2026 22:55
Adds a --trace option that produces a Chrome Trace Event Format JSON
file showing where time is spent across all build layers (flutter tool,
Gradle, flutter assemble targets). The output can be viewed in Perfetto
at https://ui.perfetto.dev.
The intermediate trace file path is shared across all Gradle variants.
This is safe today since flutter build apk only runs one variant per
invocation, but would need per-variant paths if that changes.
- Add --trace option to flutter build ios / flutter build ipa
- Pass TRACE_FILE through Xcode build settings to xcode_backend.dart
- Instrument buildXcodeProject() in mac.dart with pre-xcode, xcode, and
  post-xcode spans
- Merge flutter assemble trace events from intermediate file
- Remove TRACE_FILE from toEnvironmentConfig() since both Android and
  iOS orchestrators compute intermediate paths directly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant