应用了 Hilt Android Gradle 插件,但未找到 com.google.dagger:hilt-android 依赖项

The Hilt Android Gradle plugin is applied but no com.google.dagger:hilt-android dependency was found

我收到此 gradle 同步错误 - 应用了 Hilt Android Gradle 插件但没有 com.google.dagger:hilt-android 依赖关系被发现。 有人可以帮忙吗? :)

我正在使用 kotlin dsl,我在 buildSrc

中有 libs.kt

buildSrc - build.gradle.kts:

plugins {
    `kotlin-dsl`
}

repositories {
    jcenter()
}

libs.kt 在 buildSrc

object libs {
        object hilt {
            private const val dagger_hilt_version = "2.30.1-alpha"
            private const val jetpack_hilt_version = "1.0.0-alpha02"

            const val gradlePlugin = "com.google.dagger:hilt-android-gradle-plugin:$dagger_hilt_version"
            const val android = "com.google.dagger:hilt-android:$dagger_hilt_version"
            const val android_compiler = "com.google.dagger:hilt-android-compiler:$dagger_hilt_version"

            const val jetpack_viewmodel = "androidx.hilt:hilt-lifecycle-viewmodel:$jetpack_hilt_version"
            const val jetpack_compiler = "androidx.hilt:hilt-compiler:$jetpack_hilt_version"
        }

        /* more objects + more constants */

    }

我的应用 build.gradle.kts 看起来像:

plugins {
    id("com.android.application")
    id("kotlin-android")
    id("kotlin-kapt")
    id("dagger.hilt.android.plugin")
    id("androidx.navigation.safeargs.kotlin")
    id("com.google.firebase.crashlytics")
    id("com.google.gms.google-services")

    id("com.google.firebase.firebase-perf")
}

android {
    useLibrary("org.apache.http.legacy")

    compileSdkVersion = "30"

    dependenciesInfo {
        includeInApk = true
        includeInBundle = true
    }

    buildFeatures {
        dataBinding = true
        compose = true
    }

    defaultConfig {   
        vectorDrawables.useSupportLibrary = true

        applicationId = "aa.bb.cc"

        targetSdkVersion(30)
        minSdkVersion(21)

        versionCode = 1
        versionName = "1.0.0"
    }

    signingConfigs {
        register("configReleaseCZ").configure {
            /*hidden part*/
        }
    }

    bundle {
        language {
            enableSplit = false
        }
    }

    buildTypes {
        named("debug").configure {
            isDebuggable = true
            extra.set("enableCrashlytics", false)
        }
        named("release").configure {
            isShrinkResources = true
            isMinifyEnabled = true
            proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
            isDebuggable = false
            extra.set("enableCrashlytics", true)
            signingConfig = signingConfigs.getByName("configReleaseCZ")
        }
    }

    composeOptions {
        kotlinCompilerVersion = libs.kotlin.version
        kotlinCompilerExtensionVersion = libs.compose.version
    }

    lintOptions {
        isAbortOnError = false
    }
}

kapt {
    correctErrorTypes = true
}

dependencies {
    //implementation("com.google.dagger:hilt-android:2.30.1-alpha") 
    //kapt ("com.google.dagger:hilt-android-compiler:2.30.1-alpha") //this doesnt work wither

    implementation(libs.hilt.android)
    kapt(libs.hilt.android_compiler)

    implementation(libs.hilt.jetpack_viewmodel)
    kapt(libs.hilt.jetpack_compiler)

    /* many more dependencies */

}
kapt {
    correctErrorTypes = true
}

我的项目build.gradle.kts:

buildscript {
    repositories {
        google()
        mavenCentral()
        maven(url = "https://jitpack.io")
        maven(url = "https://repo1.maven.org/maven2/")
        maven(url = "https://plugins.gradle.org/m2/")
        maven(url = "https://github.com/userxpro/userx/raw/maven/")
        maven(url = "https://dl.bintray.com/kotlin/kotlin-eap/")
        maven(url = "https://dl.bintray.com/kotlin/kotlinx/")
        maven(url = "https://kotlin.bintray.com/kotlinx")
        maven(url = "https://oss.sonatype.org/content/repositories/snapshots")
        maven(url = "http://storage.googleapis.com/r8-releases/raw")
        gradlePluginPortal()
    }

    dependencies {
        classpath(libs.r8)

        classpath(libs.androidGradlePlugin)
        classpath(libs.kotlin.gradlePlugin)
        classpath(libs.hilt.gradlePlugin)

        classpath("com.google.gms:google-services:4.3.4")
        classpath("com.google.firebase:firebase-crashlytics-gradle:2.4.1")
        classpath("com.google.firebase:perf-plugin:1.3.4")
    }
}

subprojects {
    repositories {
        google()
        mavenCentral()
        maven(url = "https://jitpack.io")
        maven(url = "https://repo1.maven.org/maven2/")
        maven(url = "https://plugins.gradle.org/m2/")
        maven(url = "https://dl.bintray.com/kotlin/kotlin-eap/")
        maven(url = "https://dl.bintray.com/kotlin/kotlinx/")
        maven(url = "https://kotlin.bintray.com/kotlinx")
        maven(url = "https://oss.sonatype.org/content/repositories/snapshots")
        maven(url = "https://github.com/userxpro/userx/raw/maven/")
        jcenter()
    }

    tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
        kotlinOptions {
            // Treat all Kotlin warnings as errors
            //useIR = true

            //allWarningsAsErrors = true

            freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn"

            // Enable experimental coroutines APIs, including Flow
            freeCompilerArgs += "-Xopt-in=kotlinx.coroutines.ExperimentalCoroutinesApi"
            freeCompilerArgs += "-Xopt-in=kotlinx.coroutines.FlowPreview"
            freeCompilerArgs += "-Xopt-in=kotlin.Experimental"
            freeCompilerArgs += "-Xallow-jvm-ir-dependencies"

            // Set JVM target to 1.8
            jvmTarget = "1.8"
        }
    }
}

移除 id("dagger.hilt.android.plugin")

同步gradle

添加 id("dagger.hilt.android.plugin")

在我的案例中,问题与 Hilt library/plugin 和 Android Gradle 插件的不兼容版本有关,这是在我更新到 Android 之后发生的Gradle 7.0.1.

的插件

我从 2.35 -> 2.38.1 更新了 Hilt 并再次编译了应用程序。