原本好好的项目,突然有一天打开就开始报错AAPT2 error: check logsfor details。
展开错误详细信息
错误文本
com.android.build.gradle.tasks.ResourceException:Error: java.util.concurrent.ExecutionException:com.android.builder.internal.aapt.v2.Aapt2Exception: AAPT2 error: check logsfor details
错误全文
com.android.build.gradle.tasks.ResourceException: Error: java.util.concurrent.ExecutionException: com.android.builder.internal.aapt.v2.Aapt2Exception: AAPT2 error: check logs for detailsat com.android.build.gradle.tasks.MergeResources.doFullTaskAction(MergeResources.java:296)at com.android.build.gradle.internal.tasks.IncrementalTask.taskAction(IncrementalTask.java:106)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:498)at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:73)at org.gradle.api.internal.project.taskfactory.IncrementalTaskAction.doExecute(IncrementalTaskAction.java:46)at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:39)at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:26)at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter$1.run(ExecuteActionsTaskExecuter.java:121)at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199)at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110)at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:110)at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:92)at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:70)at org.gradle.api.internal.tasks.execution.OutputDirectoryCreatingTaskExecuter.execute(OutputDirectoryCreatingTaskExecuter.java:51)at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:62)at org.gradle.api.internal.tasks.execution.ResolveTaskOutputCachingStateExecuter.execute(ResolveTaskOutputCachingStateExecuter.java:54)at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:60)at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:97)at org.gradle.api.internal.tasks.execution.CleanupStaleOutputsExecuter.execute(CleanupStaleOutputsExecuter.java:87)at org.gradle.api.internal.tasks.execution.ResolveTaskArtifactStateTaskExecuter.execute(ResolveTaskArtifactStateTaskExecuter.java:52)at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52)at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:54)at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)at org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:34)at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker$1.run(DefaultTaskGraphExecuter.java:248)at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199)at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110)at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:241)at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:230)at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.processTask(DefaultTaskPlanExecutor.java:123)at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.access$200(DefaultTaskPlanExecutor.java:79)at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:104)at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:98)at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.execute(DefaultTaskExecutionPlan.java:626)at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.executeWithTask(DefaultTaskExecutionPlan.java:581)at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.run(DefaultTaskPlanExecutor.java:98)at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)at java.lang.Thread.run(Thread.java:745)
Caused by: Error: java.util.concurrent.ExecutionException: com.android.builder.internal.aapt.v2.Aapt2Exception: AAPT2 error: check logs for detailsat com.android.ide.common.res2.MergedResourceWriter.end(MergedResourceWriter.java:332)at com.android.ide.common.res2.DataMerger.mergeData(DataMerger.java:301)at com.android.ide.common.res2.ResourceMerger.mergeData(ResourceMerger.java:412)at com.android.build.gradle.tasks.MergeResources.doFullTaskAction(MergeResources.java:285)... 48 moreSuppressed: java.lang.RuntimeException: Some file processing failed, see logs for detailsat com.android.builder.internal.aapt.QueuedResourceProcessor.waitForAll(QueuedResourceProcessor.java:121)at com.android.builder.internal.aapt.QueuedResourceProcessor.end(QueuedResourceProcessor.java:141)at com.android.builder.internal.aapt.v2.QueueableAapt2.close(QueueableAapt2.java:104)at com.android.build.gradle.tasks.MergeResources.doFullTaskAction(MergeResources.java:293)... 48 more
Caused by: java.util.concurrent.ExecutionException: java.util.concurrent.ExecutionException: com.android.builder.internal.aapt.v2.Aapt2Exception: AAPT2 error: check logs for detailsat com.google.common.util.concurrent.AbstractFuture.getDoneValue(AbstractFuture.java:503)at com.google.common.util.concurrent.AbstractFuture.get(AbstractFuture.java:462)at com.google.common.util.concurrent.AbstractFuture$TrustedFuture.get(AbstractFuture.java:79)at com.android.ide.common.res2.MergedResourceWriter.end(MergedResourceWriter.java:327)... 51 more
Caused by: java.util.concurrent.ExecutionException: com.android.builder.internal.aapt.v2.Aapt2Exception: AAPT2 error: check logs for detailsat com.google.common.util.concurrent.AbstractFuture.getDoneValue(AbstractFuture.java:503)at com.google.common.util.concurrent.AbstractFuture.get(AbstractFuture.java:462)at com.google.common.util.concurrent.AbstractFuture$TrustedFuture.get(AbstractFuture.java:79)at com.android.builder.internal.aapt.v2.QueueableAapt2.lambda$compile$0(QueueableAapt2.java:136)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)... 1 more
Caused by: com.android.builder.internal.aapt.v2.Aapt2Exception: AAPT2 error: check logs for detailsat com.android.builder.png.AaptProcess$NotifierProcessOutput.handleOutput(AaptProcess.java:443)at com.android.builder.png.AaptProcess$NotifierProcessOutput.err(AaptProcess.java:395)at com.android.builder.png.AaptProcess$ProcessOutputFacade.err(AaptProcess.java:312)at com.android.utils.GrabProcessOutput$1.run(GrabProcessOutput.java:104)
排除代码问题
检查了半天没有找到问题在哪,又换另一台电脑,同样的代码和IDE(都是Android Studio 3.1.2,只是在Ubuntu上),运行没有任何问题,那么问题应该并不是出在代码上,于是想到新建一个项目试试。于是直接创建了一个新项目,没有修改任何设置,创建了一个甚至连活动都没有新项目
果然,刚刚创建的项目在Build时报出同样的错误
查阅资料,有网友表示“如果有问题可以退回到之前的版本,打开gradle.properties,添加如下内容”
android.enableAapt2=false
对于一个习惯跟在技术更新前沿的人来说,回退版本是不可能的,这辈子都不可能的,但是添加代码还是可以试试,于是便做出了尝试,但还是遇到了报错:
错误信息:
The option 'android.enableAapt2' isdeprecated and should not be used anymore.
Use 'android.enableAapt2=true' to removethis warning.
It will be removed at the end of 2018..
翻译一下就是:
选项“android.enableAapt2”已被弃用,不应再使用。
使用“android.enableAapt2=true”来删除这个警告。
它将在2018年底被移除。
很明显,我们使用“android.enableAapt2=false”来关闭AAPT2是行不通的,这个方法已经过时了,关闭之后Android Studio告诉我们要把它打开,这样就陷入了一个死循环,所以我们还是需要找到问题的根本原因在哪里才能解决。
所以直接删除了刚才插入的“android.enableAapt2=false”继续找办法
打印报错信息
查阅各方资料,学到了一个可以在Android studio中打印详细报错信息的方法:
在命令行中进入项目的根目录,或者可以在Android studio的Terminal中直接操作也可以,然后敲入一个命令:
gradlewcompileDebug --stacktrace
就可以输出较详细的信息,然后根据命令行给出的提示,还可以在后面加上-info或者-debug的选项得到更详细的信息,于是这个命令可以这样写:
gradlewcompileDebug --stacktrace -info
或者:
gradlewcompileDebug --stacktrace -debug
先试第一行代码
在Android Studio自带的Terminal内输入
gradlewcompileDebug --stacktrace
回车之后并没有给我们太多提示,但是在建议中有一句提到“Task'compileDebug' is ambiguous in root project”(翻译:命令“'compileDebug”在根项目中是不明确的),回看之前的报错,第一行提到了一个资源异常“ResourceException”
于是尝试着把命令改成了
gradlewcompileDebugSources --stacktrace -info
再看显示出来的信息,好像发现了点什么
错误信息
> Task :app:mergeDebugResources FAILED
Exception while processing job : QueuedJob{
title=Compiling abc_ic_menu_copy_mtrl_am_alpha.png,
task={from=C:\Users\郑维一\.gradle\caches\transforms-1\files-1.1\appcompat-v7-26.1.0.aar\a55ab1e075dbd5b5da002f93c40b9cfb\res\drawable-xhdpi-v4\abc_ic_menu_copy_mtrl_am_alpha.png, to=F:\Projects\AndroidStudioProjects\JinchengNewsAndroid\app\build\intermediates\res\merged\debug},
future=com.google.common.util.concurrent.SettableFuture@4d8f81a6} : com.android.builder.internal.aapt.v2.Aapt2Exception: AAPT2 error: check logs for details
Exception while processing job : QueuedJob{
title=Compiling abc_btn_radio_to_on_mtrl_000.png,
task={from=C:\Users\郑维一\.gradle\caches\transforms-1\files-1.1\appcompat-v7-26.1.0.aar\a55ab1e075dbd5b5da002f93c40b9cfb\res\drawable-hdpi-v4\abc_btn_radio_to_on_mtrl_000.png, to=F:\Projects\AndroidStudioProjects\JinchengNewsAndroid\app\build\intermediates\res\merged\debug},
future=com.google.common.util.concurrent.SettableFuture@6d94b091} : com.android.builder.internal.aapt.v2.Aapt2Exception: AAPT2 error: check logs for details
Exception while processing job : QueuedJob{
title=Compiling notify_panel_notification_icon_bg.png,
task={from=C:\Users\郑维一\.gradle\caches\transforms-1\files-1.1\support-compat-26.1.0.aar\2efeba8952eb2c0e463a57b63c49f99f\res\drawable-xhdpi-v4\notify_panel_notification_icon_bg.png, to=F:\Projects\AndroidStudioProjects\JinchengNewsAndroid\app\build\intermediates\res\merged\debug},
future=com.google.common.util.concurrent.SettableFuture@19e643ef} : com.android.builder.internal.aapt.v2.Aapt2Exception: AAPT2 error: check logs for details
Shutdown finished in 244ms
Error: java.util.concurrent.ExecutionException: com.android.builder.internal.aapt.v2.Aapt2Exception: AAPT2 error: check logs for details
:app:mergeDebugResources (Thread[Task worker for ‘:’ Thread 3,5,main]) completed. Took 5.716 secs.
发现了中文!
敏感的察觉到,这里可能有问题!(胡说八道,其实是请教了一位大佬)
问题根源
“有人提到目前的这种非AsciⅡ的确是AAPT2和Gradle插件的一个bug,但现在已经被修复,只是Android Studio还未更新成这个被修复的版本。”(大佬说的,暂未查证资料来源)
解决办法
所以现在有两个解决办法,
一、修改gradle的缓存目录
这个可以通过android studio的设置中找到gradle,配置另一个非中文目录来缓存。
File -> Settings -> Build, Execution,Deployment -> Gradle
进入这个页面找到“Service directory path”项,将里面的路径换到一个非中文路径就好了
build成功!
二、指定你的项目使用较新的gradle 插件
在build.gradle(Project)中修改Gradle版本到3.2.0-alpha11或更新版本就可以了
repositories { google() … } dependencies { classpath ‘com.android.tools.build:gradle:3.2.0-alpha11’ } }
最后还是提醒大家,开发程序不要使用中文目录,会带来无穷的困扰啊!!!