Skip to main content

Fixing 'Namespace not specified' Errors in Flutter Android Builds (AGP 8.0+)

 You have likely encountered this error immediately after upgrading your Flutter project's dependencies or bumping your Android Gradle Plugin (AGP) version to 8.0 or higher:

> Task :app:processDebugMainManifest FAILED
Error: Namespace not specified. Please specify a namespace in the module's build.gradle file like so:

android {
    namespace 'com.example.myapp'
}

This build failure is not a Flutter bug; it is a deliberate architectural change in the Android build ecosystem that breaks older project templates.

The Root Cause: AGP 8.0 and the Manifest

Prior to Android Gradle Plugin 8.0, the package attribute in your AndroidManifest.xml served two distinct purposes:

  1. Application ID: It uniquely identified your app on the Google Play Store and on the device.
  2. Java/Kotlin Namespace: It determined the package structure for generated classes like R.java (resources) and BuildConfig.java.

AGP 8.0 officially removed support for using the manifest package attribute to define the compilation namespace. This forces a decoupling of the Application ID (runtime identity) from the Namespace (compile-time code structure).

If your Flutter project was created before this change, your Gradle configuration relies on the manifest to generate the R class. When you upgrade AGP, the build system looks for a namespace property in build.gradle. Finding none, it throws the "Namespace not specified" error.

The Fix

To resolve this, you must migrate the namespace definition from XML to Gradle.

1. Identify Your Package Name

Open your android/app/src/main/AndroidManifest.xml. Locate the package attribute in the root <manifest> tag.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.my_flutter_app"> <!-- Copy this string -->
    ...
</manifest>

2. Update the App-Level Gradle File

Open android/app/build.gradle. Inside the android block, add the namespace property. Paste the string you copied from the manifest.

android {
    // START: Add this line
    namespace "com.example.my_flutter_app"
    // END: Add this line

    compileSdk flutter.compileSdkVersion
    ndkVersion flutter.ndkVersion

    defaultConfig {
        // Note: applicationId usually remains here or refers to the namespace default
        applicationId "com.example.my_flutter_app" 
        minSdkVersion flutter.minSdkVersion
        targetSdkVersion flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }
    
    // ... rest of configuration
}

3. Clean the Manifest

Return to android/app/src/main/AndroidManifest.xmlRemove the package attribute entirely. The file should now look like this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- The package attribute is gone -->
    
    <application
        android:label="my_flutter_app"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher">
        
        <!-- ... activities and metadata ... -->
        
    </application>
</manifest>

Note: You must repeat this cleanup step for src/debug/AndroidManifest.xml and src/profile/AndroidManifest.xml if they contain the package attribute.

4. Verify MainActivity Imports

If your namespace matches the original package name, your project will build immediately. However, if you changed the string during this migration, you must update the imports in your Android entry point.

Check android/app/src/main/kotlin/.../MainActivity.kt (or .java). Ensure the package declaration matches your new namespace:

package com.example.my_flutter_app // Must match the namespace in build.gradle

import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity() {
}

Why This Works

By moving the definition to build.gradle, we satisfy the AGP 8.0 requirement for an explicit DSL namespace.

  1. Gradle Sync: When Gradle syncs, it reads namespace "com.example.my_flutter_app".
  2. Resource Compilation: The build system generates com.example.my_flutter_app.R and com.example.my_flutter_app.BuildConfig.
  3. App Identity: The applicationId in defaultConfig continues to control the installation ID on the device.

This separation allows you to refactor your internal code structure (the namespace) without changing the applicationId, effectively preventing you from losing your app's history and user base on the Play Store during major refactors.

Conclusion

The "Namespace not specified" error is a mandatory migration step for modern Android development. By explicitly defining the namespace in your Gradle build scripts and cleaning up your Manifests, you align your Flutter project with modern Android standards, ensuring compatibility with AGP 8.0 and beyond.