Android已配置模块命名空间仍报“未指定命名空间”错误,如何解决?
问题
我有主应用模块:app,以及:authentication、:shared两个子模块,各模块的build.gradle配置如下:
项目根目录build.gradle
buildscript { extra["compose_version"] = "1.5.4" repositories { google() mavenCentral() } dependencies { classpath("org.jetbrains.kotlin:kotlin-serialization:1.9.21") classpath("com.google.dagger:hilt-android-gradle-plugin:2.44") classpath("com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3") classpath("com.github.dcendents:android-maven-gradle-plugin:1.5") classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:2.0.0-Beta2") } } // Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { id("com.android.application") version "8.2.0" id("org.jetbrains.kotlin.android") version "2.0.0-Beta2" id("com.google.dagger.hilt.android") version "2.44" id("com.google.devtools.ksp") version "1.9.20-1.0.14" }
:app模块build.gradle
... android { namespace = "com.example.myapp" compileSdk = 34 defaultConfig { applicationId = "com.example.efest" minSdk = 24 targetSdk = 34 versionCode = 1 versionName = "1.0" } } ...
:authentication模块build.gradle
... android { namespace = "com.example.authentication" compileSdk = 34 } ...
:shared模块build.gradle
... android { namespace = "com.example.shared" compileSdk = 34 } ...
但编译时仍报错:
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.
我已经给每个模块都指定了namespace,请问哪里遗漏了?该怎么解决?
解决方案
- 确认模块插件配置:
:authentication和:shared作为库模块,必须在build.gradle顶部应用com.android.library插件,否则AGP无法识别模块类型,会忽略android块中的namespace配置。示例配置:plugins { id("com.android.library") id("org.jetbrains.kotlin.android") } - 清理Gradle缓存并同步:执行以下步骤强制刷新配置:
- 点击IDE中的
File > Sync Project with Gradle Files - 运行终端命令:
./gradlew clean build --refresh-dependencies
缓存残留可能导致新配置无法生效,这是解决此类问题的常用手段。
- 点击IDE中的
- 移除AndroidManifest中的package属性:如果模块的AndroidManifest.xml里还保留了
package属性,会和build.gradle的namespace产生冲突。建议完全移除Manifest中的package,统一使用build.gradle的namespace配置。 - 修复插件版本兼容性:当前使用的AGP 8.2.0和Kotlin 2.0.0-Beta2属于跨版本组合,存在兼容性风险。建议将Kotlin插件版本降级到稳定版(如1.9.21),保持与AGP版本的匹配。
- 排查隐藏模块:检查项目中是否存在其他未配置namespace的子模块(比如新增的模块、被忽略的模块),这类模块也会触发该错误提示。
内容的提问来源于stack exchange,提问作者code_dude
相关产品推荐
相关产品推荐

