sweet-spi
[!CAUTION]
The project is currently not actively maintained, and is in the process of rewriting its logic from KSP to Kotlin Compiler Plugin.
See https://github.com/whyoleg/sweet-spi/issues/21 for current status and future plans.
Simple SPI (Service Provider Interface) for Kotlin Multiplatform (equivalent of JVM's Service Loader)
@Service
interface SimpleService {
fun saySomethingSweet()
}
@ServiceProvider
object SimpleServiceImpl : SimpleService {
override fun saySomethingSweet() {
println("Kotlin is Awesome")
}
}
fun main() {
ServiceLoader.load<SimpleService>().forEach { service ->
service.saySomethingSweet()
}
}
Overview
sweet-spi consists of three parts:
- multiplatform runtime with a tiny public API and all 24 supported targets
- KSP processor which generates a bit of glue code for each target
- Gradle Plugin which simplifies setup of KSP and runtime
Documentation can be found here, or on the website:
Features
- Easy-to-use SPI with just two annotations and two functions
- No additional setup is necessary for consumers, except for depending on your library, of course
- Automatic loading of service providers from any module or library which is available at runtime
- Automatic discovery of services based on the declaration type
- Support for providing services via objects, properties, and no-arg functions
- Support tor providing multiple services via one declaration
- Ability to explicitly specify which services should be provided by the specific service provider
Using in your projects
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.
import dev.whyoleg.sweetspi.gradle.*
plugins {
kotlin("multiplatform") version "2.0.0"
id("com.google.devtools.ksp") version "2.0.0-1.0.24"
id() version
}
kotlin {
withSweetSpi()
jvm()
wasmJs { browser() }
iosArm64()
iosX64()
iosSimulatorArm64()
}
sweet-spi Gradle plugin will automatically add runtime and KSP processor dependencies to appropriate configurations:
Manual setup without sweet-spi Gradle Plugin
As 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:
Implementation details
- on JVM standard ServiceLoader API is used
- on other targets EagerInitialization annotation is
used.
Note: this API is experimental/deprecated for some amount of time (AFAIK it was
deprecated from the beginning). But, it works!
Bugs and Feedback
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
License
This project is licensed under the Apache 2.0 license. See the LICENSE file for details.