No-arg compiler plugin
The no-arg compiler plugin generates a zero-argument constructor for classes with a specific annotation.
The generated constructor is annotated with @Deprecated at the HIDDEN level, so you can call it from Java but not from Kotlin. Frameworks can call it through reflection.
For example, this allows frameworks such as the Java Persistence API (JPA) to instantiate Kotlin classes that don't declare a zero-argument constructor.
Declare an annotation
The no-arg plugin only processes classes marked with an annotation that you register in its configuration. So before enabling the plugin, declare an annotation to mark the classes that need a zero-argument constructor. For example, NoArgAnnotation:
Apply the plugin
Gradle
Add the no-arg plugin in the plugins {} block of your build.gradle(.kts) file:
Then specify the annotation you declared earlier (for example, com.example.NoArgAnnotation) to generate a zero-argument constructor for annotated classes:
Enable the invokeInitializers option if you want the plugin to run the class initialization logic from the generated constructor. This option is disabled by default:
Maven
Add the plugin to your pom.xml file:
Use the no-arg plugin
When the plugin is applied, classes annotated with a registered annotation get a zero-argument constructor at compile time. You don't need to declare the constructor manually:
What's next?
For JPA entities, use the
kotlin-jpaplugin, which configures theno-argandall-openplugins with JPA annotations such as@Entity,@Embeddable, and@MappedSuperclass.Explore the source code of the
no-argcompiler plugin.