Kotlin Help

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:

package com.example annotation class NoArgAnnotation

Apply the plugin

Gradle

Add the no-arg plugin in the plugins {} block of your build.gradle(.kts) file:

plugins { kotlin("jvm") version "2.4.21" // The version of Kotlin to use kotlin("plugin.noarg") version "2.4.21" // The Kotlin no-arg plugin }
plugins { id "org.jetbrains.kotlin.jvm" version "2.4.21" id "org.jetbrains.kotlin.plugin.noarg" version "2.4.21" }

Then specify the annotation you declared earlier (for example, com.example.NoArgAnnotation) to generate a zero-argument constructor for annotated classes:

noArg { annotation("com.example.NoArgAnnotation") }

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:

noArg { invokeInitializers = true }

Maven

Add the plugin to your pom.xml file:

<properties> <kotlin.version>2.4.21</kotlin.version> </properties> <plugin> <artifactId>kotlin-maven-plugin</artifactId> <groupId>org.jetbrains.kotlin</groupId> <version>${kotlin.version}</version> <configuration> <compilerPlugins> <plugin>no-arg</plugin> </compilerPlugins> <pluginOptions> <option>no-arg:annotation=com.example.NoArgAnnotation</option> <!-- Call instance initializers in the generated constructor --> <!-- <option>no-arg:invokeInitializers=true</option> --> </pluginOptions> </configuration> <dependencies> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-maven-noarg</artifactId> <version>${kotlin.version}</version> </dependency> </dependencies> </plugin>

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:

package com.example // The no-arg plugin generates a zero-argument constructor for this class @NoArgAnnotation class MyEntity(val id: Int)

What's next?

  • For JPA entities, use the kotlin-jpa plugin, which configures the no-arg and all-open plugins with JPA annotations such as @Entity, @Embeddable, and @MappedSuperclass.

  • Explore the source code of the no-arg compiler plugin.

21 October 2026