Android Studio运行默认Basic Activity报错,更新CompileSDKVersion后仍有依赖问题
解决Android新项目依赖/类重复问题
以下是针对你遇到的依赖/类重复问题的具体解决步骤:
升级Gradle插件版本
确保项目级build.gradle中的Gradle插件版本与CompileSDK 33兼容,推荐使用7.4.0及以上版本:buildscript { repositories { google() mavenCentral() } dependencies { classpath "com.android.tools.build:gradle:7.4.2" } }同时注意Gradle wrapper版本需匹配插件版本(比如插件7.4.2对应Gradle 7.5),可在
gradle/wrapper/gradle-wrapper.properties中修改:distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip统一模块依赖版本
打开app级build.gradle,将所有官方依赖(如AppCompat、Material、ConstraintLayout)统一为适配SDK33的稳定版本,避免版本不一致导致的冲突:dependencies { implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'com.google.android.material:material:1.9.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' }也可以用版本变量统一管理,减少手动维护成本:
def appCompatVersion = "1.6.1" def materialVersion = "1.9.0" def constraintLayoutVersion = "2.1.4" dependencies { implementation "androidx.appcompat:appcompat:$appCompatVersion" implementation "com.google.android.material:material:$materialVersion" implementation "androidx.constraintlayout:constraintlayout:$constraintLayoutVersion" // 其余依赖... }排查并排除重复依赖
执行Gradle命令生成依赖树,定位重复类的来源:./gradlew app:dependencies在输出结果中查找标记为
(*)重复的依赖项,找到后在对应依赖中用exclude排除多余模块,例如:implementation('某第三方库:1.0.0') { exclude group: 'androidx.appcompat', module: 'appcompat' }清理缓存并重建项目
- 点击Android Studio顶部菜单
Build > Clean Project - 执行
Build > Rebuild Project - 若仍有问题,手动删除项目根目录下的
.gradle文件夹和app模块下的build文件夹,再点击File > Sync Project with Gradle Files
- 点击Android Studio顶部菜单
内容的提问来源于stack exchange,提问作者PHARAOH MOH
相关产品推荐
相关产品推荐

