Skip to content

Commit b3e83c8

Browse files
Yash-Gargevant
authored andcommitted
fix(docs): update multiplatform docs
1 parent 8bb648e commit b3e83c8

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

docs/multiplatform.md

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ Add the runtime dependency in commonMain
4040
sourceSets {
4141
commonMain {
4242
dependencies {
43-
implementation("me.tatarka.inject:kotlin-inject-runtime-kmp:0.7.2")
43+
implementation("me.tatarka.inject:kotlin-inject-runtime-kmp:0.8.0")
4444
}
4545
}
4646
}
4747
```
4848

49-
> [!NOTE]
49+
> [!NOTE]
5050
> `kotlin-inject-runtime-kmp` is the same as `kotlin-inject-runtime` aside from adding the `KmpComponentCreate` annotation.
5151
5252
### Adding the compiler dependencies
@@ -64,17 +64,18 @@ Add the compiler dependencies in a top level `dependencies` block
6464
```kotlin
6565
dependencies {
6666
// 1. Configure code generation into the common source set
67-
kspCommonMainMetadata(libs.kotlinInject)
67+
kspCommonMainMetadata(libs.kotlinInject.compiler)
6868

6969
// 2. Configure code generation into each KMP target source set
70-
kspAndroid("me.tatarka.inject:kotlin-inject-compiler-ksp:0.7.2")
71-
kspIosX64("me.tatarka.inject:kotlin-inject-compiler-ksp:0.7.2")
72-
kspIosArm64("me.tatarka.inject:kotlin-inject-compiler-ksp:0.7.2")
73-
kspIosSimulatorArm64("me.tatarka.inject:kotlin-inject-compiler-ksp:0.7.2")
70+
add("kspAndroid", libs.kotlinInject.compiler)
71+
add("kspIosX64", libs.kotlinInject.compiler)
72+
add("kspIosArm64", libs.kotlinInject.compiler)
73+
add("kspIosSimulatorArm64", libs.kotlinInject.compiler)
74+
// add more targets here...
7475
}
7576
```
7677

77-
> [!NOTE]
78+
> [!NOTE]
7879
> Code generation can be configured for both the common source set and all target source sets, but that will likely lead to [Redeclaration errors for @Component when ksp is used for common and target source sets](https://github.com/evant/kotlin-inject/issues/194).
7980
8081
If a `Component` is declared in the common source set, then KSP would typically be configured to generate code in the common source set. This means that the generated code will be accessible from the common source set, and any source set that depends on it.
@@ -83,7 +84,7 @@ However, certain scenarios instead require generating code for all target source
8384

8485
In those cases, while the `Component` might be declared in the common source set, it is not possible to create it, because the `create` functions are declared in each target source set.
8586

86-
> [!NOTE]
87+
> [!NOTE]
8788
> Pre Kotlin 2.0 it is possible to reference the `create` function from the common source set because code from a target source set was visible to the common source set.
8889
> However this can lead to very subtle bugs, and starting from Kotlin 2.0 common source sets can no longer see code from target source sets.
8990
@@ -102,15 +103,15 @@ actual fun createKmp(): MyKmpComponent = MyKmpComponent::class.create()
102103

103104
Creating an `actual fun` for each platform can be tedious, so kotlin-inject provides a `KmpComponentCreate` annotation.
104105

105-
```kotlin
106+
```kotlin
106107
@Component
107108
abstract class MyKmpComponent
108109

109110
@KmpComponentCreate
110111
expect fun createKmp(): MyKmpComponent
111112
```
112113

113-
> [!NOTE]
114+
> [!NOTE]
114115
> Make sure you are using the `kotlin-inject-runtime-kmp` artifact in order to have access to the `KmpComponentCreate` annotation.
115116
116117
kotlin-inject's processor will generate an `actual fun` in each target's source set that calls through to the `create` function for `MyKmpComponent`. The generated code looks like this:
@@ -171,19 +172,16 @@ The only difference is for projects that generate code into each KMP target sour
171172
kotlin {
172173
// add your project's targets here like in the snippet above
173174

174-
configureCommonMainKsp()
175+
commonMain {
176+
kotlin.srcDir("build/generated/ksp/metadata/commonMain/kotlin")
177+
}
175178
}
176179

177-
@OptIn(ExternalVariantApi::class)
178-
fun KotlinMultiplatformExtension.configureCommonMainKsp() {
179-
sourceSets.named("commonMain").configure {
180-
kotlin.srcDir("build/generated/ksp/metadata/commonMain/kotlin")
181-
}
182-
183-
project.tasks.withType(KotlinCompilationTask::class.java).configureEach {
184-
if(name != "kspCommonMainKotlinMetadata") {
185-
dependsOn("kspCommonMainKotlinMetadata")
180+
// KspAATask should be used for KSP2
181+
// For KSP1 use KotlinCompilationTask instead
182+
tasks.withType<KspAATask>().configureEach {
183+
if (name != "kspCommonMainKotlinMetadata") {
184+
dependsOn("kspCommonMainKotlinMetadata")
186185
}
187-
}
188186
}
189187
```

0 commit comments

Comments
 (0)