AAPT错误:未找到资源drawable/...

65

最近升级了我的 Android Studio,现在无法再构建我的项目了。

每次执行构建操作时,都会出现以下错误:

error: resource drawable/splash_screen (aka com.whereisthemonkey.nowalism:drawable/splash_screen) not found.
Message{kind=ERROR, text=error: resource drawable/splash_screen (aka com.whereisthemonkey.nowalism:drawable/splash_screen) not found., sources=[C:\Users\Lucas\.gradle\caches\transforms-1\files-1.1\appcompat-v7-27.1.1.aar\cf575568f869a44c685b16e47de83a28\res\values\values.xml:1632:5-84], original message=, tool name=Optional.of(AAPT)}

尽管在drawable文件夹下存在splash_screen.xml文件,但此错误仍然存在。

重建、清理项目和无效缓存均不起作用!

添加android.enableAapt2=false这一行并不能解决真正的问题,因此我宁愿找到问题的根源。

以下是我的gradle.build文件:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'//https://github.com/bumptech/glide/issues/1939

android {
    compileSdkVersion 27
    buildToolsVersion "27.0.3"
    defaultConfig {
        applicationId "com.whereisthemonkey.nowalism"
        minSdkVersion 19
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
    }
    // Keep the following configuration in order to target Java 8.
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
        main {
            res.srcDirs += [
                    'src/main/res-backgrounds',
                    'src/main/res-jobs',
            ]
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:cardview-v7:27.1.1'
    implementation 'com.android.support:recyclerview-v7:27.1.1'
    implementation 'com.android.support:design:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'

    //Do not update due to dex error
    implementation 'org.apache.commons:commons-lang3:3.6'

    //Do not update due to dex error
    implementation 'com.google.firebase:firebase-auth:11.8.0'
    implementation 'com.google.firebase:firebase-messaging:11.8.0'
    implementation 'com.google.android.gms:play-services:11.8.0'

    implementation 'com.aurelhubert:ahbottomnavigation:2.1.0'
    implementation 'com.afollestad.material-dialogs:core:0.9.6.0'
    implementation 'com.afollestad.material-dialogs:commons:0.9.6.0'

    implementation 'com.amitshekhar.android:android-networking:1.0.1'
    implementation 'org.apache.directory.studio:org.apache.commons.io:2.4'

    implementation 'com.github.ome450901:SimpleRatingBar:1.4.1'

    implementation 'com.sothree.slidinguppanel:library:3.4.0'

    implementation 'com.github.esafirm.android-image-picker:imagepicker:1.12.0'
    //Do not update due to dex error
    implementation 'com.theartofdev.edmodo:android-image-cropper:2.5.1'

    implementation 'com.github.bumptech.glide:glide:4.6.1'
    kapt 'com.github.bumptech.glide:compiler:4.6.1'//https://github.com/bumptech/glide/issues/1939

    implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'

    implementation 'de.hdodenhof:circleimageview:2.2.0'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'

    implementation 'com.firebaseui:firebase-ui-auth:3.2.1'
    implementation 'com.android.support:support-v4:27.1.1'
}


apply plugin: 'com.google.gms.google-services'
任何帮助将不胜感激。

你确定它在drawable文件夹里还是在某个drawable-xyz文件夹里?请分享目录结构的截图。 - Shubham AgaRwal
它肯定在位于src/main/res/drawable/splash_screen.xml下的主drawable文件夹中。不幸的是,任何位置的更改都没有帮助。 - Lucas Romier
32个回答

140

在我的情况下,它是一个XML选择器。在第一行中,我有两个这样的选择器(由于文件之间的复制粘贴):

<?xml version="1.0" encoding="utf-8"?>

而且Android Studio没有发出任何警告或其他提示。移除它可以解决问题,但之后需要进行完整的清除构建。


14
请注意,可能引发错误的文件可能不在您所看到的那个文件中!请检查最近在项目中添加或修改的任何 XML 文件。 - Matt
所以,官方调试输出不可靠且不自我解释。 - Muhammad Asyraf
项目无法构建。我这样做了。编译成功。然后再次删除该行,重新构建项目,现在它可以工作了。无论如何... - Torge Rosendahl
我使用了一个SVG验证器并解决了我的问题。 - profimedica
我之所以出现这个错误,是因为缺少了<?xml行。 - ehartwell
我遇到这个错误是因为缺少了<?xml这一行。 - undefined

39

我修复了错误,这个错误可能来自于一个格式错误的复制XML文件(可能是一些行结束混淆)。

重写这些内容修复了错误,尽管Android Studio显示另一个文件是问题的源头,但实际上它与错误本身无关。


2
我曾经遇到过类似的错误,是由于XML格式不正确导致的。不幸的是,Android Studio没有显示任何警告。 - NewestStackOverflowUser
6
我有很多个XML文件...我该如何找到格式不正确的文件? - Zainab Rostami
谢谢。我也遇到了同样的问题。 - Naimul Kabir

9
在我的情况下,我删除了重复的命名空间声明,例如xmlns:android="http://schemas.android.com/apk/res/android",并使用"分析"-> "检查代码"工具来显示日志中的错误,然后我解决了可绘制资源中其他文件的错误。

8

我曾经遇到过同样的问题,最终通过以下方法解决:

分析>检查代码

我的应用在一个XML文件中缺少结束标签,只需尝试分析>检查代码即可重定向到确切的错误。


4
在我的情况下,我喜欢这样做: <?xml version="1.0" encoding="utf-8"?> 所以我只是去掉了其中一个。

3
在我的情况下,这是一个重复的问题:
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>

在可绘制资源文件中


1
100%正确,你救了我的一天,谢谢。 - Ashwin H

3

检查你的每一个XML文件,也许你会发现以下情况:

  • 从其他项目复制代码或文件时,可能忘记添加一些资源,例如drawable、strings等。

  • 检查每个drawable文件,Android Studio可能会在打开该文件时进行标记。

有时我们必须找到这种类型的错误,因为Android Studio停留在错误状态。


3

我曾经遇到过同样的问题,当我将图标添加到drawable文件中并将其设置为24dp时,问题得以解决。

复制您想要的任何图标并粘贴到drawable文件中,它将默认创建24dp大小的图标。


3
如果您到这里仍然没有得到答案,那么您可能需要使用 mipmap。它用于查找与目标设备dpi相关的资产(例如:mdpi、hdpi、xhdpi等)。
所以,您只需要从 mipmap 而不是 drawable 中寻址资产即可。 android:resource="@mipmap/splash_screen" 打开您的 AndroidManifest.xml 文件,并在使用了 drawable 的任何地方将其更改为 mipmap。以下是一些示例:

enter image description here


1

如果您已经准备好了自己的图标以供商店发布,请确保在xml文件中不包括图标的扩展名。否则它将始终失败。在我的情况下是这样的..

 <bitmap android:gravity="center" android:src="@drawable/myAppSplashLogo.png"/>

正确的做法是删除 .png 扩展名。

 <bitmap android:gravity="center" android:src="@drawable/myAppSplashLogo"/>

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