Android切换Jetpack Compose为XML布局后Gradle构建失败求助
问题根因
- Kotlin 1.6.0-RC版本已将IR编译器设为默认开启状态,
useIR配置项已被官方移除,你当前项目使用的Kotlin gradle插件版本恰好为1.6.0-RC,保留useIR = true配置会直接触发属性找不到的异常 - 你已切换为XML布局开发,但项目中仍残留大量Jetpack Compose相关配置,存在版本冲突隐患
- 应用级build.gradle存在
com.android.application插件重复声明的问题
修复步骤
1. 移除useIR配置
打开应用级build.gradle,找到kotlinOptions代码块,删除useIR = true行,修改后配置如下:
kotlinOptions { jvmTarget = '1.8' }
2. 清理所有Compose相关冗余配置
- 删除
buildFeatures代码块中的compose true配置,无其他buildFeatures配置可直接删除整个buildFeatures块 - 删除整个
composeOptions代码块 - 从dependencies依赖项中删除所有Compose相关依赖:
// 需删除的依赖清单 implementation "androidx.compose.ui:ui:$compose_version" implementation "androidx.compose.material:material:$compose_version" implementation "androidx.compose.ui:ui-tooling-preview:$compose_version" implementation 'androidx.activity:activity-compose:1.3.1' androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version" debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
- 无需继续使用Compose的情况下,可同步删除项目级build.gradle中
ext块里的compose_version = '1.0.1'配置
3. 删除重复的插件声明
应用级build.gradle开头已经通过id 'com.android.application'声明了插件,删除末尾重复的apply plugin: 'com.android.application'行即可。
验证修复
修改完成后同步Gradle文件,即可正常构建项目。
内容的提问来源于stack exchange,提问作者Maxwell D. Dorliea
相关产品推荐
相关产品推荐

