简体   繁体   中英

Gradle build fails with error: Failed linking resources and AAPT2 error

Every time I try to run the project or try to rebuild it,it throws error:failed linking resources. Please see screenshot: 错误信息

All the drawable are there still it is showing this. And all the R of my java files are in red color when hover it shows a message cannot resolve symbol 'R' See screenshot: Java文件错误

Error is coming in every java file I open.

I tried Invalidating caches and cleaning project but nothing is working. Search everywhere but didn't find a solution.

Thanks in advance!

EDIT: build.gradle(Module: app)

    // Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.2'


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

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

task clean(type: Delete) {
    delete rootProject.buildDir
}

build.gradle(Project:weMate)

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.shivamtiwari.wemate"
        minSdkVersion 17
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support:design:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:support-v4:26.1.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'
}

After trying gradlew assembleDebug:

        Microsoft Windows [Version 10.0.16299.371]
(c) 2017 Microsoft Corporation. All rights reserved.

E:\MCA\AndroidProject\weMate>gradlew assembleDebug
E:\MCA\AndroidProject\weMate\app\src\main\res\layout\fragment_account.xml:27: AAPT: error: resource drawable/uploaddisplay (aka com.shivamtiwari.wemate:drawable/uploaddisplay) not fo
und.

E:\MCA\AndroidProject\weMate\app\src\main\res\layout\fragment_car_pool.xml:9: AAPT: error: resource drawable/search_bar_bg (aka com.shivamtiwari.wemate:drawable/search_bar_bg) not fo
und.

E:\MCA\AndroidProject\weMate\app\src\main\res\layout\fragment_car_pool.xml:21: AAPT: error: resource drawable/search_bar_bg (aka com.shivamtiwari.wemate:drawable/search_bar_bg) not f
ound.

E:\MCA\AndroidProject\weMate\app\src\main\res\layout\fragment_car_pool.xml:33: AAPT: error: resource drawable/search_bar_bg (aka com.shivamtiwari.wemate:drawable/search_bar_bg) not f
ound.

E:\MCA\AndroidProject\weMate\app\src\main\res\layout\fragment_car_pool.xml:51: AAPT: error: resource drawable/search_bar_bg (aka com.shivamtiwari.wemate:drawable/search_bar_bg) not f
ound.

E:\MCA\AndroidProject\weMate\app\src\main\res\layout\fragment_car_pool.xml:64: AAPT: error: resource drawable/search_bar_bg (aka com.shivamtiwari.wemate:drawable/search_bar_bg) not f
ound.

E:\MCA\AndroidProject\weMate\app\src\main\res\layout\fragment_car_pool_list.xml:8: AAPT: error: resource drawable/list_row_selector (aka com.shivamtiwari.wemate:drawable/list_row_sel
ector) not found.

E:\MCA\AndroidProject\weMate\app\src\main\res\layout\fragment_helpdesk.xml:8: AAPT: error: resource drawable/search_bar_bg (aka com.shivamtiwari.wemate:drawable/search_bar_bg) not fo
und.

E:\MCA\AndroidProject\weMate\app\src\main\res\layout\fragment_shop_list.xml:8: AAPT: error: resource drawable/list_row_selector (aka com.shivamtiwari.wemate:drawable/list_row_selecto
r) not found.

E:\MCA\AndroidProject\weMate\app\src\main\res\layout\nav_header_main.xml:2: AAPT: error: resource drawable/side_nav_bar (aka com.shivamtiwari.wemate:drawable/side_nav_bar) not found.

error: failed linking file resources.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:processDebugResources'.
> Failed to process resources, see aapt output above for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 4s
13 actionable tasks: 1 executed, 12 up-to-date

In my case, After moving the resource files from Global drawable folder to specific drawable folder solved the issue.

For ex: If your resource file is in the following path

/drawable/btn_bg.xml

Then moving it to

/drawable-hdpi/btn_bg.xml 

will solve the problem.

Similarly any image files as well,

For ex: If your resource file is in the following path

/drawable/welcome_illustration.png

Then move it to

/drawable-hdpi/welcome_illustration.png

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM