Flutter打包StackFood Android APK报错:未指定Namespace
StackFood Android打包APK失败(Namespace未指定)解决方案
问题描述
编程新手,购买StackFood多餐厅外卖应用后,按官方文档配置Android Studio打包APK时失败,错误信息如下:
FAILURE: Build failed with an exception. * Where: Build file 'C:\Users\mogha\OneDrive\Desktop\YumYum\codecanyon-8CoFNnIb-stackfood-multi-restaurant-food-delivery-app-with-laravel-admin-and-restaurant-panel-v10\User app and web\android\build.gradle' line: 34 * What went wrong: A problem occurred evaluating root project 'android'. > A problem occurred configuring project ':app'. > Could not create an instance of type com.android.build.api.variant.impl.ApplicationVariantImpl. > Namespace not specified. Specify a namespace in the module's build file. See https://d.android.com/r/tools/upgrade-assistant/set-namespace for information about setting the namespace. If you've specified the package attribute in the source AndroidManifest.xml, you can use the AGP Upgrade Assistant to migrate to the namespace value in the build file. Refer to https://d.android.com/r/tools/upgrade-assistant/agp-upgrade-assistant for general information about using the AGP Upgrade Assistant. * 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 29s Running Gradle task 'assembleRelease'... 38.1s Gradle task assembleRelease failed with exit code 1
flutter doctor检测结果:
Doctor summary (to see all details, run flutter doctor -v): [√] Flutter (Channel stable, 3.16.3, on Microsoft Windows [Version 10.0.22621.2715], locale en-GB) [√] Windows Version (Installed version of Windows is version 10 or higher) [√] Android toolchain - develop for Android devices (Android SDK version 34.0.0) [√] Chrome - develop for the web [X] Visual Studio - develop Windows apps X Visual Studio not installed; this is necessary to develop Windows apps. ! Doctor found issues in 1 category.
解决方案
错误核心是Namespace未指定,新版Android Gradle Plugin(AGP)要求在模块级build.gradle中显式配置namespace,步骤如下:
- 打开项目路径下的
android/app/build.gradle文件 - 在
android代码块内添加namespace配置,值为你原AndroidManifest.xml文件中package属性对应的包名(比如原Manifest里是package="com.yourcompany.stackfood",就填这个值):android { namespace "com.yourcompany.stackfood" // 替换为你的实际包名 compileSdk 34 // 已有配置,无需修改 defaultConfig { applicationId "com.yourcompany.stackfood" // 需与namespace保持一致 // 其他原有配置... } // 其他原有配置... } - 检查
android/app/src/main/AndroidManifest.xml,确保里面的package属性和上述namespace值一致(可以保留package属性,也可以移除,新版本AGP会优先使用build.gradle中的namespace) - 保存文件后,重新执行打包命令:
flutter build apk --release
注:flutter doctor中Visual Studio未安装的问题不影响Android APK打包,无需处理。
内容的提问来源于stack exchange,提问作者Ahmed Moghazy
相关产品推荐
相关产品推荐

