Search code examples
javaandroidkotlinksp

Running KSP to replace Kapt in android project


I'm migrating from kapt to ksp , got few libraries that i need to migrate but upon building project , it threw few errors , I would be happy to get some help , Thank you


[versions]
agp = "8.4.1"
kspPlugin = "1.9.0-1.0.12"
androidxWorkRuntimeKtx = "2.10.0"
converterGson = "2.3.0"
hiltAndroid = "2.52"
hiltWork = "1.2.0"
kotlin = "1.9.0"
coreKtx = "1.15.0"
junit = "4.13.2"
junitVersion = "1.2.1"
espressoCore = "3.6.1"
appcompat = "1.7.0"
lifecycleViewmodelKtx = "2.8.7"
loggingInterceptor = "4.6.0"
material = "1.13.0-alpha07"
activity = "1.9.3"
constraintlayout = "2.2.0"
navigationFragmentVersion = "2.8.3"
retrofit = "2.7.1"
roomRuntime = "2.6.1"
workRuntimeKtx = "2.10.0"

[libraries]
androidx-activity-ktx = { module = "androidx.activity:activity-ktx", version.ref = "activity" }
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
androidx-hilt-compiler = { module = "androidx.hilt:hilt-compiler", version.ref = "hiltWork" }
androidx-hilt-work = { module = "androidx.hilt:hilt-work", version.ref = "hiltWork" }
androidx-lifecycle-viewmodel-ktx = { module = "androidx.lifecycle:lifecycle-viewmodel-ktx", version.ref = "lifecycleViewmodelKtx" }
androidx-navigation-ui = { module = "androidx.navigation:navigation-ui", version.ref = "navigationFragmentVersion" }
androidx-room-compiler = { module = "androidx.room:room-compiler", version.ref = "roomRuntime" }
androidx-room-ktx = { module = "androidx.room:room-ktx", version.ref = "roomRuntime" }
androidx-room-runtime = { module = "androidx.room:room-runtime", version.ref = "roomRuntime" }
androidx-work-runtime-ktx = { module = "androidx.work:work-runtime-ktx", version.ref = "workRuntimeKtx" }
androidx-work-runtime-ktx-v271 = { module = "androidx.work:work-runtime-ktx", version.ref = "androidxWorkRuntimeKtx" }
com-google-dagger-hilt-compiler2 = { module = "com.google.dagger:hilt-compiler", version.ref = "hiltAndroid" }
converter-gson = { module = "com.squareup.retrofit2:converter-gson", version.ref = "converterGson" }
google-hilt-compiler = { module = "com.google.dagger:hilt-compiler", version.ref = "hiltAndroid" }
hilt-android = { module = "com.google.dagger:hilt-android", version.ref = "hiltAndroid" }
hilt-android-gradle-plugin = { module = "com.google.dagger:hilt-android-gradle-plugin", version.ref = "hiltAndroid" }
hilt-android-testing = { module = "com.google.dagger:hilt-android-testing", version.ref = "hiltAndroid" }
hilt-compiler = { module = "com.google.dagger:hilt-compiler", version.ref = "hiltAndroid" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
logging-interceptor = { module = "com.squareup.okhttp3:logging-interceptor", version.ref = "loggingInterceptor" }
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
androidx-activity = { group = "androidx.activity", name = "activity", version.ref = "activity" }
androidx-constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "constraintlayout" }
navigation-fragment = { module = "androidx.navigation:navigation-fragment", version.ref = "navigationFragmentVersion" }
retrofit = { module = "com.squareup.retrofit2:retrofit", version.ref = "retrofit" }

[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
jetbrains-kotlin-kotlin = { id = "org.jetbrains.kotlin.kapt", version.ref = "kotlin" }
google-dagger-hilt-android = { id = "com.google.dagger.hilt.android", version.ref = "hiltAndroid" }
ksp = { id = "com.google.devtools.ksp", version.ref="kspPlugin" }



plugins {
   alias(libs.plugins.android.application)
   alias(libs.plugins.jetbrains.kotlin.android)
   alias(libs.plugins.jetbrains.kotlin.kotlin)
   alias(libs.plugins.google.dagger.hilt.android)
   alias(libs.plugins.ksp)
}

android {
   namespace = "com.my.newsapp"
   compileSdk = 35

   defaultConfig {
       applicationId = "com.my.newsapp"
       minSdk = 24
       targetSdk = 35
       versionCode = 1
       versionName = "1.0"

       testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

   }

   buildFeatures {
       viewBinding = true
       dataBinding = true
       buildConfig = true
   }

   buildTypes {

       debug {
           buildConfigField(type = "String", name = "API_URL" , value = "\"https://newsapi.org/v2/\"")
           buildConfigField(type = "String", name = "API_KEY" , value = "\"--redacted--\"")
       }

       release {

           buildConfigField(type = "String", name = "API_URL" , value = "\"https://newsapi.org/v2/\"")
           buildConfigField(type = "String", name = "API_KEY" , value = "\"--redacted--\"")
           isMinifyEnabled = 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"
   }

   kapt {
       correctErrorTypes  = true
   }

}

dependencies {
   ksp (libs.androidx.hilt.compiler)
   implementation (libs.androidx.hilt.work)
   implementation (libs.androidx.work.runtime.ktx.v271)
   implementation (libs.androidx.room.runtime)
   ksp (libs.androidx.room.compiler)
   implementation (libs.androidx.room.ktx)
   implementation (libs.navigation.fragment)
   implementation (libs.androidx.navigation.ui)
   implementation (libs.androidx.work.runtime.ktx)
   implementation (libs.androidx.activity.ktx)
   implementation (libs.androidx.lifecycle.viewmodel.ktx)
   implementation (libs.retrofit)
   implementation (libs.converter.gson)
   implementation (libs.logging.interceptor)
   implementation (libs.hilt.android)
   ksp (libs.hilt.compiler)
   androidTestImplementation  (libs.hilt.android.testing)
   kaptAndroidTest (libs.google.hilt.compiler)
   testImplementation (libs.hilt.android.testing)
   kspTest (libs.com.google.dagger.hilt.compiler2)
   implementation(libs.androidx.core.ktx)
   implementation(libs.androidx.appcompat)
   implementation(libs.material)
   implementation(libs.androidx.activity)
   implementation(libs.androidx.constraintlayout)
   testImplementation(libs.junit)
   androidTestImplementation(libs.androidx.junit)
   androidTestImplementation(libs.androidx.espresso.core)
}


// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    dependencies {
        classpath (libs.hilt.android.gradle.plugin)
    }
}
plugins {
    alias(libs.plugins.android.application) apply false
    alias(libs.plugins.jetbrains.kotlin.android) apply false
    alias(libs.plugins.jetbrains.kotlin.kotlin) apply false
    alias(libs.plugins.google.dagger.hilt.android) apply false
    alias(libs.plugins.ksp) apply false
}


java.lang.NoSuchMethodError: 'kotlin.KotlinVersion com.google.devtools.ksp.processing.SymbolProcessorEnvironment.getKspVersion()'
   at dagger.spi.internal.shaded.androidx.room.compiler.processing.ksp.KspStarTypeArgumentType.<init>(KspStarTypeArgumentType.kt:43)
   at dagger.spi.internal.shaded.androidx.room.compiler.processing.ksp.KspStarTypeArgumentType.<init>(KspStarTypeArgumentType.kt:28)
   at dagger.spi.internal.shaded.androidx.room.compiler.processing.ksp.KspProcessingEnv.wrap(KspProcessingEnv.kt:281)
   at dagger.spi.internal.shaded.androidx.room.compiler.processing.ksp.KspType$typeArguments$2.invoke(KspType.kt:198)
   at dagger.spi.internal.shaded.androidx.room.compiler.processing.ksp.KspType$typeArguments$2.invoke(KspType.kt:194)
   at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
   at dagger.spi.internal.shaded.androidx.room.compiler.processing.ksp.KspType.getTypeArguments(KspType.kt:194)


Solution

  • In your version catalog:

    • Remove the following libraries:

      androidx-hilt-compiler
      com-google-dagger-hilt-compiler2
      google-hilt-compiler
      hilt-android-gradle-plugin
      hilt-compiler
      
    • Add the following library:

      hilt-android-compiler = { module = "com.google.dagger:hilt-android-compiler", version.ref = "hiltAndroid" }
      
    • Remove the kapt plugin jetbrains-kotlin-kotlin

    In your top-level gradle file remove the following:

    • The entire buildscript block
    • The kapt plugin alias(libs.plugins.jetbrains.kotlin.kotlin) apply false

    In your module-level gradle file:

    • Remove the kapt plugin alias(libs.plugins.jetbrains.kotlin.kotlin)

    • Remove the kapt block (the last element of the android block)

    • Remove the following from the dependencies block:

      ksp (libs.androidx.hilt.compiler)
      ksp (libs.hilt.compiler)
      kaptAndroidTest (libs.google.hilt.compiler)
      kspTest (libs.com.google.dagger.hilt.compiler2)
      
    • Add the following to the dependencies block:

      ksp(libs.hilt.android.compiler)
      kspTest(libs.hilt.android.compiler)
      

    That's it, after a Gradle sync you should be able to build the app again.