TRouter: A Hybrid Stack Routing Framework for Flutter with Memory Optimization and Visible Native Layer Modifications
TRouter is a hybrid stack routing framework for Flutter that reuses a single engine through native Activity/ViewController containers, offloading page transitions and URL parsing to the native layer, clearing iOS view bitmaps and applying bytecode‑hooked Android fixes to dramatically reduce memory usage while keeping native modifications visible, low‑risk, and compatible with multiple Flutter SDK versions.
In Flutter and native hybrid development, routing is an unavoidable topic. Existing solutions suffer from memory anomalies and require continual modifications to the official Flutter底层, causing risks and SDK upgrade difficulties.
The authors propose TRouter, a hybrid stack routing framework that reuses a single Flutter Engine via an Activity/ViewController container. The native layer handles page transitions, URL parsing, and attaches/detaches the engine, while the Dart layer receives the URL and parameters to render the appropriate Flutter page.
To solve iOS memory growth, TRouter clears the FlutterView.layer.contents bitmap in viewDidDisappear, releasing the large bitmap after a page disappears and re‑rendering it on viewWillAppear, eliminating the 10 MB per‑page increase.
On Android, the framework modifies the io.flutter package using a bytecode‑hook based FlutterFixPlugin. Changes are made visible and non‑intrusive, applied through a Gradle dependency ( apply plugin: 'com.tencent.fixflutter' ), and verified across Flutter SDK versions v1.17, v1.20, v1.22, and v2.0.
Advantages include simple routing, shared Dart resources, superior memory performance, visible low‑risk native modifications, and easy compatibility with future Flutter SDK updates.
Compared with the official multi‑engine approach (high memory consumption) and existing single‑engine schemes (Thrio/Boost), TRouter provides better memory efficiency and clearer, maintainable native code changes.
Future work will explore Flutter v2.0’s multi‑engine enhancements to achieve view‑level support for hybrid routing.
Tencent Cloud Developer
Official Tencent Cloud community account that brings together developers, shares practical tech insights, and fosters an influential tech exchange community.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.