Android插件已禁用,脚本插件不兼容导致编译错误如何解决?
解决Android Plugin Disabled及脚本插件兼容错误
问题根源
出现的编译错误核心是Kotlin版本、Compose编译器扩展版本与Gradle插件版本不匹配,导致脚本插件无法兼容当前编译器,进而引发Android插件加载异常。
修复步骤
1. 适配Compose编译器与Kotlin版本
你的项目使用了Compose BOM 2023.05.01,对应的兼容Kotlin版本为1.8.20,需将Compose编译器扩展版本升级到匹配的1.4.3。修改模块下的build.gradle.kts:
composeOptions { kotlinCompilerExtensionVersion = "1.4.3" }
2. 统一根目录插件版本配置
在项目根目录的settings.gradle.kts中,指定与项目适配的Gradle插件和Kotlin版本,确保全局版本一致:
pluginManagement { repositories { google() mavenCentral() gradlePluginPortal() } plugins { val kotlinVersion = "1.8.20" id("org.jetbrains.kotlin.android") version kotlinVersion id("com.android.application") version "8.1.4" // 适配Kotlin 1.8.20的Gradle插件版本 } }
3. 清理错误依赖配置
你的依赖中存在重复依赖、错误的依赖作用域问题,修正后如下:
dependencies { implementation(platform("androidx.compose:compose-bom:2023.05.01")) implementation("androidx.activity:activity-compose:1.7.2") implementation("androidx.activity:activity-ktx:1.7.2") implementation("androidx.appcompat:appcompat:1.6.1") implementation("androidx.compose.material3:material3") implementation("androidx.compose.runtime:runtime") implementation("androidx.compose.runtime:runtime-livedata") implementation("androidx.compose.ui:ui") implementation("androidx.compose.ui:ui-graphics") implementation("androidx.compose.ui:ui-tooling-preview") implementation("androidx.core:core-ktx:1.12.0") implementation("androidx.lifecycle:lifecycle-livedata-ktx:2.6.2") implementation("androidx.lifecycle:lifecycle-runtime-compose:2.6.2") implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.2") implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.6.2") implementation("androidx.work:work-runtime-ktx:2.8.1") implementation("androidx.test:monitor:1.6.1") implementation("androidx.test.ext:junit-ktx:1.1.5") testImplementation("junit:junit:4.13.2") testImplementation("org.hamcrest:hamcrest-library:1.3") androidTestImplementation("junit:junit:4.13.2") debugImplementation("androidx.compose.ui:ui-test-manifest") debugImplementation("androidx.compose.ui:ui-tooling") }
4. 同步与清理项目
- 点击Android Studio右上角的Sync Project with Gradle Files按钮同步配置
- 执行
File > Invalidate Caches... > Invalidate and Restart清理缓存并重启IDE
额外检查项
- 确认Android Studio的Kotlin插件版本与项目中使用的Kotlin版本一致(路径:
File > Settings > Languages & Frameworks > Kotlin) - 检查Gradle版本适配性:Gradle 8.0+适配Kotlin 1.8.x,可在
gradle/wrapper/gradle-wrapper.properties中设置distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-all.zip
内容的提问来源于stack exchange,提问作者AndroidTa
相关产品推荐
相关产品推荐

