
Enables simple service provider interface functionality with automatic service discovery, easy integration via annotations, and support for multiple service declarations. Includes a Gradle plugin and KSP processor for seamless configuration.
Simple SPI (Service Provider Interface) for Kotlin Multiplatform (equivalent of JVM's Service Loader)
// module: A
@Service
interface SimpleService {
fun saySomethingSweet()
}
// module: A, B or even in a library!
@ServiceProvider
object SimpleServiceImpl : SimpleService {
override fun saySomethingSweet() {
println("Kotlin is Awesome")
}
}
// module: A, B, C or may be not even in your codebase...
fun main() {
ServiceLoader.load<SimpleService>().forEach { service ->
service.saySomethingSweet()
}
}sweet-spi consists of three parts:
Documentation can be found here, or on the website:
Compatible with Kotlin 2.0.0+ and KSP 1.0.24+ (tested up to including Kotlin 2.2.20-RC and KSP 2.0.2). Using other Kotlin/KSP versions should still work but is not tested.
// In your build.gradle.kts file:
// ATTENTION: this import is REQUIRED
import dev.whyoleg.sweetspi.gradle.*
plugins {
// apply kotlin-jvm or kotlin-multiplatform plugin
kotlin("multiplatform") version "2.0.0"
// apply KSP, don't worry, Gradle Plugin will warn if you've forgotten
id("com.google.devtools.ksp") version "2.0.0-1.0.24"
// finally, apply `sweetspi` Gradle Plugin
id("dev.whyoleg.sweetspi") version "0.1.3"
}
kotlin {
// just one line and we are good to go
withSweetSpi()
// declare Kotlin targets
jvm()
wasmJs { browser() }
iosArm64()
iosX64()
iosSimulatorArm64()
// and any other target
}sweet-spi Gradle plugin will automatically add runtime and KSP processor dependencies to appropriate configurations:
implementation scope of main source set tree
api(sweetSpiRuntime()) to dependencies block of the appropriate source
setwithsweetSpi but on KotlinTarget or KotlinCompilation
withSweetSpi is called
test source set tree.
For this you could manually add KSP processor there, or use compilationFilter parameter of withSweetSpi functiondev.whyoleg.sweetspi.suppressGradleKspConfigurationChecker=true to your gradle.properties fileAs stated above, Gradle Plugin is contains just checkers and a small amount of helper functions, this means, that it's possible to use sweet-spi without it:
// In your build.gradle.kts file:
plugins {
kotlin("multiplatform") version "2.0.0"
id("com.google.devtools.ksp") version "2.0.0-1.0.24"
}
kotlin {
// declare Kotlin targets
jvm()
wasmJs { browser() }
iosArm64()
iosX64()
iosSimulatorArm64()
// and any other target
sourceSets {
commonMain.dependencies {
// add runtime
implementation("dev.whyoleg.sweetspi:sweetspi-runtime:0.1.3")
}
}
}
dependencies {
// Note: `ksp` configuration should be used only for jvm projects
// it's deprecated and will be removed in KSP 2.0
// https://kotlinlang.org/docs/ksp-multiplatform.html#avoid-the-ksp-configuration-on-ksp-1-0-1
ksp("dev.whyoleg.sweetspi:sweetspi-processor:0.1.3")
// the correct way will be to apply to each target individually,
// Note: no need to add it to the `common` compilations, as it'd do nothing there
add("kspJvm", "dev.whyoleg.sweetspi:sweetspi-processor:0.1.3")
// if needed, `test` source sets support
add("kspJvmTest", "dev.whyoleg.sweetspi:sweetspi-processor:0.1.3")
// and for each other Kotlin target...
add("kspLinuxX64Test", "dev.whyoleg.sweetspi:sweetspi-processor:0.1.3")
}deprecated from the beginning). But, it works!Of course there could be some bugs, feel free to report them, it would really mean a lot to me! For bugs, questions, and discussions, please use the GitHub Issues
This project is licensed under the Apache 2.0 license. See the LICENSE file for details.
Simple SPI (Service Provider Interface) for Kotlin Multiplatform (equivalent of JVM's Service Loader)
// module: A
@Service
interface SimpleService {
fun saySomethingSweet()
}
// module: A, B or even in a library!
@ServiceProvider
object SimpleServiceImpl : SimpleService {
override fun saySomethingSweet() {
println("Kotlin is Awesome")
}
}
// module: A, B, C or may be not even in your codebase...
fun main() {
ServiceLoader.load<SimpleService>().forEach { service ->
service.saySomethingSweet()
}
}sweet-spi consists of three parts:
Documentation can be found here, or on the website:
Compatible with Kotlin 2.0.0+ and KSP 1.0.24+ (tested up to including Kotlin 2.2.20-RC and KSP 2.0.2). Using other Kotlin/KSP versions should still work but is not tested.
// In your build.gradle.kts file:
// ATTENTION: this import is REQUIRED
import dev.whyoleg.sweetspi.gradle.*
plugins {
// apply kotlin-jvm or kotlin-multiplatform plugin
kotlin("multiplatform") version "2.0.0"
// apply KSP, don't worry, Gradle Plugin will warn if you've forgotten
id("com.google.devtools.ksp") version "2.0.0-1.0.24"
// finally, apply `sweetspi` Gradle Plugin
id("dev.whyoleg.sweetspi") version "0.1.3"
}
kotlin {
// just one line and we are good to go
withSweetSpi()
// declare Kotlin targets
jvm()
wasmJs { browser() }
iosArm64()
iosX64()
iosSimulatorArm64()
// and any other target
}sweet-spi Gradle plugin will automatically add runtime and KSP processor dependencies to appropriate configurations:
implementation scope of main source set tree
api(sweetSpiRuntime()) to dependencies block of the appropriate source
setwithsweetSpi but on KotlinTarget or KotlinCompilation
withSweetSpi is called
test source set tree.
For this you could manually add KSP processor there, or use compilationFilter parameter of withSweetSpi functiondev.whyoleg.sweetspi.suppressGradleKspConfigurationChecker=true to your gradle.properties fileAs stated above, Gradle Plugin is contains just checkers and a small amount of helper functions, this means, that it's possible to use sweet-spi without it:
// In your build.gradle.kts file:
plugins {
kotlin("multiplatform") version "2.0.0"
id("com.google.devtools.ksp") version "2.0.0-1.0.24"
}
kotlin {
// declare Kotlin targets
jvm()
wasmJs { browser() }
iosArm64()
iosX64()
iosSimulatorArm64()
// and any other target
sourceSets {
commonMain.dependencies {
// add runtime
implementation("dev.whyoleg.sweetspi:sweetspi-runtime:0.1.3")
}
}
}
dependencies {
// Note: `ksp` configuration should be used only for jvm projects
// it's deprecated and will be removed in KSP 2.0
// https://kotlinlang.org/docs/ksp-multiplatform.html#avoid-the-ksp-configuration-on-ksp-1-0-1
ksp("dev.whyoleg.sweetspi:sweetspi-processor:0.1.3")
// the correct way will be to apply to each target individually,
// Note: no need to add it to the `common` compilations, as it'd do nothing there
add("kspJvm", "dev.whyoleg.sweetspi:sweetspi-processor:0.1.3")
// if needed, `test` source sets support
add("kspJvmTest", "dev.whyoleg.sweetspi:sweetspi-processor:0.1.3")
// and for each other Kotlin target...
add("kspLinuxX64Test", "dev.whyoleg.sweetspi:sweetspi-processor:0.1.3")
}deprecated from the beginning). But, it works!Of course there could be some bugs, feel free to report them, it would really mean a lot to me! For bugs, questions, and discussions, please use the GitHub Issues
This project is licensed under the Apache 2.0 license. See the LICENSE file for details.