androidgradleandroid-jetpack-composedagger-hilt

Hilt with ksp instead of kapt


how to use hilt with ksp instead of kapt seems like i can't figure it out please let me know what dependencies should i add and how should i add them

dependencies i added:

//hilt
    val hiltVersion = "2.51"  
    implementation("com.google.dagger:hilt-android:$hiltVersion")
    ksp("com.google.dagger:hilt-android-compiler:$hiltVersion")
    ksp("com.google.dagger:hilt-compiler:$hiltVersion")

plugins:

plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.android")
    id ("com.google.dagger.hilt.android")
    id("com.google.devtools.ksp") version "1.9.22-1.0.17"
}

build gradle:

plugins {
    id("com.android.application") version "8.2.2" apply false
    id("org.jetbrains.kotlin.android") version "1.9.0" apply false
    id("com.google.dagger.hilt.android") version "2.51" apply false
    id("com.google.devtools.ksp") version "1.9.22-1.0.17" apply false
}

i tried different hilt versions like 2.48.1 different kotlinCompilerExtensionVersion like 1.5.8

nothing seems to work i've got multiple different errors don't know what i'm doing neither do i know what i'm doing wrong


Solution

  • When using kotlin, ksp and compose you have to keep in mind to use versions that are compatible with each other, otherwise building the project will most likely fail.

    Kotlin and KSP

    Take a look at releases, ksp version always consist of two parts e.g. 1.9.23-1.0.20 where 1.9.23 is kotlin version and 1.0.20 is actual KSP version (i think).

    Kotlin and Compose

    List of compatible versions can be found in Android docs.

    Your case

    Since you are using kotlin 1.9.0 you should use KSP 1.9.0-1.0.13 and kotlinCompilerExtensionVersion 1.5.2. For the dagger it should word fine for version 2.48 and above based on this, so version 2.51 is fine.

    EDIT: For Kotlin version >= 2.0.0

    To ease up this kotlin compose plugin was introduced and have to be used for kotlin >= 2.0.0, but compatible ksp version still has to be applied.

    In the libs.versions.toml file, now we are using same version for kotlin and compose compiler, ksp versions here.

    [versions]
    kotlin = "2.0.0"
    ksp = "2.0.0-1.0.22"
    ...
    
    [plugins]
    kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
    kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
    google-devtools-ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
    

    Add plugin to the top level build.gradle.kts:

    plugins {
        ...
        alias(libs.plugins.kotlin.compose) apply false
        alias(libs.plugins.google.devtools.ksp) apply false
    }
    

    And apply it in the app's build.gradle.kts:

    plugins {
        ...
        alias(libs.plugins.kotlin.android)
        alias(libs.plugins.kotlin.compose)
        alias(libs.plugins.google.devtools.ksp)
    }
    

    Also remove the kotlinCompilerExtensionVersion.