Error: Execution failed for task ':app:clean'. Unable to delete file

Bryony picture Bryony · Oct 2, 2015 · Viewed 202k times · Source

I'm trying to rebuild my Android Studio Gradle project (containing mostly Kotlin code), but it started to throw an UnableToDeleteFileException during the cleaning/rebuilding process:

Execution failed for task ':app:clean'.
> Unable to delete file: C:\Users\User\KotlinGameEngine\app\build\intermediates\exploded-aar\com.android.support\appcompat-v7\23.0.1\jars\classes.jar

This started happening after I tried to change my project's package structure. Unfortunately, I did it by renaming and moving the source folders rather than refactoring through Android Studio, which was a bad idea.

I've been searching for a solution to this problem all day, and these are the things I have tried to no avail:

  • Doing a Gradle sync;
  • Reinstalling Java JRE and Java SDK;
  • Reinstalling the latest version of Android Studio (1.4);
  • Rolling back to the previous AS version (1.3);
  • Invalidating the AS cache and restarting;
  • Deleting the gradle and .gradle directories in the project directory;
  • Deleting the .gradle directory in my user directory;
  • Running gradlew clean from the AS terminal;
  • Manually copying the sources over to a new project (weird that it somehow persists across projects...)

Things that I've tried with a little success, but only let me perform one more clean and rebuild before the error occurs again:

  • Closing AS, manually deleting the build files, and opening it again;
  • Killing the java.exe process while AS is running (this could technically be done every time, but it's tedious and slows down the build process)

So it seems that compile the Java process may put a lock on the build files for some reason, but it might also be something to do with Kotlin. I have a (more mature) Java Android project that I'm working on, though I can't reproduce this error when cleaning it. It seems to only happen to my Kotlin project.

Update:

I've found that the problem is being caused by the Kotlin Android plugin. The problem disappears when I remove apply plugin: 'kotlin-android' from the module's build.gradle file and comes back when I reinsert it. Feel free to offer any insight into this.

Update 2:

The last update isn't the cause. I found that if a project contains a Kotlin file then rebuilding and cleaning fails. It continues to fail, even if all the Kotlin files are removed, until the background Java process is killed, meaning it has some kind of lock on the build files. I submitted a bug here with more details and steps to reproduce the bug: KT-9440

Answer

Irfan picture Irfan · Mar 2, 2016

If you are using Android Studio 2.0 Beta, this issue might appear (more likely if you are working on NTFS filesystem) and it seems like the "Instant Run" is the culprit. Search for "Instant Run" in settings and uncheck the box.

I have filed an issue on the bug tracker.