Question

Fix Could Not Find Method kapt() in Android Gradle Kotlin

kotlinandroidgradleandroid-gradle-pluginandroid-instant-apps

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 implementation or kapt is 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.

Take Quiz

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-android configures Kotlin compilation for an Android module.
  • kotlin-kapt enables Kotlin annotation processing and makes the kapt configuration available.
  • implementation adds code used at runtime or compile time.
  • kapt adds 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:

  1. Gradle creates the module project.
  2. com.android.feature adds Android feature-related configuration.
  3. kotlin-kapt is applied, but the module has not been configured as a Kotlin Android module through kotlin-android.
  4. Gradle evaluates the dependencies block.
  5. It encounters kapt '...'.
  6. Gradle looks for a dependency configuration named kapt.
  7. Because the expected Kotlin Android plugin setup is incomplete for this module, Gradle cannot resolve kapt as 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 @Inject and @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.

Take Quiz

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 pluginMain purposeUse it when
implementationAdds a normal dependency for compiling and running the moduleYour app directly uses the library API
apiAdds a dependency and exposes it to modules that depend on this moduleA library module intentionally exposes the dependency in its public API
annotationProcessorRuns a Java annotation processorThe annotated source is Java, or the processor is configured for Java-only use
kaptRuns Java annotation processors with Kotlin supportKotlin code uses a library that requires a Java annotation processor
kotlin-androidEnables Kotlin support in an Android moduleThe Android module contains Kotlin source files
kotlin-kaptEnables Kotlin annotation processingThe 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
Take Quiz

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:

  • kapt is 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-kapt when Kotlin code needs Java annotation processors.
  • Keep runtime dependencies in implementation and generator dependencies in kapt.
  • 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.

Take Quiz

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.

Take Quiz

Keep learning

Related questions

View more