A Kotlin Symbol Processing tool generating default interface implementation constructor functions
@DefaultImpl(MyInterfaceImpl::class)
interface MyInterface
internal object MyInterfaceImpl
val obj = MyInterface() // this will be generated with all constructor parameters
In your build.gradle.kts
:
plugins {
// ...
id("com.google.devtools.ksp")
}
kotlin {
// ...
sourceSets {
val commonMain by getting {
kotlin.srcDir("build/generated/ksp/metadata/commonMain/kotlin") // optional: if you want to generate the code in commonMain
dependencies {
implementation("io.github.overpas:default-impl-annotations:0.0.2")
}
}
// ...
}
}
dependencies {
add("kspCommonMainMetadata", "io.github.overpas:default-impl-processor:0.0.2")
// other configurations (don't include any if you want to generate the code in commonMain):
// add("kspJvm", "io.github.overpas:default-impl-processor:0.0.2")
// add("kspAndroid", "io.github.overpas:default-impl-processor:0.0.2")
// add("kspIosArm64", "io.github.overpas:default-impl-processor:0.0.2")
// etc.
}
// optional: if you want to generate the code in commonMain
tasks.withType<org.jetbrains.kotlin.gradle.dsl.KotlinCompile<*>>().configureEach {
if (name != "kspCommonMainKotlinMetadata") {
dependsOn("kspCommonMainKotlinMetadata")
}
}
In your build.gradle.kts
plugins {
// ...
id("com.google.devtools.ksp")
}
dependencies {
implementation("io.github.overpas:default-impl-annotations:0.0.2")
ksp("io.github.overpas:default-impl-processor:0.0.2")
}
If you are using Detekt and detektMetadataMain
fails with Reason: Task ':detektMetadataMain' uses this output of task ':kspCommonMainKotlinMetadata' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed.
you might consider adding:
tasks.matching { it.name == "detektMetadataMain" }
.configureEach {
dependsOn(tasks.matching { it.name == "kspCommonMainKotlinMetadata" })
}