【问题标题】:Migrating from Eclipse to AndroidStudio: Attribute has already been defined从 Eclipse 迁移到 AndroidStudio:已定义属性
【发布时间】:2017-01-19 07:29:18
【问题描述】:

我目前正在尝试将我的 android 项目从 Eclipse 迁移到 Android Studio。构建项目失败并出现以下错误:

:app:processDebugResources

.../app/build/intermediates/res/merged/debug/values/values.xml

错误:(204) 属性“divider”已被定义

错误:(233) 属性“颜色”已被定义

这是 valuex.xml 的第 204 行

    <declare-styleable name="MyView"><attr format="dimension" name="off"/><attr format="integer" name="host_mode"/><attr format="dimension" name="h_textSize"/><attr format="dimension" name="header_textSize"/><attr format="dimension" name="h_rx"/><attr format="dimension" name="h_ry"/><attr format="dimension" name="divider_height"/><attr format="color" name="divider"/><attr format="dimension" name="chat_divider_height"/><attr format="color" name="chat_divider"/><attr format="color" name="background_color"/><attr format="boolean" name="use_name"/></declare-styleable>

模块 gradle 文件:

dependencies {
compile 'com.android.support:support-v4:24.2.0'
compile 'com.google.code.gson:gson:2.3.1'
compile 'com.google.android.gms:play-services:+'
compile files('libs/alljoyn.jar')
compile files('libs/peergroupmanager.jar')
}

有谁知道问题出在哪里?

更新: 这是模块的完整 gradle 文件

apply plugin: 'com.android.application'

android {
    compileSdkVersion 'Google Inc.:Google APIs:23'
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId ""
        minSdkVersion 16
        targetSdkVersion 24
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_7
            targetCompatibility JavaVersion.VERSION_1_7
        }
    }

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

dependencies {
    compile 'com.android.support:support-v4:24.2.0'
    compile 'com.google.code.gson:gson:2.3.1'
    compile 'com.google.android.gms:play-services:+'
    compile files('libs/alljoyn.jar')
    compile files('libs/peergroupmanager.jar')
}

更新 2: 我根据 xhamr 更改了我的 gradle 文件。我现在收到一条不同的错误消息:

:app:processDebugResources

../app/build/intermediates/res/merged/debug/values/values.xml

错误:(204) 属性“divider”已经定义为不兼容

格式。错误:(182) 此处定义的原始属性。

第 182 行:

    <declare-styleable name="ActionBar"><attr name="navigationMode"><enum name="normal" value="0"/><enum name="listMode" value="1"/><enum name="tabMode" value="2"/></attr><attr name="displayOptions"><flag name="none" value="0"/><flag name="useLogo" value="0x1"/><flag name="showHome" value="0x2"/><flag name="homeAsUp" value="0x4"/><flag name="showTitle" value="0x8"/><flag name="showCustom" value="0x10"/><flag name="disableHome" value="0x20"/></attr><attr name="title"/><attr format="string" name="subtitle"/><attr format="reference" name="titleTextStyle"/><attr format="reference" name="subtitleTextStyle"/><attr format="reference" name="icon"/><attr format="reference" name="logo"/><attr format="reference" name="divider"/><attr format="reference" name="background"/><attr format="reference|color" name="backgroundStacked"/><attr format="reference|color" name="backgroundSplit"/><attr format="reference" name="customNavigationLayout"/><attr name="height"/><attr format="reference" name="homeLayout"/><attr format="reference" name="progressBarStyle"/><attr format="reference" name="indeterminateProgressStyle"/><attr format="dimension" name="progressBarPadding"/><attr name="homeAsUpIndicator"/><attr format="dimension" name="itemPadding"/><attr format="boolean" name="hideOnContentScroll"/><attr format="dimension" name="contentInsetStart"/><attr format="dimension" name="contentInsetEnd"/><attr format="dimension" name="contentInsetLeft"/><attr format="dimension" name="contentInsetRight"/><attr format="dimension" name="elevation"/><attr format="reference" name="popupTheme"/></declare-styleable>

【问题讨论】:

  • 你能分享完整的 build.gradle 吗?它可能有助于理解这个问题
  • 我更新了我的问题

标签: android eclipse android-studio gradle eclipse-adt


【解决方案1】:

您的目标是 SDK 24 并使用紧凑型库 v24,尝试将 compileSdkVersion 24 设置为

compileSdkVersion 24
buildToolsVersion '24.0.2'
useLibrary 'org.apache.http.legacy'

defaultConfig {
    ...
    targetSdkVersion 24
    ...
}

确保您拥有来自 SDK 管理器的最新构建工具

编辑

更新后问题不在您的依赖项上,而是在您的declare-styleable 上。 android定义一个名为divider的属性与你的冲突,只需更改你的divider属性的名称:

<attr format="color" name="divider_color"/>

【讨论】:

  • 我现在收到一条不同的错误消息。请看我的更新
  • 但是我在eclipse中没有这个问题。那么我的代码怎么会有问题呢?
  • 新问题是由于依赖和构建工具的变化,android定义了相同的attr,格式与你的不同。
【解决方案2】:
  1. 更新所有工具和 SDK 支持库。为此,请转到您的 Android Studio 并打开 SDK 管理器并安装更新。
  2. 然后重启你的 Android Studio
  3. 检查您的 buil.gradle 并根据最新版本更新以下代码行

    buildToolsVersion "23.0.3"

    targetSdkVersion 24

    编译'com.android.support:support-v4:24.2.0'

  4. 同步项目

  5. 重建

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-14
    • 1970-01-01
    • 2015-04-27
    • 1970-01-01
    • 2013-11-23
    • 1970-01-01
    相关资源
    最近更新 更多