Flutter 3.24.5构建Release APK报错,求解决方案
问题现象
App在Flutter 3.24.5中可正常运行,但执行Release构建命令时报错:
"/Users/imac/Documents/flutter_sdk/flutter 3.24.5/bin/flutter" --no-color build apk
错误信息
Running Gradle task 'assembleRelease'...
You are applying Flutter's app_plugin_loader Gradle plugin imperatively using the apply script method, which is deprecated and will be removed in a future release. Migrate to applying Gradle plugins with the declarative plugins block: https://flutter.dev/to/flutter-gradle-plugin-applyFAILURE: Build failed with an exception.
Where:
Build file '/Users/imac/mobile-app-flutter/android/app/build.gradle' line: 2What went wrong:
Plugin [id: 'com.android.application'] was not found in any of the following sources:
- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (plugin dependency must include a version number for this source)
- Try:
Run with --stacktrace option to get the stack trace.
Run with --info or --debug option to get more log output.
Run with --scan to get full insights.
- Get more help at https://help.gradle.org
BUILD FAILED in 753ms
Running Gradle task 'assembleRelease'... 1,319ms
Gradle task assembleRelease failed with exit code 1
Process finished with exit code 1
当前settings.gradle代码
include ':app' def localPropertiesFile = new File(rootProject.projectDir, "local.properties") def properties = new Properties() assert localPropertiesFile.exists() localPropertiesFile.withReader("UTF-8") { reader -> properties.load(reader) } def flutterSdkPath = properties.getProperty("flutter.sdk") assert flutterSdkPath != null, "flutter.sdk not set in local.properties" apply from: "$flutterSdkPath/packages/flutter_tools/gradle/app_plugin_loader.gradle"
解决步骤
1. 迁移Gradle插件声明方式(解决app_plugin_loader废弃警告)
修改settings.gradle,移除旧的apply from方式,改用Flutter推荐的插件声明方式:
pluginManagement { def localPropertiesFile = new File(rootProject.projectDir, "local.properties") def properties = new Properties() assert localPropertiesFile.exists() localPropertiesFile.withReader("UTF-8") { reader -> properties.load(reader) } def flutterSdkPath = properties.getProperty("flutter.sdk") assert flutterSdkPath != null, "flutter.sdk not set in local.properties" includeBuild("$flutterSdkPath/packages/flutter_tools/gradle") } plugins { id "dev.flutter.flutter-plugin-loader" version "1.0.0" id "com.android.application" version "7.4.2" apply false id "org.jetbrains.kotlin.android" version "1.8.22" apply false } include ":app"
注:com.android.application和kotlin.android的版本号需与项目Gradle版本兼容,可根据实际情况调整
2. 修正app/build.gradle的插件引用
打开android/app/build.gradle,确保顶部用plugins块引用插件,移除旧的apply plugin:语句:
plugins { id "com.android.application" id "kotlin-android" id "dev.flutter.flutter-gradle-plugin" }
3. 验证Gradle仓库配置
检查android/build.gradle(根目录下的build.gradle),确保仓库配置正确:
allprojects { repositories { google() mavenCentral() } }
4. 清理并重新构建
执行以下命令清理缓存后重新构建:
flutter clean flutter pub get cd android ./gradlew clean cd .. flutter build apk --release
内容的提问来源于stack exchange,提问作者user21524558

