Android 资源链接失败Gradle 构建错误

问题描述

下面描述的问题似乎只是因为我写错了

android:id="@android:id/android:list"

在布局中。对 android:list 的引用在 Eclipse 和 Android Studio 中通过应用程序的许多更新没有引起任何问题,但是在最新的 Gradle 更新之后它导致 Gradle 构建失败。只需将代码修改

android:id="@android:id/list"

解决了问题。


接受 Gradle 更新后,Gradle 构建失败报告

“Android 资源链接失败”

错误

C:\Users\...\AndroidStudioProjects\com. ...\app\src\main\res\layout\textsummarylist.xml:7: AAPT: error: resource android:id/android:list not found.

Project build.gradle 文件读取

  // Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.0.1'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

Module build.gradle 文件读取

apply plugin: 'com.android.application'

android {
    compileSdkVersion 15
    buildToolsversion "29.0.2"

    defaultConfig {
        applicationId "com.prepbgg.mymap"
        minSdkVersion 15
        targetSdkVersion 17
    }

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

dependencies {
    compile files('libs/jcoord-1.0.jar')
    implementation files('libs/jcoord-1.0.jar')
}

textsummarylist.xml 文件内容如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
    <ListView
    android:id="@android:id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"></ListView>
</LinearLayout>

请问我该如何解决


背景: 我正在修改一个应用程序,该应用程序多年来一直在多种设备上令人满意地使用,并且偶尔会有所改进。它供我个人使用,我需要它在运行 Android 4.1.2 版的旧手机和我当前运行 Android 10 版的手机上运行。

一切都很好,直到昨天我按照提示更新 Gradle。

当我遇到 Gradle 错误时,我会向 Stackoverflow 寻求有关如何纠正它们的建议。

更新后 Gradle 构建首先报告警告

配置 'compile' 已过时,已替换为 'implementation' 和 'api'。

我还没有尝试进行任何更改。我不知道需要更改什么(如果有的话)。

下一条消息是

指定的 Android SDK 构建工具版本 (27.0.3) 被忽略,因为它低于 Android Gradle 插件 4.0.1 支持的最低版本 (29.0.2)。

因此,我在 Module build.gradle 文件中编辑了对 SDK Build Tools 版本的引用(见下文)。

一个问题是

找不到 com.android.tools.build:aapt2:4.0.1-6197926。在以下位置搜索: - ...

根据 StackOverflow 上的建议,我在 Project build.gradle 文件中插入了对 google() 的引用。

“资源链接失败”错误出现在下一次构建尝试中。

解决方法

编辑 android:id="@android:id/android:list" 读取 android:id="@android:id/list" 后,应用已经构建并安装成功。

似乎对“@android:id/list”的引用已经通过了之前在 Eclipse 和 Android Studio 中的所有检查,但现在 Gradle 提出了反对意见!