找不到资源可绘制但它们存在于可绘制文件夹中

Mr.*_*ha1 11 xml android aapt

我正在开发一个 android 应用程序大约 2 个月。几乎每次我在尝试构建项目时创建自定义可绘制对象时,都会出现 Android 资源链接失败错误,然后是 android studio 无法定位的可绘制对象的名称。

我的解决方案是,我将这些可绘制对象移出我的项目的某个位置,并在出现错误后重建应用程序我再次将可绘制对象移回我的项目,当我重建我的项目时,APK不再有错误,我可以构建、运行、甚至生成APK和那些drawables 工作得很好。但是现在我的项目完成了,我想生成签名的 apk,但是我无法摆脱 Android 资源链接失败的错误。我在下面放了一些我的可绘制代码。

retry_btn.xml:

<?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:drawable="@drawable/register_btn_disabled"/>
    <item android:state_pressed="true" android:drawable="@drawable/register_btn_clicked"/>
    <item android:state_pressed="false" android:drawable="@drawable/register_btn_normal"/>
    <item android:drawable="@drawable/register_btn_normal"/>
</selector>
Run Code Online (Sandbox Code Playgroud)

story_box.xml:

<?xml version="1.0" encoding="utf-8"?>
    <shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke android:color="#70707070" android:width="1dp"/>
</shape>
Run Code Online (Sandbox Code Playgroud)

table_border.xml:

<?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
        <item>
            <shape
        android:shape="rectangle">
                <stroke android:width="1dp" android:color="#333333" />
                <solid android:color="#EEEEEE" />
        </shape>
    </item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

我的项目中可以使用所有可绘制对象,但是当我尝试生成签名的 apk 时,我得到了这个输出:

Android 资源链接失败

输出:C:\Users\MyUser\AndroidStudioProjects\MyAppName\app\src\main\res\layout\activity_main.xml:51: 错误:资源可绘制/搜索(又名 uk.MyAppName.www.MyAppName:drawable/search)不是成立。
C:\Users\MyUser\AndroidStudioProjects\MyAppName\app\src\main\res\layout\activity_movie.xml:234: 错误:找不到资源 drawable/story_box(又名 uk.MyAppName.www.MyAppName:drawable/story_box)。
C:\Users\MyUser\AndroidStudioProjects\MyAppName\app\src\main\res\layout\activity_no_internet.xml:49: 错误:找不到资源 drawable/retry_btn(又名 uk.MyAppName.www.MyAppName:drawable/retry_btn)。
C:\Users\MyUser\AndroidStudioProjects\MyAppName\app\src\main\res\layout\activity_register.xml:205: 错误:找不到资源 drawable/register_btn(又名 uk.MyAppName.www.MyAppName:drawable/register_btn)。
C:\Users\MyUser\AndroidStudioProjects\MyAppName\app\src\main\res\layout\activity_register.xml:215: 错误:找不到资源 drawable/register_btn(又名 uk.MyAppName.www.MyAppName:drawable/register_btn)。
C:\Users\MyUser\AndroidStudioProjects\MyAppName\app\src\main\res\layout\activity_search.xml:39: 错误:未找到资源可绘制/搜索(又名 uk.MyAppName.www.MyAppName:drawable/search)。
C:\Users\MyUser\AndroidStudioProjects\MyAppName\app\src\main\res\layout\search_table_item.xml:2: 错误:资源 drawable/table_border(又名 uk.MyAppName.www.MyAppName:drawable/table_border)未找到。
C:\Users\MyUser\AndroidStudioProjects\MyAppName\app\src\main\res\layout\table_item.xml:2: 错误:找不到资源 drawable/table_border(又名 uk.MyAppName.www.MyAppName:drawable/table_border)。
错误:链接文件资源失败。

命令:C:\Users\MyUser.gradle\caches\transforms-1\files-1.1\aapt2-3.2.1-4818971-windows.jar\ac5e520165d725772f5386c054776ce5\aapt2-3.297\aapt2-3.2.1-12.windows8 I\
C:\Users\MyUser\AppData\Local\Android\Sdk\platforms\android-28\android.jar\
--manifest\
C:\Users\MyUser\AndroidStudioProjects\MyAppName\app\build\intermediates\merged_manifests\ release\processReleaseManifest\merged\AndroidManifest.xml\
-o\
C:\Users\MyUser\AndroidStudioProjects\MyAppName\app\build\intermediates\processed_res\release\processReleaseResources\out\resources-release.ap_\
-R\
@C:\Users\MyUser\AndroidStudioProjects\MyAppName\app\build\intermediates\incremental\processReleaseResources\resources-list-for-resources-release.ap_.txt\
--auto-add-overlay\
--java\
C: \Users\MyUser\AndroidStudioProjects\MyAppName\app\build\generated\not_namespaced_r_class_sources\release\processReleaseResources\r\
--proguard\
C:\Users\MyUser\AndroidStudioProjects\MyAppName\app\build\intermediates\proguard-rules\release\ aapt_rules.txt\
--custom-package\
uk.MyAppName.www.MyAppName\
-0\
apk\
--output-text-symbols\
C:\Users\MyUser\AndroidStudioProjects\MyAppName\app\build\intermediates\symbols\release\R.txt\
--no-version-vectors
Daemon: AAPT2 aapt2-3.2.1-4818971-windows Daemon #0

这是我的 gradle 文件:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "uk.MyAppName.www.MyAppName"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
//            minifyEnabled false
//            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    productFlavors {
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:support-v4:28.0.0'
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:exifinterface:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:design:28.0.0'
    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'
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    implementation 'com.android.support:support-v4:28.0.0'
    implementation 'com.android.support:cardview-v7:28.0.0'
    implementation 'org.jetbrains.anko:anko-commons:0.10.4'
    implementation 'com.squareup.picasso:picasso:2.71828'
    implementation 'com.google.android.exoplayer:exoplayer:2.9.1'
}
Run Code Online (Sandbox Code Playgroud)

Mr.*_*ha1 9

对于所有遇到此问题的人,只需将您的 drawable 文件夹更改为 drawable-v(您的 sdk 版本),希望这个答案能解决您的问题。或者添加另一个具有提到的名称的可绘制目录,然后简单地将您的可绘制文件复制到那里。


Ous*_*ush 5

在某些情况下,它与重命名可绘制对象无关。

重新检查您处理的最后一个 drawable/xml 文件,这可能是罪魁祸首。

我错误地<?xml version="1.0" encoding="utf-8"?>在顶部有 2 个根标签

  • 我也发生了完全相同的事情。 (2认同)