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

238,692

Solution 1

After I posted a bug report to the Kotlin bug tracker, I was notified of Issue 61300 on the AOSP tracker. That seems to be the cause. Since there's nothing I can do at the moment, I'll mark this question as answered, and I'll update the answer if the bug is fixed.

In the meantime, if you're running Windows, I believe I've found a workaround. You'll need to download LockHunter (at your own risk of course), then add the following to your module's gradle.build file, replacing the lockhunter variable with your path to LockHunter.exe:

task clean(type: Exec) {
    ext.lockhunter = '\"C:\\LockHunter.exe\"'
    def buildDir = file(new File("build"))
    commandLine 'cmd', "$lockhunter", '/delete', '/silent', buildDir
}

This causes LockHunter to forcefully and silently unlock and delete the build files when the app:clean task runs.

Solution 2

I killed all the java TM processes in the task manager and it let me to rebuild

Solution 3

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.

Solution 4

Try File -> Invalidate Caches and Restart

This worked for me

Solution 5

Clean project from Terminal using this command gradlew clean.

enter image description here

Share:
238,692

Related videos on Youtube

Bryony
Author by

Bryony

Hobbyist gamedev. Trans woman and proud!

Updated on February 17, 2022

Comments

  • Bryony
    Bryony over 2 years

    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

    • Mohammad Tauqir
      Mohammad Tauqir over 8 years
      invalidate cache and restart studio from file menu.
    • Bryony
      Bryony over 8 years
      I had already tried that, but I tried it again just in case. Still no luck though.
    • Mohammad Tauqir
      Mohammad Tauqir over 8 years
      This kind of issue often occurs to me. if first option did not work then log off from system then start studio after logging in.
    • Bryony
      Bryony over 8 years
      Restarted my PC and the problem still persists. Thanks for your help though.
    • Kishan Solanki
      Kishan Solanki almost 4 years
      I solved it by deleting build folder from Explorer
  • Some Noob Student
    Some Noob Student over 8 years
    +1. Zombie Java process was the issue. If you're on Win 10, open Task Manager; look for Java (TM) Platform SE binary; End Task.
  • Jayson Minard
    Jayson Minard over 8 years
    You can use gradle --stop to stop any stuck daemons as an alternative.
  • Song
    Song about 8 years
    I'm using sshfs. Same issue here. Disabling instant run works for me.
  • Yogesh Seralia
    Yogesh Seralia about 8 years
    Yes,DISABLING INSTANT RUN Studio 2.0 beta 7 made resolved this exception
  • Rachita Nanda
    Rachita Nanda about 8 years
    My Android Studio 2.0 was stuck at refreshing a particular project. I tried everything and accidentally followed this answer & it solved by problem which was different from the question.I was stuck for so long . I wish I could upvote you more times! Thank you
  • Admin
    Admin about 8 years
    This worked for me. In case it helps someone: To disable Instant Run in version 2.1 of Android Studio that's File -> Settings -> Build, Execution, Deployment -> Instant Run and then un-check the first box "Enable Instant Run...".
  • Rohan Kandwal
    Rohan Kandwal almost 8 years
    Occurring in 2.2 preview 1 for me, so I don't think it is fixed.
  • codiac
    codiac almost 8 years
    I solved the clean problem with this but unable to do the same for running the app. Still get the same error. Got any idea; maybe change the clean with something else?
  • kshepherd
    kshepherd almost 8 years
    This worked for me, until Android Studio was updated. For v2.1.2 this is no longer sufficient. Any additional fixes gratefully received.
  • kshepherd
    kshepherd almost 8 years
    I have found, like some others, that killing java processes works. Don't kill the first one, it is probably your Android Studio: "ps -All | grep java" and then kill -9 nnnn for the process ids of the second, etc processes.
  • Dale
    Dale almost 8 years
    Typing the above command started a download, unpack, and install, which took some time. After that, this solution worked for me.
  • mwieczorek
    mwieczorek almost 8 years
    It took 11 minutes for this to complete, but it seems to work.A stitch in time saves nine. This should be the accepted answer.
  • tronman
    tronman over 7 years
    I've tried to delete the intermediates directory, but Windows tells me I can't delete the directory. Weird.
  • jose
    jose over 7 years
    I'm using AS v2.1.3. In my case, the "Instant Run" box was already unchecked, so I checked it, applied it, Clean & Rebuid the project (the error was still there) and then unchecked it, applied it, Clean & Rebuid the project and the error was gone.
  • J E Carter II
    J E Carter II over 7 years
    Also using AS v2.1.3 and @jose 's solution works here, Win 7, NTFS.
  • Abhijit Gujar
    Abhijit Gujar over 7 years
    I am on ubuntu i ran the gradlew clean as instructed above but did not worked ..:( .command not found .
  • Nithinjith
    Nithinjith over 7 years
    The "Invalidate cache and restart" makes Android studio to stuck in Window 8 PC. I am using Android studio 2.2.2. I can use this feature at all. Anyone has a right solution for this?.
  • TerraPass
    TerraPass over 7 years
    @AbhijitGujar You should try ./gradlew clean on Ubuntu instead. (It will run the gradlew executable from the project directory.)
  • Androidcoder
    Androidcoder over 7 years
    Instant run was already unchecked for me, as I get the 'can't delete file' error every studio upgrade (this time to 2.3 canary 2). I also had to check it, attempt project run, get error message 'need to install apk 23' (the apk of my test device), cancel the run, uncheck instant run, run project and wait 10 minutes for the gradle build with no error.
  • abggcv
    abggcv over 7 years
    It worked for me on one of the projects but not on another :(
  • abggcv
    abggcv over 7 years
    ./gradlew clean did not solve this problem for me. It showed the same error again i.e. "unable to delete directory"
  • Riki
    Riki over 7 years
    Is there some automatic way to kill the processes. I waste so much time by killing it manually in every clean.
  • tamtom
    tamtom about 7 years
    yes I was having this problem , there was a report file that is saved in build/report/checkstyle.html , closing this file solved my problem , thanks
  • Shashwat Gupta
    Shashwat Gupta over 6 years
    Error:(21, 0) Declaring custom 'clean' task when using the standard Gradle lifecycle plugins is not allowed.
  • Venryx
    Venryx over 6 years
    Yeah, I've read elsewhere that the Android Studio/gradle/React Native/packager/watchman system has issues (for some reason) on NTFS file-systems.
  • Jan Bergström
    Jan Bergström about 6 years
    File -> Invalidate Caches and Restart works but takes so much time it is not OK. The basic problem of Win10 file privileges malfunctions re-arrived for me updating to SDK 27 (I accepted the update message). And after the same update the AS cries for NDK 15 but there is only a NDK14 available, set build gradle compile SDK to 26 and it don't cry. Look like we are in between two generations here?? In Win10 x64 there is also problems with the emulators that need data to be wiped to be reused. Sound like an awful lot of details to fix?
  • Armando
    Armando over 5 years
    In my case, there was an open Meld (compare and merge tool) that was locking the folder to clean
  • Rohit Sharma
    Rohit Sharma over 4 years
    This worked. Thank you! :) But it took almost 5 hrs. time to complete.
  • Dasser Basyouni
    Dasser Basyouni about 4 years
    in my case i didn't found Java TM so i restarted the windows
  • wangqi060934
    wangqi060934 about 4 years
    @DasserBasyouni try to kill "OpenJDK Platform binary"
  • Corie LeClair
    Corie LeClair about 4 years
    After you should run gradle build, then gradle clean.
  • Mohamed Ibrahim
    Mohamed Ibrahim almost 4 years
    try using sudo if that is UNIX based system
  • Michel Fernandes
    Michel Fernandes almost 4 years
    taskkill /F /IM java.exe was the solution for me. Thanks!
  • Utkarsh
    Utkarsh almost 4 years
    Close all the windows which are opened except the cmd
  • Luciano Brum
    Luciano Brum over 3 years
    It worked for me, but before that, I need to give proper permissions to run ./gradlew, with chmod +x ./gradlew. Using Ubuntu 20.04 and Android version 4.1.1, and gradle version 6.5.
  • Divya Gupta
    Divya Gupta over 2 years
    Well, invalidate cache and restart is just a vague solution to any problem. Instead, one should focus on finding out what is the exact root cause of a problem, rather than compromising your local history of cache.
  • Divya Gupta
    Divya Gupta over 2 years
    Cleaning gradle from command line sometimes does the exact operation as you click on Clean Project. So instead, one should try to kill the process which is holding up the build files and preventing its deletion. That way, it gets solved.
  • ABHIMANGAL MS
    ABHIMANGAL MS over 2 years
    great.. worked for me <3
  • gordinmitya
    gordinmitya over 2 years
    tnx, that helps I had two demons run at the same time
  • Cuong Vo
    Cuong Vo over 2 years
    This solution is worked for me. I am on RN and just need to close all device connection before clean build.