Mobile Development 7 min read

Optimizing Android Layout Inflation with Layout2Code: An APT‑Based Approach

The team created Layout2Code, an APT‑based generator that translates XML layouts into Kotlin code at build time—handling merge and include tags and supporting custom Views—cutting homepage layout inflation by over 200 ms, boosting performance metrics while requiring far less maintenance than traditional X2C solutions.

Xiaohongshu Tech REDtech
Xiaohongshu Tech REDtech
Xiaohongshu Tech REDtech
Optimizing Android Layout Inflation with Layout2Code: An APT‑Based Approach

At the beginning of 2020, the UI complexity of the Xiaohongshu homepage increased dramatically. While layout XML optimization and stub techniques were applied, the team sought lower‑cost, higher‑performance alternatives.

X2C, a well‑known industry technique, compiles XML into code at build time, eliminating reflection and resource‑file reads. However, extensive use of custom Views in the app made X2C maintenance costly.

Deep analysis of LayoutInflater latency led to an APT (Annotation Processing Tool) solution that works for all View scenarios, avoids reflection overhead, and adds no extra maintenance burden, becoming an out‑of‑the‑box tool.

The inspiration came from Google’s experimental ViewCompiler, which can translate XML layouts into Java or DEX files but does not support merge and include tags.

Because ViewCompiler is still experimental (introduced in Android Q and disabled in Android S), the team built a custom generator that produces Kotlin code using kapt and kotlinpoet . The generator parses XML via Resources.getLayout and XmlResourceParser , iterates over tags, and emits Kotlin code that directly creates View instances.

Special handling is added for merge and include to recursively combine parent‑child layouts.

After replacing several homepage layouts with the generated code, the p90 layout time dropped by more than 200 ms, and key metrics such as CES, retention, and overall performance improved significantly.

The article also explains the LayoutInflater workflow, showing how AppCompatViewInflater creates native Views without reflection by matching class names directly.

Compared with traditional X2C, Layout2Code offers broader applicability (including custom Views) and lower maintenance cost. It avoids moving XML parsing to compile time because the runtime cost of loading a layout is typically under 1 ms, and the maintenance effort of compile‑time parsing outweighs the benefit.

Future work includes migrating from kapt to ksp to further reduce compilation time.

performance optimizationAndroidKotlinAPTLayoutInflaterX2C
Xiaohongshu Tech REDtech
Written by

Xiaohongshu Tech REDtech

Official account of the Xiaohongshu tech team, sharing tech innovations and problem insights, advancing together.

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.