'Android resource compilation failed

I am developing an android app where I am in need of design support library so when I add implementation 'com.android.support:design:27.1.1' into my build.gradle(app) then the following error appears:

Caused by: org.gradle.internal.UncheckedException: java.util.concurrent.ExecutionException: com.android.builder.internal.aapt.v2.Aapt2Exception: Android resource compilation failed MyAppDirectory\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values\values.xml:2489: error: duplicate value for resource 'attr/itemBackground' with config ''.

But when I remove the dependency implementation 'com.android.support:design:27.1.1' then the error disappears and gradle build successfully.

My Project build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

repositories {
    google()
    jcenter()
}
dependencies {
  //        classpath 'com.android.tools.build:gradle:3.1.3'
    classpath 'com.android.tools.build:gradle:3.3.0-alpha04'


    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
     }
 }

allprojects {
repositories {
    google()
    jcenter()


   }
}

task clean(type: Delete) {
delete rootProject.buildDir
}

My app build.gradle

apply plugin: 'com.android.application'

android {
compileSdkVersion 27
buildToolsVersion "27.0.3"
defaultConfig {
    applicationId "myapplicationid"
    minSdkVersion 16
    targetSdkVersion 27
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
      }
   }
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'

implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.android.support:design:27.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

//third party libraries
implementation 'com.hbb20:ccp:2.2.2'

implementation 'info.hoang8f:fbutton:1.0.5'
implementation 'com.chaos.view:pinview:1.4.0'
implementation 'de.hdodenhof:circleimageview:2.2.0'
}

Note: I have already cleaned and rebuilded the project and I have also invalidated and cleared the cache



Solution 1:[1]

Check for an apostrophe (') for any string value in arrays.xml or strings.xml and make sure you add a backslash(\) before it

Example 1: <string-array> <item>example's</item> </string-array>

to

<string-array> <item>example\'s</item> </string-array>

Example 2: <string>example's</string>

to

<string>example\'s</string>

Solution 2:[2]

It seems one of the libraries you have used have the same name attribute with support library. I suggest you start exclude them starting from espresso, then see if you can fix it, and continuously set this to other libraries until you find which library have used it.

androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})

Solution 3:[3]

In my case error appeared because for a view that generated error, there was excessive xml namespace definition ,that is : xmlns:android="http://schemas.android.com/apk/res/android"

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/sb_swipe_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

When Android Studio is building, error appears :

Android resource compilation failed
C:\dev\hyperoptic.infinity\mobile-fit\app\build\intermediates\incremental\mergeDebugResources\stripped.dir\layout\activity_spine_build_job_report.xml:29: error: duplicate attribute.
C:\dev\hyperoptic.infinity\mobile-fit\app\build\intermediates\incremental\mergeDebugResources\stripped.dir\layout\activity_spine_build_job_report.xml: error: file failed to compile.

This means that we need to delete xmlns, as in compiled xml it appears in three places !

So, deleting xmlns:android="http://schemas.android.com/apk/res/android" resolved this problem.

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
Solution 2 matio
Solution 3 dobrivoje