【问题标题】:Android resource compilation failedAndroid资源编译失败
【发布时间】:2018-08-06 14:15:57
【问题描述】:

我正在开发一个需要设计支持库的 android 应用,所以当我将 implementation 'com.android.support:design:27.1.1' 添加到我的 build.gradle(app) 中时,会出现以下错误:

原因:org.gradle.internal.UncheckedException: java.util.concurrent.ExecutionException: com.android.builder.internal.aapt.v2.Aapt2Exception: Android资源编译失败 MyAppDirectory\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values\values.xml:2489:错误:资源 'attr/itemBackground' 与配置 '' 的重复值。

但是当我删除依赖implementation 'com.android.support:design:27.1.1' 然后错误消失,gradle build 成功。

我的项目 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
}

我的应用 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'
}

注意:我已经清理并重建了项目,并且我还无效并清除了缓存

【问题讨论】:

  • 你有 itemBackground 属性吗?
  • 尝试升级gradle和SDK。
  • @matio 我已经为工具栏和按钮添加了一些背景属性,但没有像 itemBackground 属性一样
  • @grrigore 我已经更新了所有内容,但是当我添加设计库时仍然显示相同的错误
  • 你试过无效缓存并重启吗?

标签: java android gradle aapt


【解决方案1】:

通过研究,我发现MenuView(工具栏中的菜单)已经定义了这个属性。

<declare-styleable name="MenuView">
    ...
    <!-- Default background for each menu item. -->
    <attr name="itemBackground" format="color|reference" />
    ...
</declare-styleable>

我使用了&lt;attr name="android:itemBackground" /&gt; 而不是 &lt;attr name="itemBackground" format="reference|color" /&gt; 来解决这个问题。

请升级到 1.4.1 版。

【讨论】:

    【解决方案2】:

    检查 arrays.xml 或 strings.xml 中任何字符串值的撇号 (') 并确保在其前添加反斜杠 (\)

    示例 1: &lt;string-array&gt; &lt;item&gt;example's&lt;/item&gt; &lt;/string-array&gt;

    &lt;string-array&gt; &lt;item&gt;example\'s&lt;/item&gt; &lt;/string-array&gt;

    示例 2: &lt;string&gt;example's&lt;/string&gt;

    &lt;string&gt;example\'s&lt;/string&gt;

    【讨论】:

    • 我错过了这一点。你的回答有帮助。太好了!
    【解决方案3】:

    您使用的库之一似乎与支持库具有相同的名称属性。我建议你从 espresso 开始排除它们,然后看看你是否可以修复它,并不断将它设置为其他库,直到你找到哪个库使用了它。

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

    【讨论】:

    • 谢谢,这是由于 pinview 库,所以如果我想同时使用 pinview 库和设计库怎么办。
    • @mdadil2019 也为您的 pinview 实现执行此操作,如果不起作用,那么您可以随时在他们的 github 页面上发布问题,我尝试在他们的 github 页面上查看问题,但没有这样的..
    • @AalapPatel 好的,我已经报告了这个问题
    • 好的,但是如果您已将此标记为答案,那么还有什么问题吗???
    【解决方案4】:

    在我的情况下出现错误是因为对于生成错误的视图, 有过多的 xml 命名空间定义 ,即: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">
    

    Android Studio 构建时出现错误:

    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.
    

    这意味着我们需要删除xmlns,因为在编译的xml中它出现在三个地方!

    所以,删除xmlns:android="http://schemas.android.com/apk/res/android" 解决了这个问题。

    【讨论】:

      猜你喜欢
      • 2019-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多