KSP with Kotlin Multiplatform
Here you'll learn how to use Kotlin Symbol Processing (KSP) in a Kotlin Multiplatform project. For a quick start, see an example of a multiplatform project with several targets using KSP in the source repository. The processor in this example generates a Foo class used by the project.
Add KSP to a multiplatform project
In the build.gradle.kts file of the client module (the module that uses the processor), add the appropriate KSP processor dependency for each target that requires symbol processing:
<Target>is one of the targets used in your multiplatform project.<processor>is a Gradle project path. It can be:a specific directory in your project that contains the logic for your symbol processor:
add("kspJvm", project(":local-processor"))an external processor such as Room:
add("kspJvm", "androidx.room:room-compiler:2.6.1")
Use mutiple processors in a single target
You can add more than one processor to a target:
Use the same processor in multiple targets
You can add the same processor to more than one target:
If you have many iOS targets, you can avoid repetition by looping:
Configure KSP for test compilations
To run KSP during test compilation, add the processor to the corresponding test configurations:
For Android host and device tests, KSP derives configuration names from the corresponding source set names:
Find KSP configuration names
KSP derives configuration names from the Kotlin Multiplatform source sets. To view the complete list of KSP configurations for a module, run:
Look for the configuration names that correspond to your target source sets.
Compilation and processing
In a multiplatform project, Kotlin creates a separate compilation for each target and source set, such as main andtest. For each Kotlin compilation task with one or more configured KSP processors, KSP creates a corresponding symbol processing task.
The example project defines six targets. Each target has main and test compilations, resulting in the following compilation and symbol processing tasks:
JVM:
jvmMainandjvmTestJS:
jsMainandjsTestLinuxX64:
linuxX64MainandlinuxX64TestAndroidNativeX64:
androidNativeX64MainandandroidNativeX64TestAndroidNativeArm64:
androidNativeArm64MainandandroidNativeArm64TestMingwX64:
mingwX64MainandmingwX64Test
In the example's workload/build.gradle.kts file, KSP dependencies are declared for the following configurations:
kspJvmandkspJvmTestkspJsandkspJsTestkspAndroidNativeX64andkspAndroidNativeX64TestkspAndroidNativeArm64andkspAndroidNativeArm64TestkspLinuxX64kspMingwX64
KSP creates a symbol processing task for each configuration where a KSP dependency is declared. In this example, the project creates at least 12 Kotlin compilation tasks and 10 symbol processing tasks. The remaining compilations don't have corresponding KSP tasks because KSP isn't configured for them.