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

升级Android Studio Ladybug后Flutter项目Pusher包报Namespace错误

问题描述

升级Android Studio至Ladybug版本后,Flutter项目无法运行,所有Pusher相关包(pusher_client、pusher_client_fixed、pusher_client_x等)均出现构建错误,核心报错为:

A problem occurred configuring project ':pusher_client_x'.
> Could not create an instance of type com.android.build.api.variant.impl.LibraryVariantBuilderImpl.
   > Namespace not specified. Specify a namespace in the module's build file.

配置文件信息

app/build.gradle

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

android {
    namespace = "com.cartrackernigeria.ctntelematics"
    compileSdk = flutter.compileSdkVersion
    ndkVersion = flutter.ndkVersion

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_1_8
    }

    defaultConfig {
        applicationId = "com.cartrackernigeria.ctntelematics"
        minSdk = 21
        targetSdk = flutter.targetSdkVersion
        versionCode = flutter.versionCode
        versionName = flutter.versionName
    }

    buildTypes {
        release {
            signingConfig = signingConfigs.debug
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

flutter {
    source = "../.."
}

settings.gradle

pluginManagement {
    def flutterSdkPath = {
        def properties = new Properties()
        file("local.properties").withInputStream { properties.load(it) }
        def flutterSdkPath = properties.getProperty("flutter.sdk")
        assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
        return flutterSdkPath
    }()

    includeBuild("$flutterSdkPath/packages/flutter_tools/gradle")

    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
}

plugins {
    id "dev.flutter.flutter-plugin-loader" version "1.0.0"
    id "com.android.application" version "8.3.2" apply false
    id "org.jetbrains.kotlin.android" version "1.8.22" apply false
}

include ":app"

android/build.gradle

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:自动为第三方模块补全Namespace(推荐)

AGP 8.0+要求所有Android模块必须显式指定namespace,这些Pusher包的Android模块未适配该规则。可在android/build.gradle中添加自动补全逻辑:

subprojects {
    afterEvaluate { project ->
        if (project.hasProperty('android')) {
            android {
                // 从AndroidManifest.xml的package属性自动生成namespace
                if (!project.android.namespace) {
                    def manifestFile = file("src/main/AndroidManifest.xml")
                    if (manifestFile.exists()) {
                        def manifestContent = manifestFile.getText()
                        def packageMatch = manifestContent =~ /package="([^"]+)"/
                        if (packageMatch.find()) {
                            namespace = packageMatch.group(1)
                        }
                    }
                }
            }
        }
    }
}

替换原有subprojects块后,执行以下命令:

flutter clean
flutter pub get
flutter run

方案2:降级AGP版本(临时规避)

在settings.gradle中将AGP版本回退到8.1.0(该版本对Namespace的校验更宽松):

plugins {
    id "dev.flutter.flutter-plugin-loader" version "1.0.0"
    id "com.android.application" version "8.1.0" apply false
    id "org.jetbrains.kotlin.android" version "1.8.22" apply false
}

同样执行flutter clean和flutter pub get后重新构建。

方案3:更换适配AGP 8+的Pusher包

检查pub.dev上的Pusher相关包,选择已更新适配AGP 8.0+的版本或社区维护的适配包。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 14:52:29