Home > Enterprise >  Error: Could not resolve all artifacts for configuration ':classpath'. Cannot resolve exte
Error: Could not resolve all artifacts for configuration ':classpath'. Cannot resolve exte

Time:06-08

I want to add dagger hilt dependency in my project but compiler shows this message.

This is error message

Could not resolve all artifacts for configuration ':classpath'.
   > Cannot resolve external dependency com.google.dagger:hilt-android-gradle-plugin:2.38.1 because no repositories are defined.
     Required by:
         project :

Possible solution:
 - Declare repository providing the artifact, see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html

build.gradle (projectname)

buildscript {
dependencies {
    classpath 'com.google.dagger:hilt-android-gradle-plugin:2.38.1'
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}

build.gradle:app

plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'

}

android {
compileSdk 31

defaultConfig {
    applicationId "com.example.paginationjetpackcompose"
    minSdk 21
    targetSdk 31
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

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

dependencies {
implementation 'androidx.core:core-ktx:1.8.0'
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

implementation 'com.google.dagger:hilt-android:2.28.3-alpha'
kapt 'com.google.dagger:hilt-android-compiler:2.28.3-alpha'

implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha02"
kapt "androidx.hilt:hilt-compiler:1.0.0-alpha02"


def paging_version = "3.0.0-alpha02"

implementation "androidx.paging:paging-runtime-ktx:$paging_version"

implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.squareup.retrofit2:retrofit:2.9.0' 
}

CodePudding user response:

You simply didn't tell gradle where to fetch your plugin from. To achieve this simply replace your project build.gradle by this one :

buildscript {
  // I only added this part indicating to gradle to go to mavenCentral to fetch plugins
  repositories {
    mavenCentral()
  }

  dependencies {
    classpath 'com.google.dagger:hilt-android-gradle-plugin:2.38.1'
  }
}

task clean(type: Delete) {
  delete rootProject.buildDir
}

To learn more, feel free to read Gradle docs and check Maven central repos for more info

CodePudding user response:

When you create a project in newer versions of Android Studio the build structures are a little different from what we still find in documentation...

Your build.gradle from project will be like this:

buildscript {
    ext {
        kotlin_ver = '1.6.21'
    }
}

plugins {
    id 'com.android.application' version '7.2.1' apply false
    id 'com.android.library' version '7.2.1' apply false
    id 'org.jetbrains.kotlin.android' version "$kotlin_ver" apply false
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

You still can add a dependencies block inside buildscript for plugins classpath like this:

buildscript {
    ext {
        kotlin_ver = '1.6.21'
        hilt_ver = '2.42'
    }

    dependencies {
        classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_ver"
    }
}

plugins {
    id 'com.android.application' version '7.2.1' apply false
    id 'com.android.library' version '7.2.1' apply false
    id 'org.jetbrains.kotlin.android' version "$kotlin_ver" apply false
    id 'org.jetbrains.kotlin.kapt' version "$kotlin_ver" apply false
}

// ...

Or use the newer plugin way if the library you want to use already supports this new format like this:

buildscript {
    ext {
        kotlin_ver = '1.6.21'
        hilt_ver = '2.42'
    }
}

plugins {
    id 'com.android.application' version '7.2.1' apply false
    id 'com.android.library' version '7.2.1' apply false
    id 'org.jetbrains.kotlin.android' version "$kotlin_ver" apply false
    id 'org.jetbrains.kotlin.kapt' version "$kotlin_ver" apply false
    id 'com.google.dagger.hilt.android' version "$hilt_ver" apply false
}

On build.gradle from app module will be as we always used before:

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'org.jetbrains.kotlin.kapt'
    id 'dagger.hilt.android.plugin'
}

android {
    // ...
}

dependencies {
    // ...
    
    implementation "com.google.dagger:hilt-android:$hilt_ver"
    kapt "com.google.dagger:hilt-android-compiler:$hilt_ver"
}
  • Related