Flutter构建APK遇Gradle错误:AndroidManifest包属性不再被支持
Flutter APK构建Gradle错误解决方案:NDK版本与插件Namespace问题
一、解决NDK版本要求
构建提示需指定NDK版本25.1.8937393,直接修改app/build.gradle中的NDK配置:
- 打开
app/build.gradle - 找到
android块内的ndkVersion flutter.ndkVersion - 替换为指定版本:
ndkVersion "25.1.8937393"
二、解决插件AndroidManifest.xml的package属性废弃问题
Android Gradle Plugin 8.0+不再支持通过插件AndroidManifest.xml的package属性设置namespace,可通过以下两种方式处理:
方式1:优先升级插件到兼容版本
在项目pubspec.yaml中更新目标插件至最新兼容版本(版本号以pub.dev最新发布为准):
dependencies: firebase_core: ^2.24.2 firebase_messaging: ^14.6.4 bluetooth_enable_fork: ^0.1.5 file_picker: ^6.1.1
执行flutter pub get更新依赖,多数主流插件已适配AGP 8.0,升级后可自动解决namespace问题。
方式2:Gradle脚本自动修复(升级无效时使用)
若插件暂无兼容版本,可通过Gradle脚本自动移除插件Manifest中的package属性并设置正确namespace:
修改项目根目录下的build.gradle,替换subprojects块内容为:
subprojects { afterEvaluate { project -> if (project.plugins.hasPlugin('com.android.library')) { android { // 为每个插件匹配对应namespace(根据插件原package值填写) def pluginNamespace = "" switch (project.name) { case "firebase_core": pluginNamespace = "io.flutter.plugins.firebase.core" break case "firebase_messaging": pluginNamespace = "io.flutter.plugins.firebase.messaging" break case "bluetooth_enable_fork": pluginNamespace = "com.example.bluetooth_enable" break case "file_picker": pluginNamespace = "com.mr.flutter.plugin.filepicker" break default: pluginNamespace = project.group ?: "com.flutter.plugin" } namespace pluginNamespace // 自动移除AndroidManifest.xml中的package属性 def manifestFile = project.file("src/main/AndroidManifest.xml") if (manifestFile.exists()) { def content = manifestFile.getText('UTF-8') content = content.replaceAll(/\s*package="[^"]+"/, '') manifestFile.write(content, 'UTF-8') } } } } project.evaluationDependsOn(':app') }
三、验证构建
完成修改后,执行以下命令清理并重新构建:
flutter clean flutter build apk --release
内容的提问来源于stack exchange,提问作者Jacob Miller
相关产品推荐
相关产品推荐

