Error:Attribute “color” has already been defined,

2019-06-16 12:01发布

问题:

I am trying to update my appcompat-v7 in Android Studio project from v20.0.0 to 21.0.0 for use material design component but I allways get the same error:

"Error:Attribute "color" has already been defined"

I have not idea about what to do for fix this error, I searched in internet but I cant get the answer. Here is my gradle:

android {

    compileOptions.encoding = "iso-8859-1"
    compileSdkVersion 21
    buildToolsVersion "21.1.1"

    defaultConfig {
        applicationId "com.test"
        minSdkVersion 11
        targetSdkVersion 22
    }

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

dependencies {

    compile fileTree(dir: 'libs', include: ['*.jar'])
 compile 'com.android.support:support-v4:20.0.0'
    compile 'com.google.http-client:google-http-client-gson:1.19.0'
    compile 'com.google.code.gson:gson:2.2.4'
    compile "com.android.support:appcompat-v7:21.0.+"

}

Here is the path where the conflict exist

C:\Users\Abel Dominguez\Documents\PROYECTOS_ANDROID\definitivos\d2\app\build\intermediates\exploded-aar\com.android.support\appcompat-v7\21.0.3\res\values\values.xml

and this is other error:

Error:Execution failed for task ':app:processDebugResources'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
    C:\Users\Abel Dominguez\AppData\Local\Android\sdk1\build-tools\build-tools-21.1.1\aapt.exe package -f --no-crunch -I C:\Users\Abel Dominguez\AppData\Local\Android\sdk1\platforms\android-21\android.jar -M C:\Users\Abel Dominguez\Documents\PROYECTOS_ANDROID\definitivos\d2\app\build\intermediates\manifests\full\debug\AndroidManifest.xml -S C:\Users\Abel Dominguez\Documents\PROYECTOS_ANDROID\definitivos\d2\app\build\intermediates\res\debug -A C:\Users\Abel Dominguez\Documents\PROYECTOS_ANDROID\definitivos\d2\app\build\intermediates\assets\debug -m -J C:\Users\Abel Dominguez\Documents\PROYECTOS_ANDROID\definitivos\d2\app\build\generated\source\r\debug -F C:\Users\Abel Dominguez\Documents\PROYECTOS_ANDROID\definitivos\d2\app\build\intermediates\res\resources-debug.ap_ --debug-mode --custom-package com.wherefriend -0 apk --output-text-symbols C:\Users\Abel Dominguez\Documents\PROYECTOS_ANDROID\definitivos\d2\app\build\intermediates\symbols\debug
Error Code:
    1
Output:
    C:\Users\Abel Dominguez\Documents\PROYECTOS_ANDROID\definitivos\d2\app\build\intermediates\res\debug\values\values.xml:94: error: Attribute "color" has already been defined

回答1:

My problem was solved after I read the answer by @petey. If you look at the line displayed in error message you can precisely determine which attribute is causing problem.

In my case, it was an attribute named color in a custom attrs xml file. That custom view wasn't used so I just commented that line and problem was solved.

Possible solution steps

  1. Check error output to find the path to the file and line number that is causing problems

  2. Go to that file through your file system explorer and look for the line in question

  3. That line should mention what (custom) view has an attribute that is already defined somewhere.

  4. Back in the project in you IDE, find that attribute and if it is not used comment, otherwise if it IS used, change it's name.



回答2:

You should remove this line

compile 'com.android.support:support-v4:20.0.0'

and use the same dependency used by appcompat:

compile 'com.android.support:support-v4:21.0.+'

Also I suggest to fix the 21.0.3 instead of 21.0.+



回答3:

Gradle Resource Merger merges all resource folders from all dependencies and place into single folder. In case there are duplicates build process will fail.

Fortunately, if you look below under Output: label, you will find the right path to the problem.

Here is an example

in your case it is android-support-v7-appcompat\res\values\attrs.xml:476: error: Attribute "attributeName" has already been defined

You can also build your project from command line and get the right path. attributeName Inside values\attrs.xml file on line 476 you would find a with property named "attributeName". Most probably it is your own styleable that you have to change to get rid of the duplicate.

So now, when you know the reason, you can locate that property in your project module and replace it with different name.



回答4:

In some case this might help you. This is not a specific answer though.

tools:attr markers

There can be be many attribute related markers on any particular element to resolve all attributes in conflict.

<tools:strict=”x, y, z”>

Default implicit mode for attribute, generate an error when trying the merge lower priority attribute declaration with a different value.

<tools:remove=”x, y, z”>

Remove the x, y, z attributes from any lower priority declaration when merging in.

<tools:replace=”x, y, z”>

Replace the x, y, z attributes from any lower priority declaration with the provided value (must be present on the same node).

REF: http://tools.android.com/tech-docs/new-build-system/user-guide/manifest-merger#TOC-tools:attr-markers