148

I'm facing a problem for over 3 days now and I can't solve. since I started to use Kotlin for Android,I stopped using "annotationProcessor" and started using "kapt", all things were working great with kapt until I started to build an Android Instant App, when I add "kapt" to any dependency like Glide or ButterKnife Gradle always displaying error that cound't find method 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.

Here are my project Gradle files

Project gradle file

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.2.71'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

app module gradle file

apply plugin: 'com.android.application'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 28

    defaultConfig {
        applicationId "com.demo.instantapptest.app"
        minSdkVersion 17
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"


    }

    buildTypes {
       release {
           minifyEnabled false
           proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
       }
    }

}

dependencies {
    implementation project(':feature')
    implementation project(':base')
}

base module gradle file

apply plugin: 'com.android.feature'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 28
    baseFeature true
    defaultConfig {
        minSdkVersion 17
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    api 'com.android.support:appcompat-v7:28.0.0'
    api 'com.android.support.constraint:constraint-layout:1.1.3'
    application project(':app')
    feature project(':feature')


    implementation "android.arch.lifecycle:extensions:1.1.1"
    kapt "android.arch.lifecycle:compiler:1.1.1"

    implementation 'com.github.bumptech.glide:glide:4.7.1'
    kapt 'com.github.bumptech.glide:compiler:4.7.1'

    implementation 'com.google.dagger:dagger:2.15.0'
    kapt 'com.google.dagger:dagger-compiler:2.15.0'

    implementation 'com.jakewharton:butterknife:9.0.0-rc2'
    kapt 'com.jakewharton:butterknife-compiler:9.0.0-rc2'

}

feature module gradle file

apply plugin: 'com.android.feature'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 28
    defaultConfig {
        minSdkVersion 17
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation project(':base')
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

}

instantapp gradle file

apply plugin: 'com.android.instantapp'

dependencies {
    implementation project(':feature')
    implementation project(':base')
}

==========

Solved

by adding kotlin-android plugin to the base module

apply plugin: 'kotlin-android'
O.AbedElBaset
  • 1,710
  • 2
  • 13
  • 11

8 Answers8

305

In my case,I was missing

apply plugin: 'kotlin-kapt'
starball
  • 20,030
  • 7
  • 43
  • 238
Mihai
  • 26,325
  • 7
  • 66
  • 81
130

Here is a working solution for the latest version of android studio instead of apply plugin use id 'kotlin-kapt' inside plugins.

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
} 
Md. Shofiulla
  • 2,135
  • 1
  • 13
  • 19
27

In case anybody struggles with Room library:

I tried all above tips and nothing helped, unless I changed this miserable:

ksp("androidx.room:room-compiler:$roomVersion")

into:

annotationProcessor("androidx.room:room-compiler:$roomVersion")

At first, I also changed:

val roomVersion = "2.4.2"

into:

def roomVersion = "2.4.2"

AND THIS FINALLY DID THE JOB !!!

17

I faced this same error when I was trying to add Room library in my android project. As of Aug 17th, 2022, I could solve the issue adding below lines in build.gradle(:app).

In the plugins block I added;

id 'kotlin-kapt'

in the dependencies block, I added;

def room_version = "2.4.3"

implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
// To use Kotlin annotation processing tool (kapt)
kapt "androidx.room:room-compiler:$room_version"

Doing so, I could say goodbye to the "Could not find method kapt() for arguments". Hopefully someone out there could save some time reading this post.

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
D.Y Won
  • 245
  • 3
  • 5
11

So you also forgot to add

apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'

in app level gradel file. Nice.

shekhar g h
  • 1,193
  • 1
  • 9
  • 12
4
Use plugin with this order,

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
apply plugin:  'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'
fazal ur Rehman
  • 330
  • 2
  • 5
3

I faced this

Could not find method kapt() for arguments

error, when I tried to add dependencies for dagger in my project. Here is a working solution for the latest version of Android Studio.

plugins {
   id 'com.android.application'
   id 'org.jetbrains.kotlin.android'
   id 'kotlin-kapt'   //Add this line in the plugins block usually seen at the top of build.gradle
}
Sayooj
  • 766
  • 5
  • 23
-1

Undoubtedly, people added the ksp("androidx.room:room-compiler:$roomVersion") libarary instead of kapt("androidx.room:room-compiler:$roomVersion")

emad
  • 1
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 31 '22 at 01:22