Flutter应用升级Gradle 8.0后Release APK构建失败求助
问题概述
旧版Flutter应用升级Gradle 8.0 + Kotlin 1.8.10后,Debug模式运行正常,但Release APK无法安装,构建过程中出现多轮兼容性错误。
当前android/app/build.gradle配置
def localProperties = new Properties() def localPropertiesFile = rootProject.file('local.properties') if (localPropertiesFile.exists()) { localPropertiesFile.withReader('UTF-8') { reader -> localProperties.load(reader) } } def flutterRoot = localProperties.getProperty('flutter.sdk') if (flutterRoot == null) { throw new FileNotFoundException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.") } def flutterVersionCode = localProperties.getProperty('flutter.versionCode') if (flutterVersionCode == null) { flutterVersionCode = '1' } def flutterVersionName = localProperties.getProperty('flutter.versionName') if (flutterVersionName == null) { flutterVersionName = '1.0' } apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" apply plugin: 'com.google.gms.google-services' apply plugin: 'com.google.firebase.crashlytics' def keystoreProperties = new Properties() def keystorePropertiesFile = rootProject.file('key.properties') if (keystorePropertiesFile.exists()) { keystoreProperties.load(new FileInputStream(keystorePropertiesFile)) } android { compileSdkVersion flutter.compileSdkVersion ndkVersion flutter.ndkVersion compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } sourceSets { main.java.srcDirs += 'src/main/kotlin' } lintOptions { disable 'InvalidPackage' } defaultConfig { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). applicationId "com.example.app" minSdkVersion 21 targetSdkVersion flutter.targetSdkVersion versionCode flutterVersionCode.toInteger() versionName flutterVersionName } signingConfigs { release { keyAlias keystoreProperties['keyAlias'] keyPassword keystoreProperties['keyPassword'] storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null storePassword keystoreProperties['storePassword'] } } buildTypes { release { signingConfig signingConfigs.release } } } flutter { source '../..' } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" //1.8.10 implementation 'com.google.firebase:firebase-core:16.0.9' // implementation platform('com.google.firebase:firebase-bom:29.0.4') implementation 'com.google.firebase:firebase-analytics' } apply plugin: 'com.google.gms.google-services'
首次构建错误信息
Note: Some input files use or override a deprecated API. Note: Recompile with -Xlint:deprecation for details. Note: /Users/llomen/.pub-cache/hosted/pub.dev/firebase_messaging-14.6.9/android/src/main/java/io/flutter/plugins/firebase/messaging/FlutterFirebaseMessagingPlugin.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. Note: /Users/llomen/.pub-cache/hosted/pub.dev/flutter_mailer-2.1.1/android/src/main/java/com/dataxad/flutter_mailer/FlutterMailerPlugin.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. /Users/llomen/.pub-cache/hosted/pub.dev/video_player_android-2.4.10/android/src/main/java/io/flutter/plugins/videoplayer/Messages.java:44: error: pattern matching in instanceof is not supported in -source 8 if (exception instanceof FlutterError error) { ^ (use -source 16 or higher to enable pattern matching in instanceof) /Users/llomen/.pub-cache/hosted/pub.dev/video_player_android-2.4.10/android/src/main/java/io/flutter/plugins/videoplayer/QueuingEventSink.java:62: error: pattern matching in instanceof is not supported in -source 8 } else if (event instanceof ErrorEvent errorEvent) { ^ (use -source 16 or higher to enable pattern matching in instanceof) 2 errors FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':video_player_android:compileReleaseJavaWithJavac'. > Compilation failed; see the compiler error output for details.
升级Gradle后的错误信息
A problem occurred configuring root project 'android'. > Could not resolve all files for configuration ':classpath'. > Could not resolve com.android.tools.build:gradle:8.0.0. Required by: project : > No matching variant of com.android.tools.build:gradle:8.0.0 was found. The consumer was configured to find a library for use during runtime, compatible with Java 8, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '8.0' but: - Variant 'apiElements' capability com.android.tools.build:gradle:8.0.0 declares a library, packaged as a jar, and its dependencies declared externally: - Incompatible because this component declares a component for use during compile-time, compatible with Java 11 and the consumer needed a component for use during runtime, compatible with Java 8 - Other compatible attribute: - Doesn't say anything about org.gradle.plugin.api-version (required '8.0') - Variant 'javadocElements' capability com.android.tools.build:gradle:8.0.0 declares a component for use during runtime, and its dependencies declared externally: - Incompatible because this component declares documentation and the consumer needed a library - Other compatible attributes: - Doesn't say anything about its target Java version (required compatibility with Java 8) - Doesn't say anything about its elements (required them packaged as a jar) - Doesn't say anything about org.gradle.plugin.api-version (required '8.0') - Variant 'runtimeElements' capability com.android.tools.build:gradle:8.0.0 declares a library for use during runtime, packaged as a jar, and its dependencies declared externally: - Incompatible because this component declares a component, compatible with Java 11 and the consumer needed a component, compatible with Java 8 - Other compatible attribute: - Doesn't say anything about org.gradle.plugin.api-version (required '8.0') - Variant 'sourcesElements' capability com.android.tools.build:gradle:8.0.0 declares a component for use during runtime, and its dependencies declared externally: - Incompatible because this component declares documentation and the consumer needed a library - Other compatible attributes: - Doesn't say anything about its target Java version (required compatibility with Java 8) - Doesn't say anything about its elements (required them packaged as a jar) - Doesn't say anything about org.gradle.plugin.api-version (required '8.0')
解决方案
1. 升级Java版本适配Gradle 8.0
Gradle 8.0要求JDK 11及以上,修改android/app/build.gradle的编译配置:
compileOptions { sourceCompatibility JavaVersion.VERSION_11 targetCompatibility JavaVersion.VERSION_11 } kotlinOptions { jvmTarget = '11' }
同时在Android Studio中设置项目JDK为11(File > Project Structure > SDK Location > JDK Location),本地环境需提前安装JDK 11。
2. 更新Flutter依赖包
旧版依赖无法适配新Java语法和Gradle规则,执行命令升级所有依赖:
flutter pub upgrade
若部分依赖升级后仍有问题,直接在pubspec.yaml中指定最新稳定版本,比如video_player、firebase_messaging、flutter_mailer等。
3. 修复Firebase依赖配置
移除旧版firebase-core,启用Firebase BOM统一管理版本:
dependencies { implementation platform('com.google.firebase:firebase-bom:32.7.1') // 使用最新BOM版本 implementation 'com.google.firebase:firebase-analytics' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" // 适配Kotlin 1.8.10 }
同时删除配置中重复的apply plugin: 'com.google.gms.google-services'。
4. 修正根目录build.gradle配置
确保项目根目录的build.gradle(非app模块)中插件版本匹配:
buildscript { ext.kotlin_version = '1.8.10' repositories { google() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:8.0.0' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'com.google.gms:google-services:4.4.0' classpath 'com.google.firebase:firebase-crashlytics-gradle:2.9.9' } }
5. 清理缓存重新构建
执行以下命令清除缓存并重新编译:
flutter clean flutter pub get ./gradlew clean build
内容的提问来源于stack exchange,提问作者Llomen
相关产品推荐
相关产品推荐

