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

Flutter集成google_mobile_ads构建APK时遇Namespace未指定报错

问题:Flutter构建APK时:google_mobile_ads项目提示“未指定命名空间”

执行flutter build apk --release构建Android应用时,出现:google_mobile_ads项目配置失败的错误,提示“Namespace not specified”,但已在App级build.gradle中配置了namespace。


报错详情

核心错误

* What went wrong:
A problem occurred configuring project ':google_mobile_ads'.
> 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'
     }

完整CMD输出

PS C:\Users\hp\Desktop\myprojects\tripp> flutter build apk --release


FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':google_mobile_ads'.
> 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.

* 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 2s
Running Gradle task 'assembleRelease'...                            3.3s
Gradle task assembleRelease failed with exit code 1
PS C:\Users\hp\Desktop\myprojects\tripp>

相关配置文件

App级build.gradle

plugins {
    id "com.android.application"
    id "kotlin-android"
    id "dev.flutter.flutter-gradle-plugin"
}

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

android {
    namespace "com.example.tripp"
    compileSdkVersion = 33
    ndkVersion flutter.ndkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.busdekho.tripp"
        // You can update the following values to match your application needs.
        // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
        minSdkVersion 33
        targetSdkVersion flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        
        buildFeatures {
        buildConfig = true
        }
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation 'com.google.android.gms:play-services-ads:22.3.0'
}

项目级build.gradle

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

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

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(':app')
}

tasks.register("clean", Delete) {
    delete rootProject.buildDir
}

解决方案

方案1:升级google_mobile_ads插件版本

旧版本的google_mobile_ads未适配AGP 8.0的namespace强制要求,在pubspec.yaml中将插件升级到最新稳定版,例如:

dependencies:
  google_mobile_ads: ^3.1.0 # 替换为当前最新稳定版

执行flutter pub get更新依赖后重新构建。

方案2:给所有子模块默认配置namespace

如果暂时无法升级插件,可在项目级build.gradle中添加全局配置,自动为缺失namespace的子模块生成默认命名空间:
修改项目级build.gradle的subprojects块,添加以下内容:

subprojects {
    afterEvaluate { project ->
        if (project.hasProperty('android')) {
            android {
                // 可根据需要调整命名空间规则,这里用项目名作为后缀
                namespace project.group ?: "com.example.${project.name}"
            }
        }
    }
}

方案3:确保AGP与Gradle版本匹配

AGP 8.0需要搭配Gradle 8.0及以上版本,检查gradle/wrapper/gradle-wrapper.properties中的distributionUrl:

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

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 03:09:50