使用Room、Retrofit、Hilt构建Android应用遇路径重复文件错误求助
问题:构建Android应用时出现重复文件冲突错误
使用Room、Retrofit和Hilt构建Android应用时,运行报错:
'2 files found with path 'META-INF/gradle/incremental.annotation.processors''
已尝试清理并重建项目,问题未解决。
项目Gradle配置
模块级build.gradle
plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' id 'kotlin-kapt' id 'com.google.dagger.hilt.android' } android { namespace 'com.example.gymencv2' compileSdk 33 defaultConfig { applicationId "com.example.gymencv2" minSdk 21 targetSdk 33 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables { useSupportLibrary true } } 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' } buildFeatures { compose true } composeOptions { kotlinCompilerExtensionVersion '1.3.2' } packagingOptions { resources { excludes += ('/META-INF/{AL2.0,LGPL2.1}') } } } dependencies { implementation 'androidx.core:core-ktx:1.9.0' implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1' implementation 'androidx.activity:activity-compose:1.6.1' implementation "androidx.compose.ui:ui:$compose_ui_version" implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version" implementation 'androidx.compose.material:material:1.3.1' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.4' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0' androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_ui_version" debugImplementation "androidx.compose.ui:ui-tooling:$compose_ui_version" debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_ui_version" //Retrofit implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup.retrofit2:converter-gson:2.9.0' implementation "com.squareup.okhttp3:okhttp:5.0.0-alpha.2" implementation 'com.squareup.okhttp3:logging-interceptor:4.8.1' //Navigation component implementation "androidx.navigation:navigation-compose:$nav_version" //Coil implementation("io.coil-kt:coil-compose:2.2.2") implementation("io.coil-kt:coil-svg:2.2.2") //Room implementation "androidx.room:room-runtime:$room_version" annotationProcessor "androidx.room:room-compiler:$room_version" kapt "androidx.room:room-compiler:$room_version" implementation("androidx.room:room-ktx:$room_version") //Dagger-Hilt implementation "com.google.dagger:hilt-android-compiler:$hilt_version" kapt "com.google.dagger:hilt-android-compiler:$hilt_version" implementation "com.google.dagger:hilt-android:$hilt_version" } ext { compose_ui_version = '1.3.2' nav_version = "2.5.3" room_version = "2.4.3" hilt_version = "2.44.2" }
项目级build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { id 'com.android.application' version '7.3.1' apply false id 'com.android.library' version '7.3.1' apply false id 'org.jetbrains.kotlin.android' version '1.7.20' apply false id 'com.google.dagger.hilt.android' version '2.44' apply false }
解决方案
1. 修改packagingOptions排除冲突文件
在模块级android块的packagingOptions中添加对冲突文件的排除规则:
packagingOptions { resources { excludes += ('/META-INF/{AL2.0,LGPL2.1}') // 添加此行排除冲突的增量注解处理器文件 excludes += 'META-INF/gradle/incremental.annotation.processors' } }
2. 修正重复/错误的依赖配置
- Room部分:Kotlin项目只需保留
kapt依赖,移除annotationProcessor:
//Room implementation "androidx.room:room-runtime:$room_version" // 移除该行 // annotationProcessor "androidx.room:room-compiler:$room_version" kapt "androidx.room:room-compiler:$room_version" implementation("androidx.room:room-ktx:$room_version")
- Hilt部分:编译器依赖不能用
implementation,只能用kapt,移除错误的依赖声明:
//Dagger-Hilt // 移除该行 // implementation "com.google.dagger:hilt-android-compiler:$hilt_version" kapt "com.google.dagger:hilt-android-compiler:$hilt_version" implementation "com.google.dagger:hilt-android:$hilt_version"
3. 统一OkHttp版本
当前OkHttp主版本与logging-interceptor版本不一致,可能引发冲突,统一为稳定版:
//Retrofit implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup.retrofit2:converter-gson:2.9.0' // 统一为相同稳定版本 implementation "com.squareup.okhttp3:okhttp:4.10.0" implementation 'com.squareup.okhttp3:logging-interceptor:4.10.0'
4. 同步并重建项目
完成上述修改后,执行以下操作:
- 点击Android Studio右上角的「Sync Project with Gradle Files」同步配置
- 执行
Build > Clean Project和Build > Rebuild Project
内容的提问来源于stack exchange,提问作者deja
相关产品推荐
相关产品推荐

