Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

The build breaks after moving package from AndroidManifest.xml to namespace

ErrorBuild fails after deleting the package attribute from AndroidManifest.xml

Short answer

Since AGP 8.0 the package name of a module comes from the namespace property in the module's build file, not from the manifest. If the build fails after you remove package, the namespace is missing or wrong, the application ID changed with it, or the test namespace collides. Set all three explicitly.

  • Applies to AGP 8.x, AGP 9, Gradle 8.x, Android Studio
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print what the module declares now: its namespace, its application ID and the package attribute in the manifest. All three should be set on purpose.

    grep -n "namespace\|applicationId\|testNamespace" app/build.gradle*
    grep -n "package=" app/src/main/AndroidManifest.xml
  2. 2Match what you see, then jump to its fix

    1. 1The module build file has no namespace line, and the manifest no longer has a package attributeThe module has no namespace in its build fileFix
    2. 2Imports of the R class no longer resolve in files that sit in the old packageThe namespace is different from the package that holds your codeFix
    3. 3The installed app has a new ID, or Play rejects the upload as a different appThe application ID changed because it was never setFix
    4. 4The AGP Upgrade Assistant refuses to continue, or instrumented tests fail at runtimeThe test namespace equals the main namespaceFix

What does the error look like?

  • Build fails after deleting the package attribute from AndroidManifest.xml
  • The generated R class lands in a different package than the one your code imports

Cause 1 of 4

The module has no namespace in its build file

1Check

In AGP 8.0 and later the namespace must be set in the module-level build script, not in the manifest. The DSL property exists since AGP 7.3, so you can add it before you upgrade.

grep -n "namespace" */build.gradle* build.gradle*

2Fix

Add the namespace to every module, library modules included, using the package your code lives in.

// build.gradle.kts
android {
    namespace = "com.example.myapp"
}

3Verify

Sync the project and build the module.

./gradlew :app:assembleDebug

Confirmed by developer.android.com

Did this fix it?

Cause 2 of 4

The namespace is different from the package that holds your code

1Check

The namespace becomes the Java or Kotlin package of the generated R and BuildConfig classes, and should match the base package where your activities and other code live. Sub-packages must import R from the namespace.

find app/src/main -name "MainActivity.*"

2Fix

Set the namespace to the base package of the code, or move the code to the package named by the namespace.

android {
    namespace = "com.example.myapp"   // the package of MainActivity
}

3Verify

Rebuild and check that R resolves in the sources.

./gradlew :app:compileDebugKotlin

Confirmed by developer.android.com

Did this fix it?

Cause 3 of 4

The application ID changed because it was never set

1Check

When applicationId is not set explicitly, it takes the value of the namespace. Changing the namespace then changes the application ID too.

grep -n "applicationId" app/build.gradle*

2Fix

Set applicationId explicitly to the published value before you touch the namespace.

android {
    namespace = "com.example.myapp"
    defaultConfig {
        applicationId = "com.example.myapp"
    }
}

3Verify

Install the build and check the package name that the device reports.

adb shell pm list packages | grep com.example.myapp

Confirmed by developer.android.com

Did this fix it?

Cause 4 of 4

The test namespace equals the main namespace

1Check

AGP can infer the test namespace wrongly. The Upgrade Assistant blocks the upgrade when the main and test namespaces are the same. Do not set testNamespace to the same value as namespace.

grep -n "testNamespace" app/build.gradle*

2Fix

Give testNamespace its own value, and update test sources that depend on the old package.

android {
    namespace = "com.example.myapp"
    testNamespace = "com.example.myapp.test"
}

3Verify

Run the instrumented tests or compile them.

./gradlew :app:compileDebugAndroidTestKotlin

Confirmed by developer.android.com

Did this fix it?

How do I stop it coming back?

  • Set namespace and applicationId explicitly in every module and keep them in the same file.
  • Use Tools, AGP Upgrade Assistant when you upgrade AGP, and commit before you run it.
  • Never change the application ID of a published app.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Mobile apps

Android Studio cannot pair the phone over Wi-Fi

The Pair devices over Wi-Fi dialog lists no device

Wireless pairing needs the phone on Android 11 or higher and on the same Wi-Fi network as the computer, with current Platform-Tools.

4 causes, each with a fix

Mobile apps

adb tcpip 5555 fails because no device is found

adb: more than one device/emulator

adb tcpip only works while the device is attached and listed by adb, because it tells the phone to start listening on port 5555.

4 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.