Kapt to KSP migration errors
Asked Answered
A

3

19

I am getting an error message when I try to migrate an android project using kapt to KSP.

Error message

Unable to find method ''void org.jetbrains.kotlin.gradle.tasks.KotlinCompile.<init>(org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions)''
'void org.jetbrains.kotlin.gradle.tasks.KotlinCompile.<init>(org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions)'

Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)

Re-download dependencies and sync project (requires network)
The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem.

Stop Gradle build processes (requires restart)
Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project.

In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.

Currently, the following libraries are using kapt.

  1. Moshi
  2. Hilt
  3. Room

build.gradle

plugins {
    id "com.android.application"
    id "kotlin-android"
    id "kotlin-kapt"
    id "dagger.hilt.android.plugin"
    id "com.google.devtools.ksp" version "1.6.10-1.0.4"
}

dependencies {

    // Hilt
    implementation "com.google.dagger:hilt-android:$rootProject.hiltVersion"
    kapt "com.google.dagger:hilt-compiler:$rootProject.hiltVersion"
    implementation "androidx.hilt:hilt-lifecycle-viewmodel:$rootProject.hiltLifecycleViewModelVersion"
    kapt "androidx.hilt:hilt-compiler:$rootProject.hiltCompilerVersion"

    // KSP
    // implementation "com.google.devtools.ksp:symbol-processing-api:1.6.10-1.0.4"

    // Moshi
    implementation "com.squareup.moshi:moshi:$rootProject.moshiVersion"
    implementation "com.squareup.moshi:moshi-kotlin:$rootProject.moshiKotlinVersion"
    kapt "com.squareup.moshi:moshi-kotlin-codegen:$rootProject.moshiKotlinCodegenVersion"
    // ksp "com.squareup.moshi:moshi-kotlin-codegen:1.13.0"

    // Room
    implementation "androidx.room:room-runtime:$rootProject.roomVersion"
    implementation "androidx.room:room-ktx:$rootProject.roomVersion"
    annotationProcessor "androidx.room:room-compiler:$rootProject.roomVersion"
    ksp "androidx.room:room-compiler:$rootProject.roomVersion"
    androidTestImplementation "androidx.room:room-testing:$rootProject.roomVersion"
}

For more details, The repo is hosted here - https://github.com/Abhimanyu14/finance-manager

Avi answered 25/6, 2022 at 6:58 Comment(0)
A
19

As mentioned in the question, I am using the following libraries that are using kapt.

  1. Moshi
  2. Hilt
  3. Room

From the KSP Docs, I found out Hilt is not yet supported. (as of 25-06-2022).

So, I would need to use both kapt and hilt in my project.


The following build.gradle does that,

plugins {
    id "kotlin-kapt"
    id "dagger.hilt.android.plugin"
    id "com.google.devtools.ksp" version "1.6.21-1.0.6"
}

// Hilt
implementation "com.google.dagger:hilt-android:$rootProject.hiltVersion"
kapt "com.google.dagger:hilt-compiler:$rootProject.hiltVersion"
//ksp "com.google.dagger:hilt-compiler:$rootProject.hiltVersion"
implementation "androidx.hilt:hilt-lifecycle-viewmodel:$rootProject.hiltLifecycleViewModelVersion"
kapt "androidx.hilt:hilt-compiler:$rootProject.hiltCompilerVersion"
// ksp "androidx.hilt:hilt-compiler:$rootProject.hiltCompilerVersion"

// KSP
implementation "com.google.devtools.ksp:symbol-processing-api:1.6.21-1.0.6"

// Moshi
implementation "com.squareup.moshi:moshi:$rootProject.moshiVersion"
implementation "com.squareup.moshi:moshi-kotlin:$rootProject.moshiKotlinVersion"
// kapt "com.squareup.moshi:moshi-kotlin-codegen:$rootProject.moshiKotlinCodegenVersion"
ksp "com.squareup.moshi:moshi-kotlin-codegen:1.13.0"preferences:$rootProject.datastorePreferencesVersion"

// Room
implementation "androidx.room:room-runtime:$rootProject.roomVersion"
implementation "androidx.room:room-ktx:$rootProject.roomVersion"
annotationProcessor "androidx.room:room-compiler:$rootProject.roomVersion"
// kapt "androidx.room:room-compiler:$rootProject.roomVersion"
ksp "androidx.room:room-compiler:$rootProject.roomVersion"
androidTestImplementation "androidx.room:room-testing:$rootProject.roomVersion"

Library versions

roomVersion = "2.5.0-alpha02"
moshiVersion = "1.13.0"
hiltVersion = "2.42"
hiltPluginVersion = "2.40.1"
hiltLifecycleViewModelVersion = "1.0.0-alpha03"
hiltCompilerVersion = "1.0.0"

Also encountered this error - Room - Schema export directory is not provided to the annotation processor so we cannot export the schema

And this solution helped - https://mcmap.net/q/73360/-room-schema-export-directory-is-not-provided-to-the-annotation-processor-so-we-cannot-export-the-schema

ksp {
    arg('room.schemaLocation', "$projectDir/schemas")
}
Avi answered 25/6, 2022 at 16:52 Comment(2)
UPD: KSP alpha-support released in Dagger/Hilt 2.48.Picture
@Avi But how to config it when I'm using Gradle Catalogs?Violence
B
5

I had the same problem, It seems that hilt is not supporting KSP yet, so include it in your gradle file

plugin {
    ...
    id "kotlin-kapt"
    ...
}

and replace

 ksp 'com.google.dagger:hilt-compiler:2.44'

with

 kapt 'com.google.dagger:hilt-compiler:2.44'

It worked in my case :)

Blubbery answered 24/2, 2023 at 14:24 Comment(0)
A
2

Only dagger version 2.48 or above is supported by ksp according to the dagger doc https://dagger.dev/dev-guide/ksp.

Above answered 31/3 at 14:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.