[英]Flutter: "Could not resolve all artifacts for configuration ':classpath'"
[英]Error: Could not resolve all artifacts for configuration ':classpath'. Cannot resolve external dependency X because no repositories are defined
我想在我的項目中添加 dagger hilt 依賴項,但編譯器顯示此消息。
這是錯誤信息
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(項目名稱)
buildscript {
dependencies {
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.38.1'
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
構建.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'
}
您根本沒有告訴 gradle 從哪里獲取您的插件。 要實現這一點,只需將您的項目 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
}
要了解更多信息,請隨時閱讀Gradle 文檔並查看Maven 中央存儲庫以獲取更多信息
當您在較新版本的 Android Studio 中創建項目時,構建結構與我們在文檔中仍然可以找到的結構略有不同...
您的項目中的 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
}
您仍然可以在 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
}
// ...
或者,如果您要使用的庫已經支持這種新格式,則使用更新的插件方式,如下所示:
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
}
在 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.