将Android代码转为应用模块后遇AGP版本兼容等问题求解决方案
解决AGP版本兼容报错:Could not determine whether value 7.3.1 is compatible with value 7.2.1
引入Android模块后出现AGP版本不兼容报错,提示Could not determine whether value 7.3.1 is compatible with value 7.2.1 using AgpVersionCompatibilityRule,尝试配置后问题仍未解决。
报错截图



应用级build.gradle配置
android { compileSdk 33 namespace "com.iw.tttttttt" defaultConfig { configurations.all { resolutionStrategy { force 'androidx.core:core-ktx:1.6.0' } } applicationId "com.application.alphacapital.superapp" minSdk 21 targetSdk 33 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" }
模块级build.gradle配置
apply plugin: 'com.android.library' apply plugin: 'kotlin-android' apply plugin: 'kotlin-parcelize' apply plugin: 'kotlin-kapt' android { compileSdkVersion 33 defaultConfig { minSdk 19 targetSdkVersion 32 multiDexEnabled true vectorDrawables.useSupportLibrary = true testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" javaCompileOptions { annotationProcessorOptions { arguments = ["room.incremental":"true"] } } }
解决方案
1. 统一项目所有模块的AGP版本
AGP(Android Gradle Plugin)版本必须在整个项目中保持一致,否则会触发兼容性校验错误:
- 打开项目根目录下的
build.gradle,在buildscript块中定义统一的AGP版本:buildscript { ext { agp_version = "7.3.1" // 选择主应用或模块使用的版本统一配置 kotlin_version = "1.7.20" // 确保Kotlin版本与AGP兼容 } repositories { google() mavenCentral() } dependencies { classpath "com.android.tools.build:gradle:$agp_version" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } - 确保所有模块的build.gradle中不再硬编码其他AGP版本,统一引用根配置的版本。
2. 统一Compile SDK与Target SDK版本
主应用与模块的targetSdk版本不一致(主应用33,模块32),可能加剧版本兼容问题,建议统一:
- 修改模块build.gradle中的
targetSdkVersion为33:android { compileSdkVersion 33 defaultConfig { minSdk 19 targetSdkVersion 33 // 与主应用保持一致 // ...其他配置 } }
3. 清理缓存并重新同步
- 执行
File -> Invalidate Caches... -> Invalidate and Restart清理Android Studio缓存 - 点击工具栏的
Sync Project with Gradle Files重新同步项目
4. 排查依赖冲突
如果上述方法无效,执行终端命令查看依赖树,定位冲突依赖:
./gradlew app:dependencies
找到冲突的AGP相关依赖后,在主应用的configurations.all中添加强制统一版本的规则。
内容的提问来源于stack exchange,提问作者Laxmi kant
相关产品推荐
相关产品推荐

