KSP with Kotlin Multiplatform
This guide explains how to use Kotlin Symbol Processing (KSP) in a Kotlin Multiplatform project. For a quick start, see the example multiplatform project. It uses KSP with several targets, and its processor generates a Foo class used by the project.
Add KSP to a multiplatform project
To add KSP to the client module (the module that uses the processor):
Open the module's
build.gradle(.kts)file.Add the KSP plugin to the
plugins {}block:// build.gradle.kts plugins { kotlin("multiplatform") version "2.4.21" id("com.google.devtools.ksp") version "2.3.12" }// build.gradle plugins { id 'org.jetbrains.kotlin.multiplatform' version '2.4.21' id 'com.google.devtools.ksp' version '2.3.12' }Add the processor dependency for each target that requires symbol processing:
dependencies { add("ksp<Target>", <processor>) }<Target>is the name of one of the targets used in your multiplatform project, with the first letter capitalized. For example, for theiosArm64target, use thekspIosArm64configuration.<processor>is the processor dependency. It can be:A module 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.room3:room3-compiler:3.0.3")
Use multiple 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 use a loop to avoid repetition:
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 separate compilations for each target, such as main and test. For each Kotlin compilation task with one or more configured 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 has 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.
See also
Example multiplatform project in the KSP repository.
The list of entry points for each platform when running KSP from the command line.