You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Android Studio集成Hilt与Jetpack Navigation时需添加指定kapt依赖吗?

问题:集成Hilt与Jetpack Navigation时是否需要添加androidx.hilt:hilt-compiler:1.0.0依赖?

我已阅读官方文档并按照指引修改了根目录build.gradle(Code A)和app/build.gradle(Code B)来使用Hilt实现依赖注入。现在希望将Hilt与Jetpack Navigation库集成,参考文档修改得到Code C,请问是否需要在Code C中添加kapt 'androidx.hilt:hilt-compiler:1.0.0'依赖?

Code A(根目录build.gradle)

plugins {
  ...
  id 'com.google.dagger.hilt.android' version '2.44' apply false
}

Code B(app/build.gradle)

plugins {
    id 'kotlin-kapt'
    id 'com.google.dagger.hilt.android'
}

android {
    ...
}

dependencies {
    implementation "com.google.dagger:hilt-android:2.44"
    kapt "com.google.dagger:hilt-compiler:2.44"
}

Code C(app/build.gradle)

plugins {
  id 'kotlin-kapt'
  id 'com.google.dagger.hilt.android'
}

android {
  ...
}

dependencies {
  implementation "com.google.dagger:hilt-android:2.44"
  kapt "com.google.dagger:hilt-compiler:2.44"
  implementation 'androidx.hilt:hilt-navigation-fragment:1.0.0'
  //  kapt 'androidx.hilt:hilt-compiler:1.0.0'     //是否需要添加?
}

回答

不需要添加kapt 'androidx.hilt:hilt-compiler:1.0.0'这个依赖。

原因如下:

  • hilt-navigation-fragment库的注解处理逻辑已经被你已添加的com.google.dagger:hilt-compiler:2.44完全覆盖,额外添加androidx.hilt:hilt-compiler属于冗余配置,不会带来任何功能上的增益。
  • 从版本兼容性来看,Hilt 2.44与androidx.hilt 1.0.0是兼容的,但重复引入编译器依赖只会增加构建时间,没有实际必要。
  • 官方在集成Hilt与Jetpack Navigation的指引中,也没有要求添加这个额外的kapt依赖,仅需引入hilt-navigation-fragment的implementation依赖即可。

内容的提问来源于stack exchange,提问作者HelloCW

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 00:47:06