Android集成Dagger Hilt报错:无仓库定义无法解析classpath依赖
问题描述
在Android项目中接入Dagger Hilt依赖时编译抛出异常,核心错误为:无法解析配置:classpath的所有制品,因未定义任何仓库,无法解析根项目所需的外部依赖com.google.dagger:hilt-android-gradle-plugin:2.38.1,Gradle提示需声明提供对应制品的仓库。
完整错误日志:
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
现有配置
根目录build.gradle
buildscript { dependencies { classpath 'com.google.dagger:hilt-android-gradle-plugin:2.38.1' } } task clean(type: Delete) { delete rootProject.buildDir }
app模块build.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' }
排查方向与修复方案
- 修复根build.gradle缺失的buildscript仓库配置:这是触发当前报错的直接原因。现有
buildscript代码块仅声明了依赖,未配置仓库源,Gradle无法找到Hilt插件的拉取地址。在buildscript块内补充repositories声明即可:
buildscript { // 新增以下仓库配置 repositories { google() mavenCentral() } dependencies { classpath 'com.google.dagger:hilt-android-gradle-plugin:2.38.1' } }
- 统一Hilt全链路依赖版本:当前配置中Hilt Gradle插件版本为2.38.1,但模块内的Hilt核心库版本为2.28.3-alpha,版本跨度过大必然出现兼容问题。将app模块内的Hilt核心依赖版本统一为2.38.1:
// 替换原有2.28.3-alpha版本 implementation 'com.google.dagger:hilt-android:2.38.1' kapt 'com.google.dagger:hilt-android-compiler:2.38.1'
- 移除过时的Hilt扩展依赖:当前引入的
androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha02是极早期的测试版本,Hilt从稳定版开始已原生支持ViewModel注入,无需额外引入该依赖,直接删除对应两行依赖声明即可,避免版本冲突。 - 校验全局仓库配置:检查项目根目录下的
settings.gradle文件,确认dependencyResolutionManagement代码块中已声明google()和mavenCentral()仓库,覆盖所有模块的依赖拉取需求,避免后续出现其他依赖解析失败问题,参考配置:
dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() } }
- 所有配置修改完成后,执行Gradle sync同步项目,再执行编译即可。
内容的提问来源于stack exchange,提问作者hemant
相关产品推荐
相关产品推荐

