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

升级Android项目Gradle版本时遇Namespace未指定错误

Gradle升级后已配置Namespace仍报错的解决方法

升级Android项目Gradle版本后,编译阶段触发以下错误:

Namespace not specified. Please specify a namespace in the module's build.gradle file like so:

android {
namespace 'com.example.namespace'
}

但模块的build.gradle中已明确定义namespace,仍出现该错误,相关配置如下:

相关配置内容

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:name="${applicationName}"
        android:icon="@mipmap/launcher_icon"
        android:label="xxxxxx">
        <activity
            ...
            
        </activity>
        
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>

模块build.gradle

...
android {
    namespace "mysite.com.myproject"
    compileSdkVersion flutter.compileSdkVersion
    ndkVersion flutter.ndkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }

    ...
    defaultConfig {
        applicationId "mysite.com.myproject"
        minSdkVersion 21
        targetSdkVersion flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    buildTypes {
        release {
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

buildscript配置段

buildscript {
    ext.kotlin_version = '1.9.0'
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:8.0.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

distributionUrl配置

distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-all.zip

解决方法

  • 检查所有子模块配置:项目中若存在多个Android模块(如library、feature模块),确保每个模块的build.gradle都添加了namespace声明,Gradle 8.0+要求所有Android模块必须显式配置namespace。
  • 清理缓存并重建:执行命令./gradlew clean清除编译缓存,再运行./gradlew assembleDebug重新编译,旧缓存可能导致配置未被正确识别。
  • 移除Manifest中的package属性:Gradle 8.0+中,namespace会替代AndroidManifest.xml里的package属性,若Manifest中仍保留package="xxx",可能引发冲突,建议删除该属性。
  • 匹配Flutter与Gradle版本:当前Flutter版本可能与Gradle 8.0.2不兼容,升级Flutter至稳定版(如3.10及以上),确保Flutter插件与AGP版本适配。
  • 验证AGP与Gradle版本对应:AGP 8.0.x需搭配Gradle 8.0-8.2版本,确认distributionUrl指向的Gradle版本(8.0)符合要求,若版本不匹配需调整其中一项。

内容的提问来源于stack exchange,提问作者DolDurma

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 16:42:53