配置:audio_service模块时Gradle报错:未指定Namespace
解决Gradle构建错误:未指定Namespace导致无法创建LibraryVariantBuilderImpl实例
错误详情
FAILURE: Build failed with an exception. * What went wrong: A problem occurred configuring project ':audio_service'. > 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 like so: android { namespace 'com.example.namespace' } If the package attribute is specified in the source AndroidManifest.xml, it can be migrated automatically to the namespace value in the build.gradle file using the AGP Upgrade Assistant; please refer to https://developer.android.com/studio/build/agp-upgrade-assistant for more information. BUILD FAILED in 2m 56s Exception: Gradle task assembleDebug failed with exit code 1
修复步骤
- 定位正确的配置文件:确保修改的是
:audio_service模块下的build.gradle(而非项目根目录的build.gradle)。 - 添加Namespace配置:在该模块
build.gradle的android代码块内,补充namespace属性,值为模块对应的包名,示例:
若模块的android { namespace 'com.yourpackage.audio_service' // 保留原有compileSdk、defaultConfig等配置 }AndroidManifest.xml中有package属性,namespace的值必须与该package完全一致。 - 清理缓存后重构:执行命令清理Gradle缓存,避免旧配置残留:
之后重新运行构建命令。./gradlew clean - 排查常见错误点:不要把
namespace写在根目录配置文件中;检查包名拼写是否正确;AGP 7.0+版本强制要求namespace,不可省略。
内容的提问来源于stack exchange,提问作者Kerby Elpenord
相关产品推荐
相关产品推荐

