繁体   English   中英

错误:无法解析配置“:classpath”的所有工件。 无法解析外部依赖 X,因为没有定义存储库

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM