Error:Attribute "color" has already been defined, update appcompat v-7

Error:Attribute "color" has already been defined, update appcompat v-7

我正在尝试将 Android Studio 项目中的 appcompat-v7 从 v20.0.0 更新到 21.0.0 以使用 material 设计组件,但我总是遇到相同的错误:

"Error:Attribute "颜色"已经被定义"

我不知道如何解决这个错误,我在互联网上搜索但找不到答案。 这是我的 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.+"

}

这里是存在冲突的路径

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

这是另一个错误:

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

看了@petey的回答,我的问题解决了。如果您查看错误消息中显示的行,您可以准确地确定导致问题的属性。

在我的例子中,它是自定义 attrs xml 文件中名为 color 的属性。那个自定义视图没有被使用,所以我只是评论了那一行,问题就解决了。

可能的解决步骤

  1. 检查错误输出以找到导致问题的文件路径和行号

  2. 通过文件系统资源管理器转到该文件并查找有问题的行

  3. 该行应该提到什么(自定义)视图具有已在某处定义的属性。

  4. 回到你的项目IDE,找到那个属性,如果它没有被使用注释,否则如果它被使用,改变它的名字。

你应该删除这一行

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

并使用 appcompat 使用的相同依赖项:

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

我还建议修复 21.0.3 而不是 21.0.+

Gradle 资源合并 将所有依赖项中的所有资源文件夹合并到一个文件夹中。如果存在重复构建过程将失败。

幸运的是,如果您查看下面的输出:标签,您会找到问题的正确路径。

这是一个例子

你的情况是 android-support-v7-appcompat\res\values\attrs.xml:476: error: Attribute "attributeName" has already been defined

您还可以从命令行构建项目并获得正确的路径。 attributeName 在第 476 行的 values\attrs.xml 文件中,您会发现一个名为 "attributeName" 的 属性。很可能这是您自己的风格,您必须更改以摆脱重复。

所以现在,当您知道原因后,您可以在您的项目模块中找到 属性 并将其替换为不同的名称。

在某些情况下,这可能对您有所帮助。这不是一个具体的答案。

tools:attr markers

任何特定元素上都可以有许多与属性相关的标记,以解决冲突的所有属性。

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

属性的默认隐式模式,尝试使用不同的值合并低优先级属性声明时生成错误。

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

合并时从任何优先级较低的声明中删除 x、y、z 属性。

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

将任何优先级较低的声明中的 x、y、z 属性替换为提供的值(必须出现在同一节点上)。

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