'Build android error at stage app:mergeDebugResources, ParseError AttributeNSNotUnique

I've been trying to build my Android Application on Android Studio 4.2 with Gradle 4.2.1, but I am faced with the following error of which I was only able to find one question one month ago, and another one from today which was not solved neither.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:mergeDebugResources'.
> A failure occurred while executing com.android.build.gradle.internal.res.ResourceCompilerRunnable
   > Resource compilation failed. Check logs for details.

* Try:
Run with --debug option to get more log output. Run with --scan to get full insights.

* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:mergeDebugResources'.
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.lambda$executeIfValid$1(ExecuteActionsTaskExecuter.java:200)
    at org.gradle.internal.Try$Failure.ifSuccessfulOrElse(Try.java:263)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeIfValid(ExecuteActionsTaskExecuter.java:198)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:179)
    at org.gradle.api.internal.tasks.execution.CleanupStaleOutputsExecuter.execute(CleanupStaleOutputsExecuter.java:109)
    at org.gradle.api.internal.tasks.execution.FinalizePropertiesTaskExecuter.execute(FinalizePropertiesTaskExecuter.java:46)
    at org.gradle.api.internal.tasks.execution.ResolveTaskExecutionModeExecuter.execute(ResolveTaskExecutionModeExecuter.java:62)
    ... 12 more
Caused by: org.gradle.workers.internal.DefaultWorkerExecutor$WorkExecutionException: A failure occurred while executing com.android.build.gradle.internal.res.ResourceCompilerRunnable
    at org.gradle.workers.internal.DefaultWorkerExecutor$WorkItemExecution.waitForCompletion(DefaultWorkerExecutor.java:336)
    at org.gradle.internal.work.DefaultAsyncWorkTracker.waitForItemsAndGatherFailures(DefaultAsyncWorkTracker.java:142)
    at org.gradle.internal.work.DefaultAsyncWorkTracker.waitForItemsAndGatherFailures(DefaultAsyncWorkTracker.java:94)
    at org.gradle.internal.work.DefaultAsyncWorkTracker.waitForAll(DefaultAsyncWorkTracker.java:80)
    at org.gradle.internal.work.DefaultAsyncWorkTracker.waitForCompletion(DefaultAsyncWorkTracker.java:68)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter$3.run(ExecuteActionsTaskExecuter.java:563)
    at org.gradle.internal.operations.DefaultBuildOperationRunner$1.execute(DefaultBuildOperationRunner.java:29)
    at org.gradle.internal.operations.DefaultBuildOperationRunner$1.execute(DefaultBuildOperationRunner.java:26)
    ... 93 more
Caused by: com.android.aaptcompiler.ResourceCompilationException: Resource compilation failed. Check logs for details.
    at com.android.aaptcompiler.ResourceCompiler.compileResource(ResourceCompiler.kt:121)
    at com.android.build.gradle.internal.res.ResourceCompilerRunnable$Companion.compileSingleResource(ResourceCompilerRunnable.kt:31)
    at com.android.build.gradle.internal.res.ResourceCompilerRunnable.run(ResourceCompilerRunnable.kt:15)
    at com.android.build.gradle.internal.profile.ProfileAwareWorkAction.execute(ProfileAwareWorkAction.kt:74)
    at org.gradle.workers.internal.DefaultWorkerServer.execute(DefaultWorkerServer.java:63)
    ... 10 more
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[22,347]
Message: http://www.w3.org/TR/1999/REC-xml-names-19990114#AttributeNSNotUnique?androidx.constraintlayout.widget.ConstraintLayout&layout_width&http://schemas.android.com/apk/res/android
    at java.xml/com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:652)
    at java.xml/com.sun.xml.internal.stream.XMLEventReaderImpl.nextEvent(XMLEventReaderImpl.java:83)
    at com.android.aaptcompiler.XmlProcessor.process(XmlProcessor.kt:116)
    at com.android.aaptcompiler.ResourceCompiler.compileXml(ResourceCompiler.kt:267)
    at com.android.aaptcompiler.ResourceCompiler.access$compileXml(ResourceCompiler.kt:1)
    at com.android.aaptcompiler.ResourceCompiler$getCompileMethod$2.invoke(ResourceCompiler.kt:143)
    at com.android.aaptcompiler.ResourceCompiler$getCompileMethod$2.invoke(ResourceCompiler.kt)
    at com.android.aaptcompiler.ResourceCompiler.compileResource(ResourceCompiler.kt:118)
    ... 29 more

BUILD FAILED in 2s
17 actionable tasks: 1 executed, 16 up-to-date

Edited: This is my merged AndroidManifest.xml file, it looks like this without dependencies, still the message error remains the same and in the same line [22,347]:

Android Manifest Without Dependencies

I have also taken a look at all my *.xml files, from layouts to values, and android studio has validated them, so I don't understand the exception.

The part related to the Parse Error:

Message: http://www.w3.org/TR/1999/REC-xml-names-19990114#AttributeNSNotUnique?androidx.constraintlayout.widget.ConstraintLayout&layout_width&http://schemas.android.com/apk/res/android

Makes me think there is something wrong with my layouts, but the IDE says they are fine and render normally.

Dependencies on the build.gradle (previous image shows the manifest after they were commented out):

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])

    // Core
    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'androidx.annotation:annotation:1.2.0'

    // Singleton
    implementation group: 'javax.inject', name: 'javax.inject', version: '1'

    // Lifecycle
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'

    // UI
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'androidx.navigation:navigation-ui:2.3.5'

    // Room
    implementation 'androidx.room:room-runtime:2.3.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
    testImplementation 'junit:junit:4.12'
    annotationProcessor 'androidx.room:room-compiler:2.3.0'

    // Navigation
    implementation 'androidx.navigation:navigation-fragment:2.3.5'

    // Lombok
    compileOnly "org.projectlombok:lombok:1.18.12"
    annotationProcessor "org.projectlombok:lombok:1.18.12"

    // Retrofit
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:4.9.0'

    // JSON
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'

    // Logging
    implementation 'com.jakewharton.timber:timber:4.7.1'
}


Solution 1:[1]

The answer to this question is that the error and the stacktrace are actually two different problems.

There was a problem with one of my xml layouts, yes, but the biggest issue was that the version of gradle 4.2.1 has obfuscated how to find the problem.

So if you want to actually know what is failing, downgrade your gradle to 4.1.3 and gradle will now tell you where your xml is wrong. I recommend this whatever error you have, be it a parse error, a resource error or a prolog error.

Now, for the actual exception, Android Studio does not detect when an attribute is duplicated between the <layout> tag and another different layout, like constraintLayout, frameLayout, etc... so if you are using databinding, check that any extra layouts you are using don't have the same attribute. In fact I just recommend moving any attributes to your second, implementation specific layout and leave the databinding tag alone.

Solution 2:[2]

In my case, it was an issue with the layout tag. I was using layout_width and layout_height inside it because of which it was giving me the same error.

So anyone using data binding double check this and also check if you are using the same id's for different layouts.

Solution 3:[3]

In my case I just had to review some recent layouts I modified to see the layout editor presenting the error. In my case it was a view with a duplicated id.

This build error result is a shame.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Melferas
Solution 2 Yachint
Solution 3 Lucas B.