You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter APK构建失败:google_mlkit_barcode_scanning未指定Namespace

解决Android构建时未指定Namespace的错误

问题描述

构建项目时触发以下错误:

FAILURE: Build failed with an exception.

配置项目':google_mlkit_barcode_scanning'时出现问题。
无法创建com.android.build.api.variant.impl.LibraryVariantBuilderImpl实例。
未指定Namespace。请在模块的构建文件中指定Namespace

构建任务assembleRelease执行失败,耗时4.8秒,退出码1。

用户提供的AndroidManifest.xml配置:

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET" />
<application
    android:label="inspection_app"
    android:name="${applicationName}"
    android:icon="@mipmap/ic_launcher">
    <activity
        android:name=".MainActivity"
        android:exported="true"
        android:launchMode="singleTop"
        android:taskAffinity=""
        android:theme="@style/LaunchTheme"
        android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
        android:hardwareAccelerated="true"
        android:windowSoftInputMode="adjustResize">
        <meta-data
          android:name="io.flutter.embedding.android.NormalTheme"
          android:resource="@style/NormalTheme"
          />
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <meta-data
        android:name="flutterEmbedding"
        android:value="2" />
</application>
<queries>
    <intent>
        <action android:name="android.intent.action.PROCESS_TEXT"/>
        <data android:mimeType="text/plain"/>
    </intent>
</queries>

解决步骤

1. 为google_mlkit_barcode_scanning模块添加Namespace

找到该模块的build.gradle(或build.gradle.kts)文件,在android代码块中添加namespace配置:

Groovy 版本(build.gradle)

android {
    namespace "com.example.yourpackage" // 替换为实际包名,可参考项目其他模块的包名
    // 其他原有配置...
}

Kotlin DSL 版本(build.gradle.kts)

android {
    namespace = "com.example.yourpackage"
    // 其他原有配置...
}

2. 可选:通过AGP升级助手迁移(若原Manifest含package属性)

如果项目之前在AndroidManifest.xml中指定过package属性,可使用AGP Upgrade Assistant工具自动将该属性值迁移到构建文件的namespace中。

额外调试建议

  • 执行构建命令时添加--stacktrace参数,获取完整堆栈跟踪排查深层问题
  • 使用--info或--debug参数查看更详细的日志输出
  • 用--scan参数生成完整的构建分析报告

内容的提问来源于stack exchange,提问作者Oke Tosin Emmanuel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 16:40:12