-1

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' 
}
halfer
  • 19,824
  • 17
  • 99
  • 186
hemant
  • 173
  • 2
  • 9
  • Please only use useful titles here. "How to solve this problem? Please help me" does not say what the problem is about at all. – halfer Jun 07 '22 at 21:39

2 Answers2

3

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

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.7.10'
        hilt_ver = '2.43.1'
    }

    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.7.10'
        hilt_ver = '2.43.1'
    }
}

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 'com.google.dagger.hilt.android'
}

android {
    // ...
}

dependencies {
    // ...
    
    implementation "com.google.dagger:hilt-android:$hilt_ver"
    kapt "com.google.dagger:hilt-android-compiler:$hilt_ver"
}
Thales Isidoro
  • 1,753
  • 1
  • 5
  • 19
0

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

Jacouille
  • 951
  • 8
  • 14
  • now this error occurs * Exception is: org.gradle.api.plugins.UnknownPluginException: Plugin [id: 'kotlin-kapt'] was not found in any of the following sources: – hemant Jun 07 '22 at 15:57
  • 2
    You are the wrong jacouline. This would work only if @hemant is using an old android studio. In the current android studio, we add those in the `settings. gradle` file, and it looks like Hemant is using the latest version – Sambhav Khandelwal Jun 07 '22 at 16:02
  • yes, as you can see kotlin kapt is called from app build.gradle, but not added to classpath from project build.gradle, you'll have to do same work as before. Find it in remote repos like Maven Central, and then add it with appropriate version. Then it should work as intended. Feel free to update question with problems you encounter, i'll come back to help you – Jacouille Jun 07 '22 at 16:03
  • @Sambhav.K feel free to add correct answer then – Jacouille Jun 07 '22 at 16:05
  • I couldnt find anything wrong in the code. Just checking – Sambhav Khandelwal Jun 07 '22 at 16:06