Mobile Development 8 min read

FlutterCodeX: Runtime Code Coverage SDK for Flutter Apps

FlutterCodeX introduces a non‑intrusive, cross‑platform runtime code‑coverage SDK that instruments iOS, Android and Flutter apps—using meta‑class flags, Gradle‑injected static blocks, and the Dart AOP framework AspectD—to automatically record class‑initialization events, upload compressed data to Alibaba Cloud, and provide precise online coverage metrics for dead‑code removal and package‑size reduction in Xianyu.

Xianyu Technology
Xianyu Technology
Xianyu Technology
FlutterCodeX: Runtime Code Coverage SDK for Flutter Apps

Background : With the migration of Xianyu’s legacy business to Flutter, the app package size has grown significantly, and Flutter code now accounts for about 20% of the total size. Accurate online code‑coverage data is needed to identify and remove dead code.

To obtain reliable coverage, a non‑intrusive instrumentation solution is required for iOS, Android, and Flutter.

Instrumentation approaches :

iOS : Uses the +initialize method and the RW_INITIALIZED flag in the meta‑class data to detect class initialization.

Android : Inserts static code blocks via a Gradle plugin that traverses class files at compile time.

Flutter : Lacks static initializers; instead, an AOP framework called AspectD is used to hook constructors without modifying source code.

AspectD is a Dart AOP framework that applies a Transform to the compiled dill file. Example usage:

@Aspect()
@pragma("vm:entry-point")
class CodeXExecute {}

@Call("package:flutter_codex_demo/test.dart", "A", "+A")
void _incrementA(PointCut pointcut) {
    pointcut.proceed();
    // todo report class A initialize
}

Overall design of FlutterCodeX :

Compile‑time instrumentation plugin : A CodeXGenerator with a CodeAstVisitor parses all class ASTs, finds constructors, and generates AspectD point‑cut classes that report class‑initialization events. Key snippet:

void visitClassDeclaration(ClassDeclaration node) {
    // iterate members, find constructors
    if (member is ConstructorDeclaration) {
        String constructorName = member.name?.name ?? sourceNode.name;
        sourceNode.constructor.add(constructorName);
    }
    CodeXGenerator.collector.codeList[sourceNode.key()] = sourceNode;
}

Runtime data collection : After each class is initialized, the generated code calls addCallTime to cache the event. Periodically, the data is compressed and uploaded to Alibaba Cloud OSS with a sampling strategy (e.g., at least 50k active users).

Data aggregation : Uploaded coverage data is compared against the full class list produced at build time, yielding precise online code‑coverage metrics that guide code removal and package slimming.

Demo : A simple demo project shows the end‑to‑end flow, from instrumentation to data upload and analysis.

Conclusion : FlutterCodeX is being rolled out in the Xianyu app, combining Android, iOS, and Flutter coverage data to effectively deprecate unused business logic, reduce package size, and provide long‑term health monitoring for the codebase.

fluttermobile developmentAspectDcode coverageinstrumentationRuntime Analysis
Xianyu Technology
Written by

Xianyu Technology

Official account of the Xianyu technology team

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.