DEV Community

Abdul Rehman
Abdul Rehman

Posted on

How to Properly Setup Hilt in Android Jetpack Compose Project in 2025

Direct Answer

Key Points:

  • Research suggests Hilt 2.56 is the latest version, using KSP instead of KAPT for faster annotation processing.
  • It seems likely that you need to update Gradle files to use ksp for Hilt and AndroidX Hilt dependencies.
  • The evidence leans toward including both core Hilt (2.56) and AndroidX Hilt (1.2.0) compilers with KSP for full compatibility.

Project-Level build.gradle.kts Update:

  • Add plugins for Hilt and KSP with versions:
  plugins {
      id("com.android.application") version "8.5.2" apply false
      id("org.jetbrains.kotlin.android") version "2.0.20" apply false
      id("com.google.dagger.hilt.android") version "2.56" apply false
      id("com.google.devtools.ksp") version "1.9.24-1.0.20" apply false
  }
Enter fullscreen mode Exit fullscreen mode

App-Level build.gradle.kts Update:

  • Apply plugins and update dependencies:
  plugins {
      id("com.android.application")
      id("org.jetbrains.kotlin.android")
      id("com.google.dagger.hilt.android")
      id("com.google.devtools.ksp")
  }

  dependencies {
      val hiltVersion = "2.56"
      implementation("com.google.dagger:hilt-android:$hiltVersion")
      ksp("com.google.dagger:hilt-compiler:$hiltVersion")

      val androidxHiltVersion = "1.2.0"
      implementation("androidx.hilt:hilt-work:$androidxHiltVersion")
      ksp("androidx.hilt:hilt-compiler:$androidxHiltVersion")
  }
Enter fullscreen mode Exit fullscreen mode
  • Replace kapt with ksp for all Hilt-related annotation processing.

Notes:

  • Ensure your Kotlin and Android Gradle Plugin versions are compatible with KSP 1.9.24-1.0.20.
  • If using AndroidX Hilt extensions, include both compilers to avoid issues.

Survey Note: Detailed Analysis of Hilt with KSP in 2025

This section provides a comprehensive overview of transitioning Hilt to use KSP instead of KAPT, based on the latest available information as of June 12, 2025. It covers the evolution of Hilt, the shift to KSP, and practical implementation details for Android projects.

Background on Hilt and Dependency Injection

Hilt, built on Dagger, is a dependency injection library recommended for Android apps, simplifying the setup and management of dependencies. It reduces boilerplate code by generating Dagger components automatically. Traditionally, Hilt used KAPT for annotation processing, but with Kotlin's advancements, KSP has emerged as a faster, more efficient alternative.

Transition to KSP: Why and How

KSP (Kotlin Symbol Processing) is designed to leverage Kotlin's compiler infrastructure, offering up to twice the speed of KAPT. This transition is part of Kotlin's push for better performance, especially for annotation-heavy libraries like Hilt. Research suggests that Hilt, starting from version 2.48, supports KSP, with the latest version identified as 2.56 in recent Dagger releases (Releases ยท google/dagger). For AndroidX Hilt extensions, the latest stable version is 1.2.0, supporting KSP as introduced in version 1.1.0-alpha01 (Hilt | Jetpack | Android Developers).

The shift to KSP requires updating Gradle configurations, replacing kapt with ksp for annotation processing. This change is crucial for projects aiming to reduce build times and improve development efficiency.

Implementation Details

To integrate Hilt with KSP, updates are needed in both project-level and app-level build.gradle.kts files. Below is a detailed breakdown:

  • Project-Level build.gradle.kts: This file defines the plugins and their versions for the entire project. For Hilt 2.56 and KSP, the configuration is:
  plugins {
      id("com.android.application") version "8.5.2" apply false
      id("org.jetbrains.kotlin.android") version "2.0.20" apply false
      id("com.google.dagger.hilt.android") version "2.56" apply false
      id("com.google.devtools.ksp") version "1.9.24-1.0.20" apply false
  }
Enter fullscreen mode Exit fullscreen mode

The versions (e.g., 8.5.2 for Android Gradle Plugin, 2.0.20 for Kotlin) should align with your project's setup, but these are based on recent documentation for compatibility with Hilt 2.56 and KSP 1.9.24-1.0.20, as noted in Dagger's release notes.

  • App-Level build.gradle.kts: Here, you apply the plugins and define dependencies. The updated configuration is:
  plugins {
      id("com.android.application")
      id("org.jetbrains.kotlin.android")
      id("com.google.dagger.hilt.android")
      id("com.google.devtools.ksp")
  }

  android {
      // Your existing Android configuration
  }

  dependencies {
      val hiltVersion = "2.56"
      implementation("com.google.dagger:hilt-android:$hiltVersion")
      ksp("com.google.dagger:hilt-compiler:$hiltVersion")

      val androidxHiltVersion = "1.2.0"
      implementation("androidx.hilt:hilt-work:$androidxHiltVersion")
      ksp("androidx.hilt:hilt-compiler:$androidxHiltVersion")
  }
Enter fullscreen mode Exit fullscreen mode

This setup ensures that both core Hilt and AndroidX Hilt extensions are processed with KSP. The ksp configuration replaces kapt, aligning with Hilt's support for KSP as detailed in Dagger's documentation (Dagger KSP).

Compatibility and Considerations

  • Version Compatibility: Hilt 2.56 requires KSP 1.9.24-1.0.20 or higher, as specified in Dagger's release notes. Ensure your Kotlin version (e.g., 2.0.20) and Android Gradle Plugin version (e.g., 8.5.2) are compatible with these requirements.
  • AndroidX Hilt Extensions: If using extensions like hilt-work, include androidx.hilt:hilt-compiler with KSP, as recommended for versions 1.1.0 and above. This ensures all annotations are processed correctly, avoiding potential conflicts with Javac/KAPT processors, as noted in Dagger's KSP documentation.
  • Build Performance: KSP can reduce build times significantly, especially for large projects, making it a worthwhile upgrade from KAPT.

Table: Hilt and KSP Version Compatibility

Component Version Notes
Hilt (core) 2.56 Latest as of Dagger 2.56 release
KSP 1.9.24-1.0.20 Minimum required for Hilt 2.56
AndroidX Hilt (e.g., hilt-work) 1.2.0 Latest stable, supports KSP since 1.1.0
Kotlin 2.0.20 Compatible with KSP 1.9.24-1.0.20
Android Gradle Plugin 8.5.2 Ensure compatibility with Kotlin and KSP

Practical Example

For a project using Hilt with KSP, the migration involves replacing all kapt lines with ksp. For instance, if you had:

dependencies {
    implementation("com.google.dagger:hilt-android:2.48")
    kapt("com.google.dagger:hilt-compiler:2.48")
}
Enter fullscreen mode Exit fullscreen mode

Update to:

dependencies {
    implementation("com.google.dagger:hilt-android:2.56")
    ksp("com.google.dagger:hilt-compiler:2.56")
}
Enter fullscreen mode Exit fullscreen mode

And if using AndroidX Hilt, add:

implementation("androidx.hilt:hilt-work:1.2.0")
ksp("androidx.hilt:hilt-compiler:1.2.0")
Enter fullscreen mode Exit fullscreen mode

Conclusion

The transition to Hilt with KSP is straightforward, requiring updates to Gradle plugins and dependencies. By using Hilt 2.56 and AndroidX Hilt 1.2.0 with KSP, you leverage faster annotation processing, enhancing build performance. Ensure compatibility with your project's Kotlin and Android Gradle Plugin versions, and test thoroughly to confirm all Hilt annotations are processed correctly.

Key Citations

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.