Question
Fix Could Not Find Method kapt() in Android Gradle Kotlin
Question
In an Android project that uses Kotlin and Android Instant Apps, Gradle reports the following error when an annotation processor is declared with kapt:
Could not find method kapt() for arguments [com.github.bumptech.glide:compiler:4.7.1]
on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
The project applies kotlin-kapt in the base feature module and uses dependencies such as Glide, Dagger, ButterKnife, and the Android Lifecycle compiler. Why is the kapt dependency configuration unavailable, and how can it be enabled correctly in the module?
Short Answer
You will learn that kapt is not a built-in Gradle dependency keyword. It is a dependency configuration added by Kotlin Gradle plugins. When Gradle cannot find kapt, the relevant Kotlin plugin setup is missing or has not been applied correctly in the module that declares the dependency. For Android Kotlin modules, applying kotlin-android together with kotlin-kapt enables Kotlin Android compilation and Kotlin annotation processing.
Concept
kapt stands for Kotlin Annotation Processing Tool. It lets Java-based annotation processors work with Kotlin source code.
Libraries such as Dagger, Glide, Room, ButterKnife, and older Lifecycle APIs may include two separate artifacts:
- A runtime or API dependency used by your application.
- A compiler dependency that generates code during the build.
For example:
implementation 'com.github.bumptech.glide:glide:4.7.1'
kapt 'com.github.bumptech.glide:compiler:4.7.1'
Gradle recognizes dependency names such as implementation, api, and testImplementation only when the applied plugins create those configurations. Likewise, Gradle recognizes kapt only after the Kotlin KAPT plugin has created it for that module.
The error Could not find method kapt() does not usually mean that Glide, Dagger, or another processor is invalid. It means Gradle is reading a kapt '...' line, but no configuration named kapt exists in the current module.
In an Android module containing Kotlin code, use the Android Kotlin plugin and the KAPT plugin:
apply plugin: 'com.android.feature'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
Each Gradle module is configured independently. Applying a plugin in the app module does not automatically make its configurations available in a base feature or dynamic feature module.
Mental Model
Think of a Gradle plugin as an extension installed into a workshop.
- Gradle is the workshop.
- A dependency configuration such as
implementationorkaptis a labeled storage shelf. - Applying a plugin installs the shelves that plugin supports.
If you try to put an item on the kapt shelf before the Kotlin annotation-processing plugin has installed that shelf, Gradle says it cannot find it.
Also, every module is a separate workshop room. Installing a shelf in the app room does not install it in the base or feature room.
Syntax and Examples
A typical Kotlin Android module applies both Kotlin Android support and KAPT before declaring kapt dependencies.
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 28
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.github.bumptech.glide:glide:4.7.1'
kapt 'com.github.bumptech.glide:compiler:4.7.1'
}
What each line does:
kotlin-androidconfigures Kotlin compilation for an Android module.kotlin-kaptenables Kotlin annotation processing and makes thekaptconfiguration available.implementationadds code used at runtime or compile time.kaptadds an annotation processor used while building the project.
For the base feature module in the question, the missing plugin is:
apply plugin: 'kotlin-android'
Applied with the existing KAPT plugin:
apply plugin: 'com.android.feature'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
Step by Step Execution
Consider this module:
apply plugin: 'com.android.feature'
apply plugin: 'kotlin-kapt'
dependencies {
kapt 'com.github.bumptech.glide:compiler:4.7.1'
}
During Gradle configuration, this happens:
- Gradle creates the module project.
com.android.featureadds Android feature-related configuration.kotlin-kaptis applied, but the module has not been configured as a Kotlin Android module throughkotlin-android.- Gradle evaluates the
dependenciesblock. - It encounters
kapt '...'. - Gradle looks for a dependency configuration named
kapt. - Because the expected Kotlin Android plugin setup is incomplete for this module, Gradle cannot resolve
kaptas a valid dependency method and stops with the error.
Corrected module setup:
apply plugin: 'com.android.feature'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
dependencies {
implementation 'com.github.bumptech.glide:glide:4.7.1'
kapt 'com.github.bumptech.glide:compiler:4.7.1'
}
Now the Kotlin Android and KAPT plugins establish the Kotlin compilation and annotation-processing setup before Gradle evaluates the kapt dependency.
Real World Use Cases
Annotation processing is commonly used when a library generates source code based on annotations in your Kotlin classes.
- Dependency injection: Dagger can generate factory and component-related classes from annotations such as
@Injectand@Component. - Image loading: Glide's compiler can generate optimized APIs for annotated modules.
- Database access: Room historically uses an annotation processor to generate database implementations and DAO code.
- View binding libraries: Older ButterKnife versions generate binding code from view annotations.
- Metadata generation: Some frameworks scan annotations and generate registries, adapters, serializers, or validation code.
In each case, the application dependency belongs in implementation or api, while the code generator belongs in kapt when it is a Java annotation processor used from Kotlin.
Real Codebase Usage
In real projects, plugin and annotation-processor declarations are usually kept close to the module that owns the annotated Kotlin code.
Configure every relevant module
If :feature contains Kotlin classes annotated for Dagger or Room, configure KAPT in :feature. If :base contains those classes instead, configure it in :base.
// base/build.gradle
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
dependencies {
implementation 'com.google.dagger:dagger:2.15'
kapt 'com.google.dagger:dagger-compiler:2.15'
}
Keep runtime and compiler dependencies paired
implementation 'android.arch.lifecycle:extensions:1.1.1'
kapt 'android.arch.lifecycle:compiler:1.1.1'
The first library provides APIs your code uses. The second runs only during builds to generate source code.
Apply plugins before dependencies
With legacy Groovy Gradle scripts, place apply plugin statements before the dependencies block. This makes configuration availability obvious and avoids ordering confusion.
Prefer the processor required by the library
Do not add a compiler artifact just because a library has one. Check whether the library version and features you use actually require annotation processing. Modern Android libraries may use KSP or other code-generation approaches instead.
Common Mistakes
Assuming plugins apply to all modules
This is incorrect reasoning:
// app/build.gradle
apply plugin: 'kotlin-kapt'
// base/build.gradle
kapt 'com.github.bumptech.glide:compiler:4.7.1'
A plugin applied in :app does not create kapt in :base.
Avoid it: Apply the required Kotlin plugins in every module that uses kapt.
Applying KAPT without Kotlin Android support
For an Android module, this setup can be incomplete:
apply plugin: 'com.android.feature'
apply plugin: 'kotlin-kapt'
Avoid it: Add the Android Kotlin plugin:
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
Using annotationProcessor for Kotlin-generated code
annotationProcessor 'com.google.dagger:dagger-compiler:2.15'
annotationProcessor is generally for Java annotation processing. It does not provide the Kotlin stubs and integration needed for Kotlin source in the same way as KAPT.
Avoid it: Use kapt for Java-based processors that process Kotlin code.
kapt 'com.google.dagger:dagger-compiler:2.15'
Putting compiler artifacts in implementation
implementation 'com.github.bumptech.glide:compiler:4.7.1'
This treats a build-time code generator as a runtime dependency.
Avoid it: Put the compiler artifact in kapt.
kapt 'com.github.bumptech.glide:compiler:4.7.1'
Forgetting the Kotlin Gradle plugin at the project level
The module plugin cannot be resolved unless the project build configuration makes the Kotlin Gradle plugin available.
buildscript {
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
This is especially relevant in older Gradle projects that use apply plugin syntax.
Comparisons
| Configuration or plugin | Main purpose | Use it when |
|---|---|---|
implementation | Adds a normal dependency for compiling and running the module | Your app directly uses the library API |
api | Adds a dependency and exposes it to modules that depend on this module | A library module intentionally exposes the dependency in its public API |
annotationProcessor | Runs a Java annotation processor | The annotated source is Java, or the processor is configured for Java-only use |
kapt | Runs Java annotation processors with Kotlin support | Kotlin code uses a library that requires a Java annotation processor |
kotlin-android | Enables Kotlin support in an Android module | The Android module contains Kotlin source files |
kotlin-kapt | Enables Kotlin annotation processing | The Kotlin module declares KAPT processors |
implementation and kapt often appear together, but they solve different problems:
implementation 'com.google.dagger:dagger:2.15' // APIs used by source code
kapt 'com.google.dagger:dagger-compiler:2.15' // generator run during build
Cheat Sheet
// Project-level build configuration in older Gradle projects
buildscript {
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
// Module-level Android Kotlin setup
apply plugin: 'com.android.library' // or com.android.application / com.android.feature
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
// Normal library + annotation processor
implementation 'group:library:version'
kapt 'group:library-compiler:version'
Quick rules:
kaptis created by a plugin; it is not built into Gradle.- Configure plugins in the same module that declares
kapt. - Android modules with Kotlin normally need
kotlin-android. - Apply
kotlin-kaptwhen Kotlin code needs Java annotation processors. - Keep runtime dependencies in
implementationand generator dependencies inkapt. - A missing
kapt()method usually indicates missing or incompatible module plugin configuration.
FAQ
Why does Gradle say it cannot find method kapt()?
Gradle cannot find a dependency configuration named kapt in the current module. Usually, the Kotlin KAPT setup is missing or incomplete in that module.
Is kapt a Gradle built-in dependency type?
No. It is added by Kotlin's annotation-processing Gradle plugin.
Do I need to apply kotlin-kapt in every module?
Apply it in every module that declares kapt dependencies or contains Kotlin code that must be processed by those processors.
Why do I need kotlin-android in an Android feature module?
It configures the module as an Android module that compiles Kotlin. In the shown Instant App base feature setup, adding it completes the Kotlin plugin configuration needed for KAPT.
Should I use annotationProcessor or kapt in a Kotlin Android project?
Use kapt for Java-based annotation processors that need to process Kotlin source. Use annotationProcessor for Java-only source processing where appropriate.
Do compiler dependencies belong in implementation?
Usually no. A compiler artifact generates code at build time, so it belongs in kapt, while the library API normally belongs in implementation.
Does applying kotlin-kapt in the app module enable it in feature modules?
No. Gradle plugins and dependency configurations are scoped to individual modules.
Is KAPT required by every Kotlin library?
No. Only use it when a library's documentation says its Kotlin integration needs an annotation processor. Some modern code generators use KSP instead.
Mini Project
Description
Create a small Android library-module Gradle configuration that uses a Kotlin class with Dagger annotations. The project demonstrates the difference between a normal dependency and an annotation processor, and it ensures the module exposes the kapt configuration correctly.
Goal
Configure an Android Kotlin module so Dagger's compiler runs through KAPT without a Could not find method kapt() error.
Requirements
Apply the Android library plugin to the module.
Apply the Kotlin Android and Kotlin KAPT plugins.
Add Dagger as an implementation dependency.
Add the matching Dagger compiler artifact with kapt.
Include one Kotlin class that uses a Dagger annotation.
Keep learning
Related questions
Accessing Kotlin Extension Functions from Java
Learn how Kotlin extension functions are compiled and how to call them correctly from Java with clear examples and common pitfalls.
Add a Maven Repository URL in Gradle Kotlin DSL (build.gradle.kts)
Learn how to add Maven Central and a custom Maven repository URL in Gradle Kotlin DSL using build.gradle.kts syntax.
Allow HTTP and HTTPS in Android 9 Pie with Network Security Configuration
Learn how Android 9 Pie handles cleartext HTTP traffic and how to allow HTTP and HTTPS safely using network security config.