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

Flutter集成agora_rtm时Gradle构建失败:未指定Namespace

Flutter构建失败问题:agora_rtm模块未配置Namespace

错误场景

调试模式下启动lib/main.dart到sdk gphone x86时构建失败,错误信息如下:

FAILURE: Build failed with an exception.

问题原因:
配置项目':agora_rtm'时出现异常,无法创建com.android.build.api.variant.impl.LibraryVariantBuilderImpl实例。
未指定Namespace,请在模块构建文件中配置Namespace
若已在AndroidManifest.xml中指定package属性,可使用AGP Upgrade Assistant迁移至构建文件的Namespace配置

排查建议:
使用--stacktrace选项获取堆栈跟踪;使用--info或--debug选项查看更多日志;使用--scan获取完整分析信息;访问官方Gradle帮助获取更多帮助。

BUILD FAILED in 1s
Error: Gradle task assembleDebug failed with exit code 1
Exited (1).

当前配置文件

android/app/src/main/AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <application
        android:label="babble"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:launchMode="singleTop"
            android:taskAffinity=""
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
    <!-- Required to query activities that can process text -->
    <queries>
        <intent>
            <action android:name="android.intent.action.PROCESS_TEXT"/>
            <data android:mimeType="text/plain"/>
        </intent>
    </queries>
</manifest>

android/app/build.gradle

plugins {
    id "com.android.application"
    // START: FlutterFire Configuration
    id 'com.google.gms.google-services'
    // END: FlutterFire Configuration
    id "kotlin-android"
    // The Flutter Gradle Plugin must be applied after the Android and Kotlin Gradle plugins.
    id "dev.flutter.flutter-gradle-plugin"
}

android {
    namespace = "com.example.babble"
    compileSdk = flutter.compileSdkVersion
    ndkVersion = flutter.ndkVersion

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

    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_1_8
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID
        applicationId = "com.example.babble"
        minSdk = 23
        targetSdk = flutter.targetSdkVersion
        versionCode = flutter.versionCode
        versionName = flutter.versionName
    }

    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 = "../.."
}

已尝试操作

已执行flutter clean、flutter pub get、flutter run,问题未解决。

可行解决方法

  • 升级agora_rtm依赖版本:检查pub.dev上该包的最新版本,更新pubspec.yaml中的版本号后重新执行flutter pub get。旧版本的agora_rtm可能未适配AGP的Namespace要求。
  • 手动为agora_rtm模块添加Namespace:
    1. 打开项目中.pub-cache/hosted/pub.dev/agora_rtm-<版本号>/android/build.gradle文件
    2. 在android块内添加namespace = "io.agora.rtm.flutter"(或对应官方包名)
    3. 保存后重新执行flutter run
  • 降级Android Gradle Plugin(AGP)版本:如果当前AGP版本过高,可尝试降级到兼容版本。修改android/build.gradle中com.android.application的版本,同步Gradle版本后重新构建。
  • 使用AGP Upgrade Assistant迁移配置:在Android Studio中打开项目,通过Tools > AGP Upgrade Assistant工具,按照提示完成Namespace配置迁移,确保所有模块符合AGP要求。
  • 检查Flutter SDK兼容性:确保使用的Flutter SDK版本与agora_rtm依赖兼容,尝试升级或降级Flutter SDK后重新构建。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 16:04:51