错误:属性“theme”已经定义。

22

我正在使用Android Studio构建应用程序,使用以下依赖项:

  1. Play服务 : compile 'com.google.android.gms:play-services:5.2.08'
  2. AppCompat v7 : compile 'com.android.support:appcompat-v7:21.0.0'
  3. 支持CardView : compile 'com.android.support:cardview-v7:21.0.0'
  4. 支持RecyclerView : compile 'com.android.support:recyclerview-v7:21.0.0'

在构建我的应用程序时,我遇到以下错误:

app/build/intermediates/exploded-aar/com.google.android.gms/play-services/5.2.08/res/values/wallet_attrs.xml
    Error:Attribute "theme" has already been defined

代码样式.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </style>

</resources>

编写 wallet_attrs.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2014 Google Inc. All Rights Reserved. -->
<resources>
    <!-- Attributes for the WalletFragment &lt;fragment&gt; tag -->
    <declare-styleable name="WalletFragmentOptions">
        <!-- Theme to be used for the Wallet selector -->
        <attr name="theme" format="enum">
            <enum name="holo_dark" value="0"/>
            <enum name="holo_light" value="1"/>
        </attr>
        <!-- Google Wallet environment to use -->
        <attr name="environment" format="enum">
            <enum name="production" value="1"/>
            <enum name="sandbox" value="0"/>
            <enum name="strict_sandbox" value="2"/>
        </attr>
        <!-- A style resource specifing attributes to customize the look and feel of WalletFragment -->
        <attr name="fragmentStyle" format="reference"/>
        <!-- Fragment mode -->
        <attr name="fragmentMode" format="enum">
            <enum name="buyButton" value="1"/>
            <enum name="selectionDetails" value="2"/>
        </attr>
    </declare-styleable>

    <!-- Attributes that may be specified in a style resource to customize the look and feel of
         WalletFragment -->
    <declare-styleable name="WalletFragmentStyle">
        <!-- Height of the buy button. This includes an 8dp padding (4dp on each side) used for
             pressed and focused states of the button. The value can be a specific height, e.g.
             "48dp", or special values "match_parent" and "wrap_content". -->
        <attr name="buyButtonHeight" format="dimension">
            <enum name="match_parent" value="-1" />
            <enum name="wrap_content" value="-2" />
        </attr>
        <!-- Width of the buy button. This includes an 8dp padding (4dp on each side) used for
             pressed and focused states of the button. The value can be a specific width, e.g.
             "300dp", or special values "match_parent" and "wrap_content". -->
        <attr name="buyButtonWidth" format="dimension">
            <enum name="match_parent" value="-1" />
            <enum name="wrap_content" value="-2" />
        </attr>
        <!-- Text on the buy button. Must be one of "buy_with_google", "buy_now" and "book_now" -->
        <attr name="buyButtonText" format="enum">
            <enum name="buy_with_google" value="1"/>
            <enum name="buy_now" value="2"/>
            <enum name="book_now" value="3"/>
        </attr>
        <!-- Appearance of the buy button. Must be one of "classic", "grayscale" and "monochrome" -->
        <attr name="buyButtonAppearance" format="enum">
            <enum name="classic" value="1"/>
            <enum name="grayscale" value="2"/>
            <enum name="monochrome" value="3"/>
        </attr>
        <!-- TextAppearance for masked wallet details -->
        <attr name="maskedWalletDetailsTextAppearance" format="reference"/>
        <!-- TextAppearance for headers describing masked wallet details -->
        <attr name="maskedWalletDetailsHeaderTextAppearance" format="reference"/>
        <!-- Masked wallet details background -->
        <attr name="maskedWalletDetailsBackground" format="reference|color"/>
        <!-- TextAppearance for the "Change" button in masked wallet details view -->
        <attr name="maskedWalletDetailsButtonTextAppearance" format="reference"/>
        <!-- "Change" button background in masked wallet details view -->
        <attr name="maskedWalletDetailsButtonBackground" format="reference|color"/>
        <!-- Color of the Google Wallet logo text in masked wallet details view -->
        <attr name="maskedWalletDetailsLogoTextColor" format="color"/>
        <!-- Type of the wallet logo image in masked wallet details view -->
        <attr name="maskedWalletDetailsLogoImageType" format="enum">
            <enum name="classic" value="1"/>
            <enum name="monochrome" value="2"/>
        </attr>
    </declare-styleable>
</resources>

请发布您的 wallet_attrs.xml 文件和 values 文件夹内的 styles.xml 代码。 - Hirak Chhatbar
请提供原始英文文本进行翻译。 - Coder
5
尝试将您的Google Play服务依赖项升级到'com.google.android.gms:play-services:6.1.+'。 - Hirak Chhatbar
3
请确保您的SDK管理器中所有的构建工具和支持工具都是最新的,或者已经更新。因为您的XMS文件似乎没有问题,问题出在库文件上。所以请更新您SDK管理器中的所有内容,以及如上所述更新GMS服务依赖项。 - Hirak Chhatbar
5个回答

41

只需使用最新的 (21)
appCompat 和
google play services 库

例如:

compile 'com.android.support:support-v4:21.0.0'
compile 'com.android.support:appcompat-v7:21.0.0'
compile 'com.android.support:recyclerview-v7:21.0.0' 
compile 'com.android.support:cardview-v7:21.0.0'
compile 'com.google.android.gms:play-services:6.1.11'

然后一切都正常了 :)


我在其他库中也遇到了同样的问题,两个declare-styleable并没有重复,gradle-plugin/build工具版本分别是1.0.0/20.0.0和0.9+/19.0.0。 - Yuki Yoshida
完美。保持更新到最新可用版本。 - vinksharma

11

请检查一下是否有一个名为 attrs.xml 的文件。如果有,在该文件中将以下行替换为:

 <attr name="theme" format="reference" />

和;随着

 <attr name="apptheme" format="reference" />

也就是说,重新命名主题名称。 现在清理项目。 这解决了我的问题。


我将名称“theme”更名为“apptheme”。 - codeX
上帝保佑你!谢谢! - Anton Shkurenko
+1 这是该问题的确切解决方案。但我不知道当这些冲突属性存在于两个不可修改的第三方库中时该怎么办。 - C--

2

在通过SDK管理器更新内容后,我在Eclipse中开发时遇到了同样的问题。

我不得不完全从工作区中删除Google Play服务库,然后按照这里的说明复制和导入更新版本。

没有更新引用的JAR文件对我有帮助-只有一个完全新的导入。这包括更新android-support-v7-appcompat的导入。


0

如果您想继续支持旧版的Android(2.3.x,3.x.x,4.0),请使用修改后的库版本,删除钱包类和资源(前提是您不需要钱包功能)。这可以避免出现“主题”属性问题。我正在使用play-services-5.0.89.arr,没有任何问题。

特别是,删除com.google.android.gms.wallet.*类和wallet_*.xml资源即可。


-3

感谢Hirak提供的帮助,正如您所说,错误出在依赖项上。我使用以下版本的库解决了这个问题:

compile 'com.android.support:appcompat-v7:20.0.0'
compile 'com.google.android.gms:play-services:4.+'
compile 'com.android.support:cardview-v7:21.0.0'
compile 'com.android.support:recyclerview-v7:21.0.0'

2
那是一个非常老的Google Play服务版本。 - Jared Burrows
是的...但它开始工作了...如果我使用所有这些库的最新版本,就会出现错误。 - Coder
我没有给你的回答投反对票,但你应该使用这个:compile 'com.google.android.gms:play-services:6.1.11'。为什么要在这种情况下回答自己的问题呢? - Jared Burrows

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接