21

I am following the android developers tutorial to implement a local room database.

I am currently working on the Setup therefore changing my dependencies. But once I sync the app build.gradle it throws an error.

A problem occurred evaluating project ':app'.
> Could not find method ksp() for arguments [androidx.room:room-compiler:2.3.0] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

How can I point it to the ksp() method?

Previousely I had the same problem with kapt() but resolved it by adding id kotlin-kapt but I have not found the dependency needed for ksp() yet.

My apps build.gradle looks like this. The respective part is in the lower part of the code snippet and marked with //rooom <---------

    plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-android-extensions'
    //Room
    id 'kotlin-kapt'

}



android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.example.testapplication"
        minSdkVersion 24
        targetSdkVersion 30
        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 "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.2.0'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'androidx.cardview:cardview:1.0.0' //for the popup window

    //Room <------------------------------
    def room_version = "2.3.0"

    implementation "androidx.room:room-runtime:$room_version"
    implementation "androidx.legacy:legacy-support-v4:1.0.0"
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.0.0'

    // To use Kotlin annotation processing tool (kapt)
    kapt "androidx.room:room-compiler:$room_version"
    // To use Kotlin Symbolic 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 - Test helpers
    testImplementation "androidx.room:room-testing:$room_version"

}
a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
Magnus Offermanns
  • 410
  • 1
  • 3
  • 12

3 Answers3

30

First, you need to add ksp plugin repo gradlePluginPortal In your project build.gradle

buildscript {
ext.ksp_version = "1.5.31-1.0.0"
ext.kotlin_version = "1.5.31"
repositories {
    maven{ url 'https://dl.bintray.com/kotlin/kotlin-eap'}
    maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
    mavenCentral()
    jcenter() // Warning: this repository is going to shut down soon
    gradlePluginPortal()
    google()

}
dependencies {
    classpath "com.android.tools.build:gradle:7.0.2"
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    //other dependencies here
}

}

second, you need to enable ksp plugin In your app build.gradle

plugins {
id 'com.android.application'
id 'kotlin-android'
id 'com.google.devtools.ksp' version "$ksp_version"
}
Islam Assem
  • 1,376
  • 13
  • 21
  • 8
    I was stuck trying to use KSP for hours. This answer is what solved it for me. Particularly the line `id 'com.google.devtools.ksp' version "$ksp_version"`. Google's own docs don't tell you to include the ksp version. – John Oct 09 '21 at 02:41
  • Glad to here that my answer helped you :) – Islam Assem Oct 09 '21 at 11:49
  • 1
    If you are compiling an android library ("com.android.library") consider updating to the version mentioned in this response, "1.5.31-1.0.0" of KSP & "1.5.31" of Kotlin, this due https://github.com/google/ksp/issues/314 – Akhha8 Oct 30 '21 at 01:35
11

First of all you need to choose only one of these:

  // To use Kotlin annotation processing tool (kapt)
kapt "androidx.room:room-compiler:$room_version"
// To use Kotlin Symbolic Processing (KSP)
ksp "androidx.room:room-compiler:$room_version"

Once you choose it, add plugin. I.e. id 'kotlin-kapt'

Vytenis Bučius
  • 147
  • 1
  • 4
1

I believe that you need to follow the quickstart guide e.g. the dependency is ang the lines of :-

implementation("com.google.devtools.ksp:symbol-processing-api:1.5.0-1.0.0-alpha10")

See https://github.com/google/ksp/blob/main/docs/quickstart.md

You may also wish to consider looking at:

MikeT
  • 51,415
  • 16
  • 49
  • 68
  • It seems that one has to do quite some additional steps to run a room database with ksp. Kapt runs without creating a gradle project and stuff. So I will just comment ksp and work on with kapt. When the project is done I will change it. Thank you very much for the help though, did not spot this quickstart guide :) – Magnus Offermanns May 16 '21 at 16:36