[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Migrate from kapt to KSP\n\n| **Note:** Kapt is now in maintenance mode, and we recommend that you migrate from kapt to KSP for all processors that support it. In most cases, this migration only requires changes to your project's build configuration.\n\n[Kapt (the Kotlin Annotation Processing Tool)](https://kotlinlang.org/docs/kapt.html) lets you use\nJava annotation processors with Kotlin code, even if those processors don't have\nspecific support for Kotlin. This is done by generating Java stubs from your\nKotlin files that the processors can then read. This stub generation is an\nexpensive operation and has a significant impact on build speed.\n\n[KSP (Kotlin Symbol Processing)](https://github.com/google/ksp) is a Kotlin-first alternative to\nkapt. KSP analyzes Kotlin code directly, which is [up to 2x\nfaster](https://android-developers.googleblog.com/2021/09/accelerated-kotlin-build-times-with.html). It also has a better understanding of Kotlin\nlanguage constructs.\n\nYou can run kapt and KSP alongside each other in your project while you're\nmigrating, and the migration can be done module by module, library by library.\n| **Note:** If a module has any kapt processors remaining, stubs are still generated in that module. This means that the majority of performance improvement will occur only when all usages of kapt are removed from the module.\n\nHere's an overview of the migration steps:\n\n1. Check the libraries you use for KSP support\n2. Add the KSP plugin to your project\n3. Replace annotation processors with KSP\n4. Remove the kapt plugin\n\nCheck the libraries you use for KSP support\n-------------------------------------------\n\nTo get started, check if the libraries you're using with kapt already have KSP\nsupport. This is the case for many popular libraries (including\n[Dagger](https://github.com/google/dagger/issues/2349), [Glide](https://bumptech.github.io/glide/doc/download-setup.html#kotlin---ksp), [Room](/jetpack/androidx/releases/room#declaring_dependencies),\nand [Moshi](https://github.com/square/moshi#codegen)), and others are adding support.\n\nYou can check the [list of supported libraries](https://kotlinlang.org/docs/ksp-overview.html#supported-libraries) in the\ndocumentation, or refer to the documentation and issue tracker of the libraries\nyou're using.\n| **Note:** While not a traditionally-included library dependency, [Data Binding](/topic/libraries/data-binding) also uses an annotation processor to provide its functionality, and [KSP support\n| for Data Binding is not planned](https://issuetracker.google.com/issues/173030256#comment10). You can mitigate the impact of kapt on your build by isolating the usages of Data Binding to separate modules.\n\nAdd the KSP plugin to your project\n----------------------------------\n\nFirst, declare the KSP plugin in your top level `build.gradle.kts` file.\nMake sure that you choose a KSP version aligned with your project's Kotlin\nversion. You can find a list of releases on the [KSP GitHub\npage](https://github.com/google/ksp/releases).\n**Note:** The first part of the KSP version must match the version of Kotlin being used in your build. For example, if you're using Kotlin `2.0.21`, the KSP version must be one of the `2.0.21-x.y.z` releases. \n\n### Kotlin\n\n```kotlin\nplugins {\n id(\"com.google.devtools.ksp\") version \"2.0.21-1.0.27\" apply false\n}\n```\n\n### Groovy\n\n```groovy\nplugins {\n id 'com.google.devtools.ksp' version '2.0.21-1.0.27' apply false\n}\n```\n\nThen, enable KSP in your module-level `build.gradle.kts` file: \n\n### Kotlin\n\n```kotlin\nplugins {\n id(\"com.google.devtools.ksp\")\n}\n```\n\n### Groovy\n\n```groovy\nplugins {\n id 'com.google.devtools.ksp'\n}\n```\n\nReplace annotation processors with KSP\n--------------------------------------\n\nWith KSP enabled, you can start replacing usages of kapt with KSP. For a vast\nmajority of libraries, this just requires changing kapt to ksp at the dependency\ndeclaration, as they ship their annotation processor and KSP processor in the\nsame artifact.\n**Note:** Some libraries (such as [Glide](https://bumptech.github.io/glide/doc/download-setup.html#kotlin---ksp)) might require you to change the dependency to a different artifact as well. Make sure to consult their documentation. \n\n### Kotlin\n\n```kotlin\ndependencies {\n kapt(\"androidx.room:room-compiler:2.5.0\")\n ksp(\"androidx.room:room-compiler:2.5.0\")\n}\n```\n\n### Groovy\n\n```groovy\ndependencies {\n kapt 'androidx.room:room-compiler:2.5.0'\n ksp 'androidx.room:room-compiler:2.5.0'\n}\n```\n\nAfter moving to KSP, sync and build your project to see if it still works\ncorrectly.\n\nSome common issues to look out for:\n\n- Some libraries don't support the exact same set of features with kapt and KSP. If your code breaks after migrating, check the library's documentation.\n- KSP has more accurate Kotlin type information than kapt (for example, about nullability), which means that KSP processors can be more precise about type requirements. This might require some fixes in your source code as well, in addition to updating your build files.\n- If you were previously passing in arguments to the annotation processor, you'll likely need to pass in those arguments to KSP now. Note that the format of the arguments might differ between kapt and KSP. See the [KSP\n documentation](https://kotlinlang.org/docs/ksp-quickstart.html#pass-options-to-processors) and consult the documentation of the library you're using to learn more.\n\nRemove the kapt plugin\n----------------------\n\nWhen you have no dependencies included with `kapt` in your module anymore,\nremove the kapt plugin.\n| **Note:** [Data Binding](/topic/libraries/data-binding) also requires kapt to be enabled in the module. In modules where Data Binding is used, kapt can't be removed.\n\nIf it was declared in a plugins block: \n\n### Kotlin\n\n```kotlin\nplugins {\n id(\"org.jetbrains.kotlin.kapt\")\n}\n```\n\n### Groovy\n\n```groovy\nplugins {\n id 'org.jetbrains.kotlin.kapt'\n}\n```\n\nIf it was using the apply plugin syntax using Groovy: \n\n```\napply plugin: 'kotlin-kapt'\n```\n\nYou should also remove any leftover configuration related to kapt, such as: \n\n### Kotlin\n\n```kotlin\n\nkapt {\n correctErrorTypes = true\n useBuildCache = true\n}\n```\n\n### Groovy\n\n```groovy\n\nkapt {\n correctErrorTypes true\n useBuildCache true\n}\n```\n\nAdditional resources\n--------------------\n\n- [KSP documentation on Kotlinlang.org](https://kotlinlang.org/docs/ksp-overview.html)\n- [KSP on GitHub](https://github.com/google/ksp)\n- [kapt on Kotlinlang.org](https://kotlinlang.org/docs/kapt.html) --\\\u003e"]]