AAPT error: resource drawable/... not found

AAPT error: resource drawable/... not found

最近升级了我的 android 工作室后,我无法再构建我的项目了。

每次执行构建时,我都会遇到以下错误:

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)}

此错误仍然存​​在,即使文件 splash_screen.xml 存在于 drawable 文件夹下。

重建、清理项目和使缓存无效!

添加行 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'

如有任何帮助,我们将不胜感激。

我修复了错误,该错误可能来自格式错误的复制 XML 文件(可能是一些行尾混淆)

重写这些修复了错误,尽管 Android Studio 将另一个文件显示为问题的来源,实际上与错误本身无关。

在我的例子中,它是格式错误的 splash_screen.xml,它是 "reverted commit" 的一部分,实际上删除了这个文件,但不知何故它仍然可能在活动的更改列表中。

修复了该文件并解决了问题。

在我的例子中,它是一个需要更高 API 版本的可绘制资源文件,因为它是我在 build.gradle 中设置的 minSdkVersion。主要问题是文件在你打开之前没有显示任何错误,所以很难找到它。我花了至少 4 个小时尝试其他事情,因为在构建项目时出现错误,它显示其他可绘制资源文件根本没有任何问题。删除需要更高 API 的可绘制对象后,应用程序正常构建。

在我的例子中,它是一个 xml 选择器。在第一行中,我有两个(感谢在我的文件中复制粘贴):

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

和 Android Studio 没有给出任何警告或其他信息。删除它解决了问题,但之后需要一个完整的干净构建。

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

  • 在处理其他项目的代码或文件时,您可能忘记添加一些资源,例如drawable、strings。

  • 检查每个可绘制文件,可能 Android Studio 会在您打开该文件时突出显示。

有时我们必须找到这种类型的错误,因为 Android Studio 卡住了错误。

以我为例 我删除了重复的名称空间声明,例如 xmlns:android="http://schemas.android.com/apk/res/android"
我使用 Analyze -> inspect code 工具在日志中显示错误,然后我解决了可绘制资源中其他文件 xml 中的错误。

我遇到了同样的问题,当我将可绘制文件中的图标添加到 24dp 时我解决了这个问题。

复制您想要的任何图标并将其粘贴到可绘制文件中,它将创建(默认)24dp 图标。

我在构建我的应用程序的发布版本时遇到了同样的问题,问题是我的项目的一些重要 res 文件位于 debug 目录中,而不是 debug 目录中=12=] 目录所以我通过将 debug 中的文件移动到 release 中来解决问题,您可以通过在 project 而不是 android 下查看项目树来检查它

在复制和粘贴代码的同时,我也将代码粘贴到行下方

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

因为,我的 xml 文件已经包含以上行,并且在添加此行时它出现了两次。

所以我删除了其中一行并解决了我的错误

就我而言,我使用了对 android 资源文件的错误引用 即 android:color/splash_bg 而不是 @color/splash_bg

其中 splash_bg 代表我在 res/values/colors.xml 文件中声明的自定义颜色资源。

在我的例子中,我不得不从 splash_screen.xml 中删除 android:src="@drawable/background"android:src="@drawable/logo" 行,尽管错误最初指向 AndroidManifest.xml。

就像第一个 post 所说的那样,确保 <?xml version="1.0" encoding="utf-8"?> 在所有 xml 文件中都是正确的。

我的是代码冲突问题,其他人和我改了同一个文件。资源代码已更改。当我修复文件后,问题就消失了。

在我的例子中,我删除了文件夹“drawable”

就我而言,我喜欢这样: <?xml version="1.0" encoding="utf-8"?><?xml version="1.0" encoding="utf-8"?>

所以我只删除其中一个

在我的例子中:在可绘制文件中有一个我没有创建的文件,这个文件是空的,我只是删除它并启动了程序

我遇到了同样的问题,最后通过

解决了这个问题

Analyze>Inspect Code

我的应用程序在其中一个 XML 文件中缺少结束标记只需尝试 分析 > 检查代码 它会将您重定向到确切的错误。

我在 xml(矢量)文件之一中重新格式化了代码(因为我没有发现错误),@Lucas Romier 也提到了错误(我认为),但是文件名称带有红色下划线,它建议然后清理构建,然后它就可以工作了。 :)

如果您来到这里但没有得到答案,那么您可能需要使用 mipmap。它用于查找与目标设备 dpi 相关的资产(例如:mdpi、hdpi、xhdpi、...)。

因此,您需要做的就是从 mipmap 而非 drawable 中处理资产。

android:resource="@mipmap/splash_screen"

打开你的AndroidManifest.xml,把你用过drawable的地方改成mipmap。这是一些例子:

在我的例子中,它是重复的:

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

在可绘制资源文件中

从另一个项目复制“res”文件夹(res 文件夹在哪里 - android/app/src/main)粘贴您自己的项目。重新 运行.

复制 drawable 文件夹中的所有文件并粘贴 drawable_v24 文件夹您自己的项目。重新 运行。我希望这是工作。

对我来说,问题是我拼错了 drawable-mdpidrawable-hdpi 等名称(我将它们写成 dawable-mdpi 等)

就我而言 File > Invalidate CachesBuild > Rebuild Project,删除 .gradle 文件夹没有帮助。所以我删除了一个包含冲突文件的文件夹 res\mipmap-anydpi-v26

我遇到了同样的问题。手动添加解决了

dependencies {
    ...
    implementation project(':react-native-splash-screen')
}

甚至在与 react-native 链接之后。还添加了

protected void onCreate(Bundle savedInstanceState) {
    SplashScreen.show(this);  // Added this
    super.onCreate(savedInstanceState);
}

如果您已将自己的图标放在一起准备在商店发布,请确保不要在 xml 文件中包含图标的扩展名。它总是会失败。在我的情况下是这样的..

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

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

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