Android Studio集成Hilt-Dagger时Rebuild项目出现链接失败如何解决
Android集成Hilt后Rebuild Project链接失败问题
我是Android开发新手,正跟随教程学习Android开发相关知识,尝试在应用中集成Hilt-Dagger时,执行Rebuild Project操作后出现链接失败错误,暂未找到修复方案。以下是我的Gradle配置内容:
错误截图:
应用级build.gradle配置
apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-kapt' apply plugin: "androidx.navigation.safeargs.kotlin" apply plugin: "dagger.hilt.android.plugin" android { compileSdkVersion 30 buildToolsVersion "29.0.3" defaultConfig { applicationId "com.androiddevs.bikemap" minSdkVersion 21 targetSdkVersion 30 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 = JavaVersion.VERSION_1_8.toString() } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) //noinspection GradleDependency implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'androidx.appcompat:appcompat:1.3.1' implementation 'androidx.core:core-ktx:1.6.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.0' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' // Material Design implementation 'com.google.android.material:material:1.5.0-alpha03' // Architectural Components implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1" // Room implementation "androidx.room:room-runtime:2.3.0" kapt "androidx.room:room-compiler:2.3.0" // Kotlin Extensions and Coroutines support for Room implementation "androidx.room:room-ktx:2.3.0" // Coroutines implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.1' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.1' // Coroutine Lifecycle Scopes implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1" implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.3.1" // Navigation Components implementation "androidx.navigation:navigation-fragment-ktx:2.3.5" implementation "androidx.navigation:navigation-ui-ktx:2.3.5" // Glide implementation 'com.github.bumptech.glide:glide:4.11.0' kapt 'com.github.bumptech.glide:compiler:4.11.0' // Google Maps Location Services implementation 'com.google.android.gms:play-services-location:18.0.0' implementation 'com.google.android.gms:play-services-maps:17.0.1' // Dagger Core implementation "com.google.dagger:dagger:2.38.1" kapt "com.google.dagger:dagger-compiler:2.38.1" // Dagger Android api 'com.google.dagger:dagger-android:2.35.1' api 'com.google.dagger:dagger-android-support:2.28.1' kapt 'com.google.dagger:dagger-android-processor:2.23.2' //Activity KTX for ViewModels() implementation "androidx.activity:activity-ktx:1.3.1" //Dagger-Hilt implementation 'com.google.dagger:hilt-android:2.38.1' kapt 'com.google.dagger:hilt-android-compiler:2.38.1' implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03' kapt 'androidx.hilt:hilt-compiler:1.0.0' // Easy Permissions implementation 'pub.devrel:easypermissions:3.0.0' // Timber implementation 'com.jakewharton.timber:timber:4.7.1' // MPAndroidChart implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0' implementation 'android.arch.lifecycle:extensions:1.1.1' }
运行环境:Windows 10
修复方案
- 移除冗余冲突依赖
你同时引入了Dagger原生系列依赖和Hilt依赖,二者存在功能重叠、版本不统一的冲突问题,直接删除以下Dagger原生相关代码即可:
// Dagger Core implementation "com.google.dagger:dagger:2.38.1" kapt "com.google.dagger:dagger-compiler:2.38.1" // Dagger Android api 'com.google.dagger:dagger-android:2.35.1' api 'com.google.dagger:dagger-android-support:2.28.1' kapt 'com.google.dagger:dagger-android-processor:2.23.2'
- 统一Hilt版本
确认项目根目录build.gradle文件中的Hilt插件版本和应用级的Hilt依赖版本完全一致,都使用2.38.1,根目录配置示例:
buildscript { dependencies { // 其他插件 classpath "com.google.dagger:hilt-android-gradle-plugin:2.38.1" } }
- 清理缓存重建
点击Android Studio菜单栏File -> Invalidate Caches...,勾选清理文件系统缓存和本地历史,重启IDE后再执行Rebuild Project操作即可。
内容的提问来源于stack exchange,提问作者Justine Guillermo
相关产品推荐
相关产品推荐

