Improving Kotlin Data Class Serialization with a Compiler Plugin: Design, Implementation, and Performance Evaluation
This article presents a Kotlin compiler plugin that replaces verbose annotation‑based serialization with concise delegated properties, explains its design and implementation details, demonstrates significant compilation‑time reductions of up to 20% and cleaner code, and provides a step‑by‑step guide for developers interested in building similar compiler extensions.
The author observed that Kotlin data classes used for network deserialization often require many annotations, nullable types, and mutable var fields, which makes the code noisy and slows down compilation when processed by KAPT. To address these pain points, a custom Kotlin Compiler Plugin (KCP) was created to generate a syntactic sugar that replaces the boilerplate with delegated properties while preserving runtime behavior.
Background
Existing solutions such as android-kotlin-synthetics and ksp provide better performance than traditional annotation processing (APT, Transform). The author built a prototype plugin called Kotlin-Compiler-Plugin (KCP) that improves both code elegance and compilation speed.
Experiment and Results
An example POJO with seven annotations and nullable fields is shown. By converting the fields to delegated properties using by serialized<T>() , the visual clutter disappears and the need for var backing fields is eliminated.
@ProtoMessage("webcast.data.GiftTip")
data class GiftTip(
@SerializedName("display_text") @JvmField var displayText: Text? = null,
@SerializedName("background_color") @JvmField var backgroundColor: String? = null,
@SerializedName("prefix_image") @JvmField var perfixImage: ImageModel? = null
)Performance tests show compilation time reductions of 20% locally and 16.4% on CI when switching from KAPT to KCP:
Scenario
kapt (old)
kcp (new)
Local build
07:03
05:38
CI build
10:35
08:51
Kotlin (JVM) Compilation Overview
The article outlines the two‑stage compilation process (prepare → compile) and the separation of front‑end and back‑end phases. The front‑end parses source files into PSI/KtElements, builds descriptors, and stores them in a BindingContext . The back‑end consumes the BindingContext to generate bytecode.
Key Backend Classes
MemberCodegen<T : KtPureElement> : entry point for generating bytecode for classes or objects.
ClassBuilder : abstraction over ASM’s ClassVisitor that creates the actual class file.
Utility classes such as FunctionCodegen , PropertyCodegen , ExpressionCodegen , KotlinTypeMapper , and InstructionAdapter assist in generating method bodies and handling types.
Implementation Details
The plugin works in three phases:
Collect properties that delegate to serialized during the analysis phase ( AnalysisHandlerExtension ).
Intercept class building ( ClassBuilderInterceptorExtension ) to suppress the default field and accessor generation for delegated properties.
Generate custom bytecode ( ExpressionCodegenExtension ) that creates protected backing fields, rewrites constructors, and adds a Protobuf‑aware constructor that reads data via a ProtoReader .
Example of the generated delegate functions:
fun
serialized(key: String = ""): DecodeDelegate
// no default value
fun
serialized(key: String = "", defaultValue: R): DecodeDelegate
// with default valueThe generated Java code for a class using the plugin demonstrates how the delegated properties become regular final fields with appropriate getters, and how a constructor that accepts a ProtoReader populates those fields.
Conclusion
While developing KCP is more involved than using existing Android extensions, the plugin yields cleaner source code and measurable compile‑time improvements. The article shares practical tips on feature design, code generation, handling of collections, nullability, and default values, and points to useful open‑source references for further exploration.
Sohu Tech Products
A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.
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.