Flutter项目Gradle构建报错:Java插件与Android插件不兼容
解决Flutter Gradle构建失败(Java插件与Android插件不兼容)
问题根源
虽然你没找到apply plugin: 'java',但纯Java项目用的kotlin插件会自动引入Java插件,和com.android.application插件冲突。另外还有两处配置问题加剧了冲突。
修复步骤
1. 替换Kotlin插件为Android专用版本
打开app/build.gradle,把plugins块里的id "kotlin"替换成Android专属的Kotlin插件:
plugins { id "com.android.application" id "org.jetbrains.kotlin.android" // 替换原有的id "kotlin" id 'kotlin-parcelize' id "dev.flutter.flutter-gradle-plugin" id "com.google.gms.google-services" }
2. 移除重复的Google Services插件应用
在app/build.gradle的android代码块末尾,删掉apply plugin: 'com.google.gms.google-services'——你已经在plugins块里声明过这个插件,重复应用会引发冲突。
3. 替换Multidex依赖为AndroidX版本
Flutter项目默认使用AndroidX,把dependencies里的旧support库替换成AndroidX版本:
dependencies { // Import the Firebase BoM implementation(platform("com.google.firebase:firebase-bom:32.8.0")) implementation("com.google.firebase:firebase-analytics") implementation 'androidx.multidex:multidex:2.0.1' // 替换原有的com.android.support版本 }
4. 清理缓存并重新构建
执行以下命令清理缓存后重新编译:
flutter clean flutter pub get flutter run
额外检查
如果仍有报错,确认根目录android/build.gradle里的Kotlin版本(当前为1.9.23)和org.jetbrains.kotlin.android插件兼容,无需修改。
内容的提问来源于stack exchange,提问作者Davis Fila
相关产品推荐
相关产品推荐

