KotlinCompilation
Kotlin compilation
Represents the configuration of a Kotlin Compiler invocation. The KotlinCompilation API is designed to ensure the correct and consistent propagation of any compilation changes to all underlying tasks and configurations. Use the KotlinCompilation API instead of getting tasks, configurations, and other related domain objects directly through the Gradle API. For Native targets, KotlinCompilation also provides an API to configure cinterop.
A KotlinTarget contains multiple KotlinCompilations. By default, a KotlinTarget contains two KotlinCompilations with the names "main" and "test".
Here's an example of how to use a KotlinCompilation to configure compilation tasks for the JVM target:
// build.gradle.kts
kotlin {
jvm {
compilations.all {
compileTaskProvider {
// Configure the compile task here
}
}
}
}
Main compilation
The KotlinCompilation with the name "main" represents a Kotlin compiler invocation for the main sources of the KotlinTarget. The results of the main compilation are publishable and exposed via the KotlinTarget.apiElementsConfigurationName consumable configuration.
Here's an example of how to use consume the outputs of the main JVM Compilation for custom processing:
// build.gradle.kts
val jvmMainClasses = kotlin.jvm().compilations.getByName("main").output.classesDirs
tasks.register<Jar>("customJar") {
from(jvmMainClasses)
}
Test compilation
The KotlinCompilation with the name "test" represents a Kotlin Compiler invocation for the test source sets. The test compilation is implicitly associated with the main compilation. See KotlinCompilation.associatedCompilations for more information. This means that the test compilation sees all dependencies, internal and public declarations of the main compilation.
Custom compilation
It is possible to create additional custom compilations for a KotlinTarget:
kotlin {
jvm {
compilations.create("customCompilation")
}
}
Use a separate Gradle project instead of creating a custom compilation for an easier and safer setup.
Metadata target compilation
The Kotlin metadata target is a special KotlinTarget that manages compiler invocations that compile the code of shared source sets. There are no "main" or "test" Kotlin Compilations for the Metadata Target. Instead, there is a dedicated compilation for each shared source set.
Types
Constants for the KotlinCompilation.
Properties
A full transitive closure of associatedCompilations.
All KotlinSourceSets used by this compilation.
The name of the Gradle Configuration that contains api
dependencies.
A list of all compilations that were previously associated with this compilation using associateWith.
The name of the compilation.
The Gradle task name that is used as a meta task to trigger all the required compilation tasks for this KotlinCompilation.
The name of the Gradle configuration containing all the resolved dependencies required for compilation.
A collection of file system locations for the artifacts of compilation dependencies.
The Kotlin Gradle plugin task name that is used to run the compilation process for this Kotlin compilation.
The name of the Gradle Configuration containing compileOnly
dependencies.
Provides access to the compilation task for this compilation.
The KotlinSourceSet by default associated with this compilation.
A unique name for this compilation in the project.
The name of the Gradle Configuration that contains implementation
dependencies.
All KotlinSourceSets used by this compilation.
Represents the output of a Kotlin compilation.
Represents the KotlinPlatformType to which this compilation belongs.
The name of the Gradle configuration containing all the resolved dependencies required to run compilation output.
A collection of file system locations for the artifacts of runtime dependencies.
The name of the Gradle Configuration containing runtimeOnly
dependencies.
Represents a KotlinTarget which this compilation belongs to.
Functions
Associates the current KotlinCompilation with another KotlinCompilation.
Configures the compilation AttributeContainer with the provided configuration.
Configures the provided defaultSourceSet.
Configures all dependencies for this entity.
Will add a KotlinSourceSet directly into this compilation. This method is deprecated and is soon to be removed.