Error: Execution failed for task ':app:clean'. Unable to delete file
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
.
Related videos on Youtube
Comments
-
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'sbuild.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 over 8 yearsinvalidate cache and restart studio from file menu.
-
Bryony over 8 yearsI had already tried that, but I tried it again just in case. Still no luck though.
-
Mohammad Tauqir over 8 yearsThis 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 over 8 yearsRestarted my PC and the problem still persists. Thanks for your help though.
-
Kishan Solanki almost 4 yearsI solved it by deleting build folder from Explorer
-
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 over 8 yearsYou can use
gradle --stop
to stop any stuck daemons as an alternative. -
Song about 8 yearsI'm using sshfs. Same issue here. Disabling instant run works for me.
-
Yogesh Seralia about 8 yearsYes,DISABLING INSTANT RUN Studio 2.0 beta 7 made resolved this exception
-
Rachita Nanda about 8 yearsMy 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 about 8 yearsThis 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 almost 8 yearsOccurring in 2.2 preview 1 for me, so I don't think it is fixed.
-
codiac almost 8 yearsI 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 almost 8 yearsThis worked for me, until Android Studio was updated. For v2.1.2 this is no longer sufficient. Any additional fixes gratefully received.
-
kshepherd almost 8 yearsI 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 almost 8 yearsTyping the above command started a download, unpack, and install, which took some time. After that, this solution worked for me.
-
mwieczorek almost 8 yearsIt 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 over 7 yearsI've tried to delete the intermediates directory, but Windows tells me I can't delete the directory. Weird.
-
jose over 7 yearsI'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 over 7 yearsAlso using AS v2.1.3 and @jose 's solution works here, Win 7, NTFS.
-
Abhijit Gujar over 7 yearsI am on ubuntu i ran the gradlew clean as instructed above but did not worked ..:( .command not found .
-
Nithinjith over 7 yearsThe "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 over 7 years@AbhijitGujar You should try
./gradlew clean
on Ubuntu instead. (It will run thegradlew
executable from the project directory.) -
Androidcoder over 7 yearsInstant 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 over 7 yearsIt worked for me on one of the projects but not on another :(
-
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 over 7 yearsIs there some automatic way to kill the processes. I waste so much time by killing it manually in every clean.
-
tamtom about 7 yearsyes 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 over 6 yearsError:(21, 0) Declaring custom 'clean' task when using the standard Gradle lifecycle plugins is not allowed.
-
Venryx over 6 yearsYeah, 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 about 6 yearsFile -> 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 over 5 yearsIn my case, there was an open Meld (compare and merge tool) that was locking the folder to clean
-
Rohit Sharma over 4 yearsThis worked. Thank you! :) But it took almost 5 hrs. time to complete.
-
Dasser Basyouni about 4 yearsin my case i didn't found Java TM so i restarted the windows
-
wangqi060934 about 4 years@DasserBasyouni try to kill "OpenJDK Platform binary"
-
Corie LeClair about 4 yearsAfter you should run gradle build, then gradle clean.
-
Mohamed Ibrahim almost 4 yearstry using sudo if that is UNIX based system
-
Michel Fernandes almost 4 yearstaskkill /F /IM java.exe was the solution for me. Thanks!
-
Utkarsh almost 4 yearsClose all the windows which are opened except the cmd
-
Luciano Brum over 3 yearsIt 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 over 2 yearsWell, 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 over 2 yearsCleaning 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 over 2 yearsgreat.. worked for me <3
-
gordinmitya over 2 yearstnx, that helps I had two demons run at the same time
-
Cuong Vo over 2 yearsThis solution is worked for me. I am on RN and just need to close all device connection before clean build.