在 android 中实现刀柄库时 gradle 出错

Error in gradle when implement hilt library in android

我正在尝试在 android 工作室版本 2020.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.

这里是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
}

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")


}

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

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

即使我遇到过这个问题也很奇怪,我通过确保解决了它 Build.gradle(app) 中的这两个依赖版本匹配,希望这对您也有帮助

 implementation "com.google.dagger:hilt-android:2.31-alpha"
 kapt "com.google.dagger:hilt-android-compiler:2.31-alpha"

这是我对 hilt 的完整依赖列表 Build.gradle(应用程序)

plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
 }

dependencies {
//Dagger - Hilt
implementation "com.google.dagger:hilt-android:2.31-alpha"
kapt "com.google.dagger:hilt-android-compiler:2.31-alpha"
implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
kapt "androidx.hilt:hilt-compiler:1.0.0"
 }

build.gradle(项目)

  dependencies {
  classpath "com.google.dagger:hilt-android-gradle-plugin:2.28.3-alpha"
  }

我遇到了同样的问题,GitHub 的回答为我解决了这个问题。 link dagger 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'
}
}

之后

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'

    }
}