Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in gradle when implement hilt library in android

I am trying to implement hilt in android studio version 2020.1.1 Canary 1 but I get this error in 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.

Here's the build.gradle level project

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

and build.gradle level module

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


}

and 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

I tried to Clear cache and restart but same problem

like image 814
Ahmed M. Abdalla Avatar asked Mar 02 '23 14:03

Ahmed M. Abdalla


1 Answers

I had the same issue the answer from GitHub fixed it for me. link dagger github

Edit your project level build.gradle

Before

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

After

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'

    }
}
like image 79
Loveth Avatar answered Mar 07 '23 05:03

Loveth