Could not find method ksp() for arguments

18.9k views Asked by At

I just started learning about Android in Kotlin and I was creating a note application just for learning purposes. I wanted to use Room to store saved notes in my database, so i was checking out the developer.android tutorials and We have some necessary dependencies which we have to add in our build.gradle(Module) as mentioned on developer.android.com https://developer.android.com/training/data-storage/room#kts but when i add these dependencies i am having an error which i'm not able to figure out

Could not find method ksp() for arguments [androidx.room:room-compiler:2.4.3] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler

I was having the similar error with kapt, but i solved it by adding the necessary plugin in my build.gradle(Module).For now i am removing this implementation from my build.gradle and moving ahead with the test project, but any help would be appreciated, thankyou.

dependencies mentioned in the developer.android.com

dependencies {
    val 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")
    // To use Kotlin Symbol Processing (KSP)
    ksp("androidx.room:room-compiler:$room_version")

    // optional - Kotlin Extensions and Coroutines support for Room
    implementation("androidx.room:room-ktx:$room_version")

    // optional - RxJava2 support for Room
    implementation("androidx.room:room-rxjava2:$room_version")

    // optional - RxJava3 support for Room
    implementation("androidx.room:room-rxjava3:$room_version")

    // optional - Guava support for Room, including Optional and ListenableFuture
    implementation("androidx.room:room-guava:$room_version")

    // optional - Test helpers
    testImplementation("androidx.room:room-testing:$room_version")

    // optional - Paging 3 Integration
    implementation("androidx.room:room-paging:$room_version")
}


my build.gradle(Module)

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'kotlin-android'
    id 'kotlin-kapt'

}
android {
    namespace 'android.example.mynotes'
    compileSdk 32

    defaultConfig {
        applicationId "android.example.mynotes"
        minSdk 21
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'com.google.android.material:material:1.7.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

    def room_version = "2.4.3"
    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"
    implementation "androidx.room:room-rxjava2:$room_version"
    implementation "androidx.room:room-rxjava3:$room_version"
    implementation "androidx.room:room-guava:$room_version"
    testImplementation "androidx.room:room-testing:$room_version"

    implementation("com.google.devtools.ksp:symbol-processing-api:1.8.0-1.0.8")

    kapt "androidx.room:room-compiler:$room_version"
    ksp("androidx.room:room-compiler:$room_version")

}


my build.gradle(Project)

plugins {
    id 'com.android.application' version '7.3.1' apply false
    id 'com.android.library' version '7.3.1' apply false
    id 'org.jetbrains.kotlin.android' version '1.7.20' apply false
}
5

There are 5 answers

4
Nic On BEST ANSWER

On the project gradle.build file upgrade the kotlin gradle plugin to 1.8.0 in the plugins{ ... } block.


// Project level build file

plugins {
    ...
    id 'org.jetbrains.kotlin.android' version '1.8.0' apply false
}

On the App(mudule) gradle.build file you don't need to set kapt(), just add ksp() in the dependencies{ ... } block and also set the KSP plugin


// App module build file

plugins{
    ...
    id 'com.google.devtools.ksp' version '1.8.0-1.0.8'
}

dependencies {
   
    ...
    // You don't need this
    // kapt("androidx.room:room-compiler:$room_version")

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

}


3
Андрей Утко On

Make sure you're kotlin gradle plugin version is 1.8.0: or higher

classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.0'

In the app level gradle file add dependency

id 'com.google.devtools.ksp' version '1.8.0-1.0.8'

Finally, build your project.

0
BollMose On

You might find the answer from Google codelab

1- add plugin in app/build.gradle

plugins {
    ...
    // ksp for room
    id 'com.google.devtools.ksp' version "1.8.21-1.0.11"
}

2- add dependencies in app/build.gradle

dependencies {
    ...
    def room_version = "2.5.1"
    implementation "androidx.room:room-runtime:$room_version"
    ksp "androidx.room:room-compiler:$room_version"
    implementation "androidx.room:room-ktx:$room_version"
}
0
Tombstone On

First, you need to declare the KSP plugin in your top-level build(Project). gradle.kt file. Make sure that you choose a KSP version aligned with your project's Kotlin version.

If you are using Kotlin - build.gradle.kts(Project level)

plugins {
     id("com.google.devtools.ksp") version "1.8.10-1.0.9" apply false
}

If you are using Groovy- build.gradle(Project level)

plugins {
     id 'com.google.devtools.ksp' version '1.8.10-1.0.9' apply false
}

Then, enable KSP in your module-level or app-level build.gradle.kts file:

Kotlin - build.gradle.kts(module-level or app-level)

plugins {
     id("com.google.devtools.ksp")
}

Groovy - build.gradle(module-level or app-level)

plugins {
    id 'com.google.devtools.ksp'
}

Replace annotation processors with KSP

   //For .kts
dependencies {
    //When you have no dependencies included with kapt in your module anymore, remove the kapt plugin.
    //kapt("androidx.room:room-compiler:2.5.0")
    ksp("androidx.room:room-compiler:2.5.0")
}
//For Groovy
dependencies {
    //When you have no dependencies included with kapt in your module anymore, remove the kapt plugin.
    //kapt 'androidx.room:room-compiler:2.5.0'
    ksp 'androidx.room:room-compiler:2.5.0'
}

You should also remove any leftover configuration related to kapt.

2
Mahdi Zareei On

according to this documentation

First, declare the KSP plugin in your top level build.gradle.kts file. Make sure that you choose a KSP version aligned with your project's Kotlin version

plugins {
   id("com.google.devtools.ksp") version "1.8.10-1.0.9" apply false
}

Then, enable KSP in your module-level build.gradle.kts file:

plugins {
   id("com.google.devtools.ksp")
}