在android中实现hilt库时gradle出错

Ahm*_*lla 6 android gradle android-studio dagger-2 dagger-hilt

我正在尝试在 android studio 版本中实现 hilt2020.1.1 Canary 1但我在 gradle 中收到此错误

Unable to find method ''void com.android.build.api.extension.AndroidComponentsExtension$DefaultImpls.androidTests$default(com.android.build.api.extension.AndroidComponentsExtension, com.android.build.api.extension.VariantSelector, kotlin.jvm.functions.Function1, int, java.lang.Object)''
'void com.android.build.api.extension.AndroidComponentsExtension$DefaultImpls.androidTests$default(com.android.build.api.extension.AndroidComponentsExtension, com.android.build.api.extension.VariantSelector, kotlin.jvm.functions.Function1, int, java.lang.Object)'

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.
Run Code Online (Sandbox Code Playgroud)

这是build.gradle关卡项目

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext {
        compose_version = '1.0.0-beta07'
        kotlin_version = "1.4.32"
        lifecycle_version = "2.3.1"
        hilt_version = '2.35'
    }
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.1.0-alpha01'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
Run Code Online (Sandbox Code Playgroud)

build.gradle水平模块

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id("kotlin-kapt")
    id("kotlin-parcelize")
    id("dagger.hilt.android.plugin")
}

android {

    compileSdk 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.android.gymexercisestracker"
        minSdk 21
        targetSdk 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary true
        }
    }

    buildTypes {
        release {
            minifyEnabled 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'
        useIR = true
    }
    buildFeatures {
        compose true
        dataBinding true
    }
    composeOptions {
        kotlinCompilerExtensionVersion compose_version
        kotlinCompilerVersion '1.4.32'
    }


}

dependencies {

    implementation 'androidx.core:core-ktx:1.5.0'
    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.compose.material:material:$compose_version"
    implementation "androidx.compose.ui:ui-tooling:$compose_version"
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
    implementation 'androidx.activity:activity-compose:1.3.0-alpha08'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"

    // Coroutines
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0'

    //Android Room
    implementation "androidx.room:room-runtime:2.4.0-alpha02"
    implementation "androidx.room:room-ktx:2.4.0-alpha02"
    kapt "androidx.room:room-compiler:2.4.0-alpha02"


    implementation("com.google.dagger:hilt-android:$hilt_version")
    kapt("com.google.dagger:hilt-android-compiler:$hilt_version")


}
Run Code Online (Sandbox Code Playgroud)

gradle-wrapper.properties

#Wed May 26 14:56:53 EET 2021
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME

Run Code Online (Sandbox Code Playgroud)

我尝试清除缓存并重新启动但同样的问题

Lov*_*eth 7

我遇到了同样的问题,GitHub 的答案为我解决了。链接 匕首github

编辑您的项目级别 build.gradle

   buildscript {

    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath ClassPaths.gradlePlugin
        classpath ClassPaths.kotlinPlugin
        classpath Libs.Hilt.gradlePlugin
        classpath 'com.android.tools.build:gradle:7.1.0-alpha01'
}
}
Run Code Online (Sandbox Code Playgroud)

buildscript {

    repositories {
        google()
        mavenCentral()
        maven {
            url  "https://oss.sonatype.org/content/repositories/snapshots"
            content {
                includeModule("com.google.dagger", "hilt-android-gradle-plugin")
            }
        }
    }

    dependencies {
      classpath ClassPaths.gradlePlugin
      classpath ClassPaths.kotlinPlugin
      classpath "com.google.dagger:hilt-android-gradle-plugin:HEAD-SNAPSHOT"
      classpath 'com.android.tools.build:gradle:7.1.0-alpha01'

    }
}
Run Code Online (Sandbox Code Playgroud)