Hilt with ksp instead of kapt
Asked Answered
E

3

7

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

Edi answered 12/4, 2024 at 11:42 Comment(0)
M
9

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.

Minuscule answered 12/4, 2024 at 12:20 Comment(0)
P
11

At your project level build.gradle add below plugins:

plugins {
    id("org.jetbrains.kotlin.jvm") version "1.9.23"
    id("com.google.devtools.ksp") version "1.9.23-1.0.20"
}

And in build.gradle(app), add this plugin:

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

Dependencies:

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

Make sure you have latest and stable compose version:

In app's build.gradle
composeOptions {
    kotlinCompilerExtensionVersion = "1.5.11"
}
Phenica answered 12/4, 2024 at 12:24 Comment(2)
Miroslav Hybler's answer worked for me but the thing that attracted my attention to your answer is why would i add id("org.jetbrains.kotlin.jvm") instead of id("org.jetbrains.kotlin.android")Edi
No, i didn't asked to remove org.jetbrains.kotlin.android, it should be there!!. I suggested to add org.jetbrains.kotlin.jvm as extra plugin because if you add room_dependency in future in project than it will be required.Phenica
M
9

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.

Minuscule answered 12/4, 2024 at 12:20 Comment(0)
W
2

Kotlin Version = 2.0.10

build.gradle (Project) > plugins

id("com.google.dagger.hilt.android") version "2.51.1" apply false
id("com.google.devtools.ksp") version "2.0.10-1.0.24" apply false

build.gradle (Module) > (1) plugins

id("com.google.dagger.hilt.android")
id("com.google.devtools.ksp")

(2) dependencies

implementation("com.google.dagger:hilt-android:2.51.1")
ksp("com.google.dagger:hilt-compiler:2.51.1")

Links

Dependency injection with Hilt

Compose to Kotlin Compatibility Map

Releases-google/ksp

Wittenburg answered 17/8, 2024 at 8:24 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.