[英]Error: Could not resolve all artifacts for configuration ':classpath'. Cannot resolve external dependency X because no repositories are defined
I want to add dagger hilt dependency in my project but compiler shows this message.我想在我的项目中添加 dagger hilt 依赖项,但编译器显示此消息。
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) build.gradle(项目名称)
buildscript {
dependencies {
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.38.1'
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
build.gradle:app构建.gradle:应用程序
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'
}
You simply didn't tell gradle where to fetch your plugin from.您根本没有告诉 gradle 从哪里获取您的插件。 To achieve this simply replace your project build.gradle by this one :
要实现这一点,只需将您的项目 build.gradle 替换为这个:
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要了解更多信息,请随时阅读Gradle 文档并查看Maven 中央存储库以获取更多信息
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...当您在较新版本的 Android Studio 中创建项目时,构建结构与我们在文档中仍然可以找到的结构略有不同...
Your build.gradle from project will be like this:您的项目中的 build.gradle 将如下所示:
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 中为插件类路径添加一个依赖块,如下所示:
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:在 app 模块中的 build.gradle 将像我们之前一直使用的那样:
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"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.