Implementing Aspect‑Oriented Programming in Flutter with AspectD
This article explains how the open‑source AspectD library enables Aspect‑Oriented Programming in Flutter by modifying the app.dill intermediate file, covering Flutter's compilation process, AST manipulation, point‑cut designs, code conversion flow, and practical use cases such as performance monitoring and page‑view tracking.
1. Project background: AOP (Aspect Oriented Programming) is introduced and the open‑source AspectD library for Flutter (GitHub: https://github.com/alibaba-flutter/aspectd) is highlighted.
2. Flutter compilation process: Dart source is compiled to the intermediate app.dill file, which becomes kernel_blob.bin in debug mode or a framework/so binary in release mode; Flutter AOP works by modifying this app.dill file.
3. The app.dill file is a binary intermediate; it can be converted to a readable format using dump_kernel.dart :
dart /path/to/dump_kernel.dart /path/to/app.dill /path/of/output.dill.txt4. Example Flutter application code (MyApp) and its transformed representation in the dill file are shown.
class MyApp extends StatelessWidget { /* ... */ }5. AST (Abstract Syntax Tree) access: the Component containing all libraries is read from the dill file with BinaryBuilderWithMetadata , then visited using the visitor.dart library.
final Component component = Component();
final List<int> bytes = File(dillFile).readAsBytesSync();
BinaryBuilderWithMetadata(bytes).readComponent(component);6. A simple MethodVisitor demonstrates how to replace calls to printCounter() with printCounterHook() by overriding visitMethodInvocation :
class MethodVisitor extends Transformer {
@override
MethodInvocation visitMethodInvocation(MethodInvocation methodInvocation) {
methodInvocation.transformChildren(this);
final Node? node = methodInvocation.interfaceTargetReference?.node;
if (node is Procedure && node.parent?.name == "_MyHomePageState" && methodInvocation.name.name == "printCounter") {
return MethodInvocation(methodInvocation.receiver, Name('printCounterHook'), null);
}
return methodInvocation;
}
}7. Beike_AspectD design: four point‑cut types (Call, Execute, Inject, Add) and the PointCut class that carries source information, target, function name, stub key, parameters, members, annotations, and a proceed() method to invoke the original implementation.
8. Code conversion flow integrated into ke_flutter_tools ; an Execute point‑cut example replaces the build() method with a hook and generates a stub method build_aop_stub_1 that contains the original logic:
method build(fra::BuildContext* context) → fra::Widget* {
return new hook::hook::•().hookBuild(new poi::PointCut::•({"importUri":"package:aop_demo/main.dart", ...}, this, "build", "aop_stub_1", [context], {}, {}, {}));
}
method build_aop_stub_1(fra::BuildContext* context) → fra::Widget* {
return new app::MaterialApp::•(title:"Flutter Demo", theme: the::ThemeData::•(primarySwatch:#C124), home: new main::MyHomePage::•(title:"Flutter Demo Home Page"));
}9. Application scenarios: the library is used for performance detection, event tracking, and JSON model conversion. An example injects code into MaterialPageRoute to capture the displayed widget name and its import URI for page‑view reporting.
@Inject("package:flutter/src/material/page.dart", "MaterialPageRoute", "-buildPage", lineNum:92)
@pragma("vm:entry-point")
void hookBuildPage() {
dynamic result;
String widgetName = result.toString();
String importUri = result.importUri(null);
print(widgetName + importUri);
// further reporting logic
}10. References to related blog posts and technical articles are listed at the end.
Beike Product & Technology
As Beike's official product and technology account, we are committed to building a platform for sharing Beike's product and technology insights, targeting internet/O2O developers and product professionals. We share high-quality original articles, tech salon events, and recruitment information weekly. Welcome to follow us.
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.