Setup
Multiplatform
You need to enable KSP. Add according to the platforms you need.
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.3.0")
}
}
}
}
dependencies {
// Add it according to your targets.
+ val processor = "com.moriatsushi.koject:koject-processor-app:1.3.0"
+ add("kspAndroid", processor)
+ add("kspJvm", processor)
+ add("kspJs", processor)
+ add("kspIosX64", processor)
+ add("kspIosArm64", processor)
}
Single platform
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.3.0")
+ ksp("com.moriatsushi.koject:koject-processor-app:1.3.0")
}
Library module
In the library module, use koject-processor-lib
instead of koject-processor-app
to prevent container generation in the library module.
To enable the collection of transitive dependencies, set the moduleName
property.
Multiplatform
build.gradle.kts
dependencies {
// Add it according to your targets.
- val processor = "com.moriatsushi.koject:koject-processor-app:1.3.0"
+ val processor = "com.moriatsushi.koject:koject-processor-lib:1.3.0"
add("kspAndroid", processor)
add("kspJvm", processor)
add("kspJs", processor)
add("kspIosX64", processor)
add("kspIosArm64", processor)
}
+ ksp {
+ arg("moduleName", project.name)
+ }
Single platform
build.gradle.kts
dependencies {
implementation("com.moriatsushi.koject:koject-core:1.3.0")
- ksp("com.moriatsushi.koject:koject-processor-app:1.3.0")
+ ksp("com.moriatsushi.koject:koject-processor-lib:1.3.0")
}
+ ksp {
+ arg("moduleName", project.name)
+ }
Android
For Android applications, additional functionality is available by adding the following packages.
dependencies {
// Inject Application / Context (Recommended)
implementation("com.moriatsushi.koject:koject-android-core:1.3.0")
// Activity support
implementation("com.moriatsushi.koject:koject-android-activity:1.3.0")
// Fragment support
implementation("com.moriatsushi.koject:koject-android-fragment:1.3.0")
// ViewModelFactory only
implementation("com.moriatsushi.koject:koject-android-viewmodel:1.3.0")
}
Compose
If you are using Jetpack Compose or Compose Multiplatform, the following package is available.
dependencies {
implementation("com.moriatsushi.koject:koject-compose-core:1.3.0")
}
To inject Android ViewModel into Composable, use the following package.
dependencies {
// Inject ViewModel into Composable (Android only)
implementation("com.moriatsushi.koject:koject-compose-viewmodel:1.3.0")
}
Version Catalog
Copy the following snippets if you are using gradle verion catalog.
libs.versions.toml
[versions]
koject = "1.3.0"
[libraries]
koject-core = { group = "com.moriatsushi.koject", name = "koject-core", version.ref = "koject" }
koject-test = { group = "com.moriatsushi.koject", name = "koject-test", version.ref = "koject" }
koject-processor-app = { group = "com.moriatsushi.koject", name = "koject-processor-app", version.ref = "koject" }
koject-processor-lib = { group = "com.moriatsushi.koject", name = "koject-processor-lib", version.ref = "koject" }
koject-android-core = { group = "com.moriatsushi.koject", name = "koject-android-core", version.ref = "koject" }
koject-android-activity = { group = "com.moriatsushi.koject", name = "koject-android-activity", version.ref = "koject" }
koject-android-fragment = { group = "com.moriatsushi.koject", name = "koject-android-fragment", version.ref = "koject" }
koject-android-viewmodel = { group = "com.moriatsushi.koject", name = "koject-android-viewmodel", version.ref = "koject" }
koject-compose-core = { group = "com.moriatsushi.koject", name = "koject-compose-core", version.ref = "koject" }
koject-compose-viewmodel = { group = "com.moriatsushi.koject", name = "koject-compose-viewmodel", version.ref = "koject" }