我正在开发一个 Android 项目,无需使用以下 Hilt 应用程序类即可正常运行:
package com.app.bestride
import android.app.Application
import dagger.hilt.android.HiltAndroidApp
@HiltAndroidApp
class BestRideApplication : Application() {
// Empty for now
}
但是,每当我添加这个类时,我都会遇到以下错误:
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 文件
plugins {
alias(libs.plugins.androidApplication)
alias(libs.plugins.jetbrainsKotlinAndroid)
alias(libs.plugins.daggerHilt)
kotlin("kapt")
}
android {
namespace = "com.app.bestride"
compileSdk = 34
defaultConfig {
applicationId = "com.app.bestride"
minSdk = 24
targetSdk = 34
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary = true
}
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.5.1"
}
packaging {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
}
}
}
dependencies {
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.lifecycle.runtime.ktx)
implementation(libs.androidx.activity.compose)
implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.ui)
implementation(libs.androidx.ui.graphics)
implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.navigation.compose)
implementation(libs.androidx.material3.android)
implementation(libs.play.services.maps)
implementation(libs.maps.compose)
implementation(libs.hilt.android)
implementation(libs.firebase.auth.ktx)
kapt(libs.hilt.android.compiler)
implementation(libs.androidx.hilt.navigation.compose)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
androidTestImplementation(platform(libs.androidx.compose.bom))
androidTestImplementation(libs.androidx.ui.test.junit4)
debugImplementation(libs.androidx.ui.tooling)
debugImplementation(libs.androidx.ui.test.manifest)
}
// Ensure kapt settings are included
kapt {
correctErrorTypes = true
}
这是 toml 文件
[versions]
agp = "8.3.0"
hiltAndroid = "2.44"
hiltNavigationCompose = "1.2.0"
javapoet = "1.13.0"
kotlin = "1.9.0"
coreKtx = "1.12.0"
junit = "4.13.2"
junitVersion = "1.1.5"
espressoCore = "3.5.1"
lifecycleRuntimeKtx = "2.7.0"
activityCompose = "1.8.2"
composeBom = "2023.08.00"
mapsCompose = "4.3.3"
material3Android = "1.2.1"
navigationCompose = "2.7.7"
playServicesMaps = "19.0.0"
firebaseAuthKtx = "23.0.0"
[libraries]
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
androidx-hilt-navigation-compose = { module = "androidx.hilt:hilt-navigation-compose", version.ref = "hiltNavigationCompose" }
hilt-android = { module = "com.google.dagger:hilt-android", version.ref = "hiltAndroid" }
hilt-android-compiler = { module = "com.google.dagger:hilt-android-compiler", version.ref = "hiltAndroid" }
javapoet = { module = "com.squareup:javapoet", version.ref = "javapoet" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
androidx-lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycleRuntimeKtx" }
androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" }
androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" }
androidx-ui = { group = "androidx.compose.ui", name = "ui" }
androidx-ui-graphics = { group = "androidx.compose.ui", name = "ui-graphics" }
androidx-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" }
androidx-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" }
androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" }
androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" }
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
androidx-material3-android = { group = "androidx.compose.material3", name = "material3-android", version.ref = "material3Android" }
androidx-navigation-compose = { group = "androidx.navigation", name = "navigation-compose", version.ref = "navigationCompose" }
maps-compose = { module = "com.google.maps.android:maps-compose", version.ref = "mapsCompose" }
play-services-maps = { group = "com.google.android.gms", name = "play-services-maps", version.ref = "playServicesMaps" }
firebase-auth-ktx = { group = "com.google.firebase", name = "firebase-auth-ktx", version.ref = "firebaseAuthKtx" }
[plugins]
androidApplication = { id = "com.android.application", version.ref = "agp" }
jetbrainsKotlinAndroid = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
daggerHilt = { id = "com.google.dagger.hilt.android", version.ref = "hiltAndroid" }
我尝试过的步骤:
如何解决这个问题?
我也有同样的问题。
尝试过
apply false
.... 好吧,它没有应用该插件。我显然需要该插件来进行依赖注入才能工作。
我将 kotlin 版本更新为
2.0.21
(当前最新),最新的 ksp,最新的 hilt dagger 插件,依赖项 - 相同的结果。
我也注意到了
> Task :app:hiltAggregateDepsDebug FAILED
因此,通过谷歌搜索这个问题,我偶然发现了这个问题。
提供的答案对我有用。
简而言之
将此添加到所有使用柄的模块中。
hilt {
enableAggregatingTask = false
}
附注如果它有所作为,我使用
ksp
而不是 kapt