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个回答

1
在我的情况下,一个可绘制的资源文件需要更高的API版本,因为我的build.gradle中设置了minSdkVersion。主要问题是该文件在打开之前没有显示任何错误,因此很难找到它。我花费了至少4个小时尝试其他方法,因为在构建项目时显示了其他可绘制的资源文件的错误,但它们根本没有问题。删除需要更高API的可绘制部分后,应用程序正常构建。

1
在我的情况下,问题出在格式不正确的splash_screen.xml文件上,这个文件是“还原提交”中删除的一部分,但不知何故它可能仍然处于活动变更列表中。修复了该文件并解决了问题。

1
在我的情况下:一个我没有创建的文件出现在drawable文件夹中,这个文件是空的,我只是将其删除后程序就可以正常运行了。

1
在我的情况下,问题是代码冲突,其他人与我同时更改了同一文件。资源代码已经被更改。当我修复该文件时,问题就解决了。

1
在我的情况下,我创建了一些全部为调试分类的可绘制文件,现在需要回去创建一个相应的发布分类。完成后,第一次尝试就可以正常工作。

优秀的答案,简单易行。只需将调试文件夹内容复制到发布文件夹中即可。 - Hmerman6006

1

1 - 进入应用程序文件夹并删除构建文件夹
2 - 点击文件 -> 无效缓存 - 无效并重启


1

在我的情况下,在drawable文件夹下,我更改了与AndroidManifest文件中声明相同的图像。


0

我曾经遇到过同样的问题。通过手动添加解决了它。

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

即使与React Native链接后仍然如此。还添加了

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

0

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


0
在我的情况下,我错误地引用了 Android 资源文件,即 android:color/splash_bg,而不是 @color/splash_bg
其中,splash_bg 表示我在 res/values/colors.xml 文件中声明的自定义颜色资源。

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