Error:com.android.tools.aapt2.Aapt2Exception: AAPT

2018-12-31 17:47发布

This is the output of my Gradle console, I am unable to build my project

D:\Android Projects\....\app\src\main\res\layout\topic_view_header.xml
Error:error: resource attr/?? (aka -packagename- :attr/??) not found.
Error:error: resource attr/?? (aka -packagename-:attr/??) not found.
Error:error: resource attr/?? (aka -packagename-:attr/??) not found.
Error:resource attr/?? (aka -packagename-:attr/??) not found.
Error:resource attr/?? (aka -packagename-:attr/??) not found.
Error:resource attr/?? (aka -packagename-:attr/??) not found.
Error:failed linking file resources.
Error:java.util.concurrent.ExecutionException: 
java.util.concurrent.ExecutionException: 
com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details
Error:java.util.concurrent.ExecutionException: 
com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details
Error:com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for 
details
Error:Execution failed for task ':app:processDebugResources'.
> Failed to execute aapt
Information:BUILD FAILED in 27s
Information:11 errors
Information:0 warnings

Android Studio 3.0 RC 2

30条回答
宁负流年不负卿
2楼-- · 2018-12-31 18:22

I just had this problem when trying to use data bind and declaring the layout tag. I know it is a bit late but for the sake of anyone encountering this problem, What I did to resolve the issue after so many attempts was that on your root layout when you are not using data bind say for example this

  <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">     </android.support.constraint.ConstraintLayout>

remove the

xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"

and just put it on your layout tag(that is if you are using data binding)

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

</layout>

and hopefully it will work. the android.enableAapt2=false didn't work for me so I have to remove everything and try to figure out why I get the error when I put layout tag and use data binding thus I came up with the solution. Hope it helps

查看更多
姐姐魅力值爆表
3楼-- · 2018-12-31 18:23

In my case the real problem was that after generating Image Asset to generate launcher mipmap icon for my project, the generated file ic_launcher_foreground.xml had error inside (was wrongly generated). There was missing closing xml tag in the end of file, < / vector> was missing.

查看更多
像晚风撩人
4楼-- · 2018-12-31 18:24

UPDATE

A new version of Gradle and Android-gradle-plugin is available that fixes these issues.

build.gradle (top level)

buildscript {
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
    }
}

gradle-wrapper.properties

distributionUrl=https\://services.gradle.org/distributions/gradle-4.8-all.zip

PREVIOUS ANSWER

If you disable AAPT2 you are just hiding the real issue.

Please be aware that AAPT1might be removed in the future therefore you are forced to use AAPT2. Actually the migration guide isn't hard to follow since you don't see that much changes at the same time this way is future proof.

Element hierarchies in the Android manifest

In previous versions of AAPT, elements nested in incorrect nodes in the Android manifest are either ignored or result in a warning. For example, consider the following sample:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.myname.myapplication">
   <application
       ...
       <activity android:name=".MainActivity">
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />
               <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
           <action android:name="android.intent.action.CUSTOM" />
       </activity>
   </application>
</manifest>

Therefore you must check first if your really follow the correct Manifest structure as showed below.

Manifest file structure

The code snippet below shows the general structure of the manifest file and every element that it can contain. Each element, along with all of its attributes, is fully documented in a separate file.

<manifest>

    <uses-permission />
    <permission />
    <permission-tree />
    <permission-group />
    <instrumentation />
    <uses-sdk />
    <uses-configuration />  
    <uses-feature />  
    <supports-screens />  
    <compatible-screens />  
    <supports-gl-texture />  

    <application>

        <activity>
            <intent-filter>
                <action />
                <category />
                <data />
            </intent-filter>
            <meta-data />
        </activity>

        <activity-alias>
            <intent-filter> . . . </intent-filter>
            <meta-data />
        </activity-alias>

        <service>
            <intent-filter> . . . </intent-filter>
            <meta-data/>
        </service>

        <receiver>
            <intent-filter> . . . </intent-filter>
            <meta-data />
        </receiver>

        <provider>
            <grant-uri-permission />
            <meta-data />
            <path-permission />
        </provider>

        <uses-library />

    </application>

</manifest>
查看更多
若你有天会懂
5楼-- · 2018-12-31 18:24

This error message (AAPT2 error: check logs for details ) is not helpful because it doesn't tell you what the real problem is.

In my case, it was due to a missing resource XML drawable file.

error: failed linking file resources.

org.gradle.tooling.BuildException: Failed to process resources, see aapt output above for details.

I only figured out because I undid the changes in the XML file, and this time I got a more helpful message:

error: resource drawable/ic_filter_off (aka com.xxx:drawable/ic_filter_off) not found.

Message{kind=ERROR, text=error: resource drawable/ic_filter_off (aka com.xxx:drawable/ic_filter_off) not found., sources=[C:\code\xxx\app\src\main\res\layout\app_bar_main.xml:69], original message=, tool name=Optional.of(AAPT)}
查看更多
千与千寻千般痛.
6楼-- · 2018-12-31 18:25

I had an error in my XML layout. So check your xml layout for errors.

查看更多
明月照影归
7楼-- · 2018-12-31 18:25

As @Izabela Orlowska pointed out: In my case the problem occurred due to some files that could not be found inside the gradle cache folder. (Windows OS)

The default gradle path was inside my user folder. the path contained umlauts and a space. I moved the gradle folder by setting GRADLE_HOME and GRADLE_USER_HOME environment variable to some newly created folder without umlauts or spaces in path. This fixed the problem for me.

查看更多
登录 后发表回答