1. libs.versions.toml
[versions]
hilt = "2.48"

[libraries]
hilt-android = { group = "com.google.dagger", name = "hilt-android", version.ref = "hilt" }
hilt-compiler = { group = "com.google.dagger", name = "hilt-compiler", version.ref = "hilt" }

[plugins]
hilt = { id = "com.google.dagger.hilt.android", version.ref = "hilt" }
kapt = { id = "org.jetbrains.kotlin.kapt" }
  1. 模块级 build.gradle.kts
plugins {
    alias(libs.plugins.hilt)
    alias(libs.plugins.kapt)
}

dependencies {
    implementation(libs.hilt.android)
    kapt(libs.hilt.compiler)
}
  • 在 Android 项目中,使用了 Hilt,构建项目时,出现如下错误信息
Unable to find method ''java.lang.String com.squareup.javapoet.ClassName.canonicalName()''
'java.lang.String com.squareup.javapoet.ClassName.canonicalName()'

Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)

Re-download dependencies and sync project (requires network)
The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem.

Stop Gradle build processes (requires restart)
Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project.

In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.
处理策略
  • 在项目级 build.gradle.kts 中,添加如下内容
plugins {
    alias(libs.plugins.hilt) apply false
}
Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐