Android build stops with 'resource linking failed' from AAPT2
Android resource linking failedShort answer
AAPT2 links your XML resources and manifest into one package, and it fails on the first thing it cannot resolve. The output after the error names a file and line. In practice it is a misplaced manifest element, a missing or extra @ in a resource reference, or an item name written with a resource type.
Fix it fast
1Find out which cause you have
The summary line is useless. Run the failing task with the stack trace and read the 'error:' lines above it, because each carries a file path and a line number.
./gradlew :app:processDebugResources --stacktrace2Match what you see, then jump to its fix
- 1
AndroidManifest.xml:NN: error: unknown element <action> found.A manifest element sits in the wrong parentFix - 2
expected color but got (raw string) color/colorPrimaryA resource reference is missing its @ or has one it should not haveFix - 3
style attribute 'attr/attr/my_attr (aka my.package:attr/attr/my_attr)' not foundA style item names a resource type inside its nameFix
- 1
What does the error look like?
Android resource linking failedAAPT2 aapt2-x.x.x-xxxxxxx-linux Daemon #0: Command failedAndroidManifest.xml:15: error: unknown element <action> found.error: style attribute 'attr/...' not found
Cause 1 of 3
A manifest element sits in the wrong parent
1Check
AAPT2 is strict about manifest nesting. An <action> must be inside an <intent-filter>, not directly inside <activity>. Open the line number the error gives.
sed -n '1,60p' app/src/main/AndroidManifest.xml2Fix
Move the element into its correct parent, as the manifest documentation shows.
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>3Verify
Run the resource task again.
./gradlew :app:processDebugResourcesConfirmed by developer.android.com
Cause 2 of 3
A resource reference is missing its @ or has one it should not have
1Check
In a style item, a reference to your own resource starts with @. A reference to the android namespace is written without a leading @ in the item name.
grep -rn "<item name=" app/src/main/res/values*/ | head -402Fix
Add the @ to the value, and remove the @ from the item name when it points at the android namespace.
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="android:windowEnterAnimation">@anim/slide_in</item>
</style>3Verify
Rebuild the debug variant.
./gradlew :app:assembleDebugConfirmed by developer.android.com
Cause 3 of 3
A style item names a resource type inside its name
1Check
AAPT2 no longer accepts a resource type written in the name attribute, such as name="attr/my_attr".
grep -rn 'name="attr/' app/src/main/res/2Fix
Declare the type with the type attribute instead.
<style name="childStyle" parent="parentStyle">
<item type="attr" name="my_attr">@color/pink</item>
</style>3Verify
Run the resource task and confirm the error is gone.
./gradlew :app:processDebugResourcesConfirmed by developer.android.com
How do I stop it coming back?
- Treat the file and line in the 'error:' line as the fix location, not the Gradle summary.
- Run ./gradlew assembleDebug before pushing any change to the manifest or to styles.
- Keep style items in the documented form: @type/name for values, android: prefix for framework attributes.
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
The editor cannot find R, so every R.string and R.id turns red
Unresolved reference: R
R is a class that the build generates from your res folder, in the package named by the module's namespace.
4 causes, each with a fixMobile 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
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 fixMobile apps
The Android Emulator says /dev/kvm is not found
KVM acceleration can NOT be used
The emulator wants a hypervisor and cannot find one.
4 causes, each with a fix