Failed to execute aapt (NoSuchElementException at

2019-01-11 17:39发布

问题:

The project gets built without error from Android Studio -> Build -> Build APKs and after installation it works fine. But running the project with a Run Configuration gives the following error:

    Failed to execute aapt

    org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':MyProject:transformDexWithInstantRunDependenciesApkForFlavorDebug'.
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:103)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:73)
        at org.gradle.api.internal.tasks.execution.OutputDirectoryCreatingTaskExecuter.execute(OutputDirectoryCreatingTaskExecuter.java:51)
        at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:59)
        at org.gradle.api.internal.tasks.execution.ResolveTaskOutputCachingStateExecuter.execute(ResolveTaskOutputCachingStateExecuter.java:54)
        at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:59)
        at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:101)
        at org.gradle.api.internal.tasks.execution.FinalizeInputFilePropertiesTaskExecuter.execute(FinalizeInputFilePropertiesTaskExecuter.java:44)
        at org.gradle.api.internal.tasks.execution.CleanupStaleOutputsExecuter.execute(CleanupStaleOutputsExecuter.java:91)
        at org.gradle.api.internal.tasks.execution.ResolveTaskArtifactStateTaskExecuter.execute(ResolveTaskArtifactStateTaskExecuter.java:62)
        at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:59)
        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:256)
        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:249)
        at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:238)
        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:663)
        at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.executeWithTask(DefaultTaskExecutionPlan.java:597)
        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: java.lang.RuntimeException: com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: Failed to execute aapt
        at com.android.builder.profile.Recorder$Block.handleException(Recorder.java:55)
        at com.android.builder.profile.ThreadRecorder.record(ThreadRecorder.java:104)
        at com.android.build.gradle.internal.pipeline.TransformTask.transform(TransformTask.java:212)
        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:497)
        at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:73)
        at org.gradle.api.internal.project.taskfactory.IncrementalTaskAction.doExecute(IncrementalTaskAction.java:50)
        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:124)
        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:113)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:95)
        ... 33 more
    Caused by: com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: Failed to execute aapt
        at com.android.build.gradle.internal.transforms.InstantRunDependenciesApkBuilder.transform(InstantRunDependenciesApkBuilder.java:152)
        at com.android.build.gradle.internal.pipeline.TransformTask$2.call(TransformTask.java:221)
        at com.android.build.gradle.internal.pipeline.TransformTask$2.call(TransformTask.java:217)
        at com.android.builder.profile.ThreadRecorder.record(ThreadRecorder.java:102)
        ... 49 more
    Caused by: com.android.ide.common.process.ProcessException: Failed to execute aapt
        at com.android.builder.core.AndroidBuilder.processResources(AndroidBuilder.java:809)
        at com.android.builder.core.AndroidBuilder.processResources(AndroidBuilder.java:797)
        at com.android.build.gradle.internal.transforms.InstantRunSplitApkBuilder.generateSplitApkResourcesAp(InstantRunSplitApkBuilder.java:373)
        at com.android.build.gradle.internal.transforms.InstantRunSplitApkBuilder.generateSplitApkResourcesAp(InstantRunSplitApkBuilder.java:321)
        at com.android.build.gradle.internal.transforms.InstantRunSplitApkBuilder.generateSplitApk(InstantRunSplitApkBuilder.java:210)
        at com.android.build.gradle.internal.transforms.InstantRunDependenciesApkBuilder.transform(InstantRunDependenciesApkBuilder.java:149)
        ... 52 more
    Caused by: java.util.NoSuchElementException
        at com.google.common.collect.AbstractIndexedListIterator.next(AbstractIndexedListIterator.java:80)
        at com.google.common.collect.Iterators.getOnlyElement(Iterators.java:315)
        at com.google.common.collect.Iterables.getOnlyElement(Iterables.java:263)
        at com.android.builder.internal.aapt.v1.AaptV1.makePackageProcessBuilder(AaptV1.java:202)
        at com.android.builder.internal.aapt.AbstractProcessExecutionAapt.makeValidatedPackage(AbstractProcessExecutionAapt.java:67)
        at com.android.builder.internal.aapt.AbstractAapt.link(AbstractAapt.java:34)
        at com.android.builder.core.AndroidBuilder.processResources(AndroidBuilder.java:807)
        ... 57 more

The error appeared after moving to Android Studio 3.1, Gradle 4.4 and adding apply plugin: 'io.fabric' to the build.gradle file. The Run Configuration worked fine just before update. The project is JavaVersion.VERSION_1_8 compatible. I also removed android.enableAapt2 = false from gradle.properties since it's deprecated. Any ideas what could cause that?

UPDATE: gradle.properties was cached in C:\Users\MyUser\.gradle\gradle.properties for Windows. Obviously that file was used even when I switched to the wrapper. The changes to the project's gradle.properties didn't took any effect until I removed the cache. When android.enableAapt2 = false was actually removed, the warning was gone and I could enable Instant Run.

回答1:

It is a bug in gradle plugin code that calls AAPT1. It was fixed but not cherry-picked, it should be available in the next released version (it should be fixed in 3.2 canary 3 and newer).
This only affects AAPT1. Removing the android.enableAapt2=false flag will make the code not hit this codepath (check your gradle.properties file and the extra arguments you pass to gradle from Android Studio).



回答2:

I did below and it solved this issue:

  1. In gradle.properties file:

    android.enableAapt2=false

  2. I disabled the Instant Run.(Important)



回答3:

In your gradle properties: remove android.enableAapt2=false, and upgrade the buildToolVersion in your app build.gradle and in all the other modules(if used) too.

Run the command gradlew --info assembleDebug in your terminal and check for other errors and if you find them then manually go and fix them. In my case there was a 9 patch image which was causing all the trouble.

Try to clean and rebuild your project.

Or

Try to disable instant run feature and see if it works.

Or

Try and enable multiDexEnabled.

defaultConfig {
    multiDexEnabled true
}

Or

File -> Invalidate caches and restart



回答4:

You can follow any of the following ways to fix the issue:

For Android studio 3.1 or <3.2

  1. Update gradle.properties file with android.enableAapt2=false
  2. Disable Instant Run(Ctrl+shift+A(win+Linux) or Command+shift+A(Mac) then search for Instant run).

For Android studio 3.2 or later

  1. Update Android studio to 3.2
  2. Update gradle.properties file with android.enableAapt2=true
  3. You can keep remain enable the Instant run now.


回答5:

Open the File tab and then click on Invalidate Caches \ Restart. It worked for me :).