【问题标题】:Error in gradle when implement hilt library in android在android中实现hilt库时gradle出错
【发布时间】:2021-05-26 13:15:21
【问题描述】:

我正在尝试在 android studio 版本 2020.1.1 Canary 1 中实现 hilt,但我在 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

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

【问题讨论】:

标签: android android-studio gradle dagger-2 dagger-hilt


【解决方案1】:

我遇到了同样的问题,来自 GitHub 的答案为我解决了这个问题。关联 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'

    }
}

【讨论】:

  • 非常感谢,这解决了我的问题。我还添加了includeModule("com.google.dagger", "hilt-android-compiler")
【解决方案2】:

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

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

这是我对 hilt 的完整依赖列表 构建.gradle(app)

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-24
  • 1970-01-01
  • 2022-01-02
  • 2023-01-29
  • 2013-07-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多