The build breaks after moving package from AndroidManifest.xml to namespace
Build fails after deleting the package attribute from AndroidManifest.xmlShort 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.
Fix it fast
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.xml2Match what you see, then jump to its fix
- 1
The 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
Imports 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
The installed app has a new ID, or Play rejects the upload as a different appThe application ID changed because it was never setFix - 4
The AGP Upgrade Assistant refuses to continue, or instrumented tests fail at runtimeThe test namespace equals the main namespaceFix
- 1
What does the error look like?
Build fails after deleting the package attribute from AndroidManifest.xmlThe 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:assembleDebugConfirmed by developer.android.com
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:compileDebugKotlinConfirmed by developer.android.com
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.myappConfirmed by developer.android.com
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:compileDebugAndroidTestKotlinConfirmed by developer.android.com
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
adb cannot start because something already holds its port
adb start-server failed
The adb server must bind local TCP port 5037, and every adb client talks to it there.
2 causes, each with a fixMobile apps
The phone shows as unauthorized and Android Studio will not run the app
adb devices lists the phone but Android Studio cannot deploy to it
An unauthorized phone means it has not trusted this computer's RSA key.
3 causes, each with a fixMobile 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 fixMobile apps
adb cannot connect to its daemon or start the server
* daemon started successfully *
The adb server is a background process on your computer that listens on TCP port 5037.
2 causes, each with a fixMobile apps
adb prints 'server version doesn't match this client; killing...'
adb server version doesn't match this client; killing...
adb runs a background server, and every adb command is a client.
3 causes, each with a fixMobile 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