Koject is a DI Container libreary for Kotlin Multiplatform.
fun main() {
Koject.start()
val controller = inject<Controller>()
}
@Singleton
@Provides
class Api
@Singleton
@Provides
fun createDB(): DB {
return DB.create()
}
@Singleton
@Provides
class Repository(
private val api: Api,
private val db: DB,
)
@Provides
fun Controller(
private val repository: Repository
)
- Support Kotlin Multiplatform
- Easy provide with annotations
- Code generation with KSP
- Support multi-module project
You need to enable KSP.
build.gradle.kts
plugins {
kotlin("multiplatform")
+ id("com.google.devtools.ksp") version "<ksp-version>"
}
kotlin {
android()
jvm()
js(IR) {
nodejs()
browser()
}
ios()
sourceSets {
val commonMain by getting {
dependencies {
+ implementation("com.moriatsushi.koject:koject-core:1.0.0-alpha04")
}
}
}
}
dependencies {
// Add it according to your targets.
+ val processor = "com.moriatsushi.koject:koject-processor-app:1.0.0-alpha04"
+ add("kspAndroid", processor)
+ add("kspJvm", processor)
+ add("kspJs", processor)
+ add("kspIosX64", processor)
+ add("kspIosArm64", processor)
}
Inject can also be used on a single platform.
build.gradle.kts
plugins {
kotlin("<target>")
+ id("com.google.devtools.ksp") version "<ksp-version>"
}
dependencies {
+ implementation("com.moriatsushi.koject:koject-core:1.0.0-alpha04")
+ ksp("com.moriatsushi.koject:koject-processor-app:1.0.0-alpha04")
}
Use koject-processor-lib
to avoid generating Container
in library modules.
build.gradle.kts (Multiplatform)
dependencies {
// Add it according to your targets.
- val processor = "com.moriatsushi.koject:koject-processor-app:1.0.0-alpha04"
+ val processor = "com.moriatsushi.koject:koject-processor-lib:1.0.0-alpha04"
add("kspAndroid", processor)
add("kspJvm", processor)
add("kspJs", processor)
add("kspIosX64", processor)
add("kspIosArm64", processor)
}
build.gradle.kts (single platform)
dependencies {
implementation("com.moriatsushi.koject:koject-core:1.0.0-alpha04")
- ksp("com.moriatsushi.koject:koject-processor-app:1.0.0-alpha04")
+ ksp("com.moriatsushi.koject:koject-processor-lib:1.0.0-alpha04")
}
Add @Provides
annotation to the class you want to provide.
@Provides
class Repository
@Provides
class Controller(
private val repository: Repository
)
You can get an instance using inject
after calling Koject.start()
.
fun main() {
Koject.start()
val controller = inject<Controller>()
}
Any types can be provided from top-level functions with a @Provides
annotation. This is useful when provide outside modules classes.
@Provides
fun createDB(): DB {
return DB.create()
}
You can also provide from object functions.
object DBFactory {
@Provides
fun create(): DB {
return DB.create()
}
}
By adding the @Singleton
annotation, the instance will be created only once and reused within the application.
@Singleton
@Provides
class Api
@Singleton
@Provides
fun createDB(): DB {
return DB.create()
}
Note that you can't inject a normally scope type into a singleton scope type.
@Provides
class NormalScope
@Singleton
@Provides
class SingletonScope(
// cannot inject!
private val normal: NormalScope
)
This library is incomplete and the following features will be added later.