Flutter升级Gradle后打包APK报错:第三方库未指定namespace
解决Flutter升级Gradle后打包报错(namespace未指定)
问题背景
升级Flutter项目Gradle版本后做了以下修改:
- 在
gradle.properties中添加:org.gradle.java.home=C:\Program Files\\Java\\jdk-18.0.2.1 - 修改顶层
build.gradle:ext.kotlin_version = '1.8.22' dependencies { classpath 'com.android.tools.build:gradle:8.0.0' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" }
执行flutter build apk时出现报错,核心提示:
A problem occurred configuring project ':flutter_inappwebview'.
Could not create an instance of type com.android.build.api.variant.impl.LibraryVariantBuilderImpl.
Namespace not specified. Please specify a namespace in the module's build.gradle file...
但自身app模块的build.gradle已正确配置namespace "com.example.educationadmin"。
解决方案
1. 升级flutter_inappwebview依赖版本
AGP 8.0强制要求所有Android模块必须声明namespace,旧版本的flutter_inappwebview未适配该规则。打开项目的pubspec.yaml,将该依赖升级到适配AGP8.0的最新稳定版:
dependencies: flutter_inappwebview: ^6.0.0 # 或更高版本
执行flutter pub get更新依赖。
2. 临时修复:自动为第三方模块补全namespace
若暂时无法升级依赖,可修改顶层build.gradle,添加自动配置逻辑:
subprojects { afterEvaluate { project -> if (project.hasProperty('android')) { project.android { if (!project.android.namespace) { def manifestFile = file("src/main/AndroidManifest.xml") if (manifestFile.exists()) { def manifestContent = manifestFile.getText() def packageMatcher = manifestContent =~ /package="([^"]+)"/ if (packageMatcher.find()) { namespace packageMatcher.group(1) } } } } } } }
这段代码会自动读取第三方模块AndroidManifest.xml中的package属性,将其设为模块的namespace,满足AGP8.0的要求。
3. 清理缓存并重新打包
执行以下命令清理缓存后重新打包:
flutter clean flutter build apk
内容的提问来源于stack exchange,提问作者Aryan Singh
相关产品推荐
相关产品推荐

