189

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

Zoe
  • 27,060
  • 21
  • 118
  • 148
Bryony
  • 2,209
  • 2
  • 11
  • 12

42 Answers42

138

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

Andres Suarez
  • 1,389
  • 2
  • 7
  • 3
123

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.

Irfan
  • 1,758
  • 3
  • 24
  • 32
  • I'm using sshfs. Same issue here. Disabling instant run works for me. – Song Apr 07 '16 at 18:17
  • 2
    Yes,DISABLING INSTANT RUN Studio 2.0 beta 7 made resolved this exception – Yogesh Seralia Apr 11 '16 at 05:31
  • 1
    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 – Rachita Nanda Apr 12 '16 at 18:53
  • 11
    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...". –  Apr 29 '16 at 17:33
  • This worked for me, until Android Studio was updated. For v2.1.2 this is no longer sufficient. Any additional fixes gratefully received. – kshepherd Jun 07 '16 at 12:34
  • 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. – kshepherd Jun 07 '16 at 17:30
  • 1
    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. – jose Sep 01 '16 at 09:10
  • Also using AS v2.1.3 and @jose 's solution works here, Win 7, NTFS. – J E Carter II Sep 27 '16 at 20:17
  • 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. – Androidcoder Nov 24 '16 at 17:42
53

Try File -> Invalidate Caches and Restart

This worked for me

Cloy
  • 2,141
  • 23
  • 32
  • 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?. – Nithinjith Oct 28 '16 at 04:38
  • 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? – Jan Bergström Apr 01 '18 at 23:31
31

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.

Bryony
  • 2,209
  • 2
  • 11
  • 12
  • 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? – codiac Jun 01 '16 at 13:56
  • Error:(21, 0) Declaring custom 'clean' task when using the standard Gradle lifecycle plugins is not allowed. – Shashwat Gupta Aug 23 '17 at 18:55
30

Clean project from Terminal using this command gradlew clean.

enter image description here

Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
Biswajit Karmakar
  • 9,799
  • 4
  • 39
  • 41
  • 2
    Typing the above command started a download, unpack, and install, which took some time. After that, this solution worked for me. – Dale Jun 18 '16 at 18:15
  • 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. – mwieczorek Jun 25 '16 at 00:23
  • I am on ubuntu i ran the gradlew clean as instructed above but did not worked ..:( .command not found . – Abhijit Gujar Oct 10 '16 at 11:14
  • 1
    @AbhijitGujar You should try `./gradlew clean` on Ubuntu instead. (It will run the `gradlew` executable from the project directory.) – TerraPass Nov 03 '16 at 00:34
  • 11
    `./gradlew clean` did not solve this problem for me. It showed the same error again i.e. "unable to delete directory" – abggcv Jan 03 '17 at 17:13
  • 1
    try using sudo if that is UNIX based system – Mohamed Ibrahim Jun 04 '20 at 08:46
  • 1
    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. – lubrum Nov 30 '20 at 19:37
  • 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. – Divya Gupta Oct 21 '21 at 09:32
25

I solved with command:

taskkill /F /IM java.exe

and then:

gradle assembleDebug 
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
Rick
  • 311
  • 3
  • 5
17

Deleting the directory intermediates is a quick fix for problem.

The directory will be rebuilt when the project is rebuilt.

Aleks
  • 4,866
  • 3
  • 38
  • 69
DeaMon1
  • 572
  • 5
  • 10
12

I was facing same issue on Android Studio 2.2 preview 1, solution by @AndresSuarez was correct but for some reasons I couldn't find JAVA TM process in my task manager. So I tried the following solution and it worked -

Open command prompt and type TASKKILL /F /IM java.exe. This will kill all JAVA TM processes automatically. Now re-compile the app again, it will work.

Additionally, you can create a .bat file, add the above code in it and run it every time you face the issue.

Rohan Kandwal
  • 9,112
  • 8
  • 74
  • 107
6

I had the same problem and this worked for me:

  1. close Android Studio.
  2. delete intermediates directory (as long as Android Studio is opened, this directory can't be deleted)
  3. open Android Studio again
M. Chavoshi
  • 1,023
  • 9
  • 17
5

I had this problem in Android Studio 2.3.

I simply restarted Android Studio and after that I could clean without complaints.

Johan Lund
  • 133
  • 3
  • 5
5

For me, this happened because of an active debugging process. So, before cleaning or rebuilding, make sure to kill all active processes. To nail down the success, execute Invalidate Caches/Restart.

JorgeAmVF
  • 1,660
  • 3
  • 21
  • 32
Dmitry Smolyaninov
  • 2,159
  • 1
  • 18
  • 32
4

For me it helps when I Exit Android Studio, click one more time Clean Project (it appears the same error) and next click Make Project - after that of course Run 'app' button in Android Studio.

y07k2
  • 1,898
  • 4
  • 20
  • 36
4

For me, the culprit is adb holding onto the apk file since I utilise it to install and start the application on my physical device through command line.

So simply:

Task Manager > End process adb.exe

And then the file is free to be deleted and the project may be cleaned

benallansmith
  • 816
  • 3
  • 10
  • 26
3

I solved this one in my ubuntu like this.

  1. Check for the processes running in your system(Ubuntu) like in task manager in windows.

    Ubuntu terminal command to list out all processes running ----> "gnome-system-monitor"

  2. Terminate or end the JAVA and ANDROID processes in the list.

  3. Again start the studio and import project.

Its works for me without any problems. Hope this helps...

I thought this is the problem with the process that are already created and Duplicated.

HariPrasad
  • 131
  • 1
  • 5
3

The solution is quite easy.

This is one of the solution which worked for me.

It might be possible that your project's app/build/outputs/apk folder is opened.

so just close this folder and rebuild your project. and it will be solved.

Mehul
  • 2,141
  • 1
  • 15
  • 15
  • 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 – tamtom Mar 02 '17 at 18:16
3

Find programs who used app/build/outputs/apk folder, then just delete folder.

I think Android Studio have to delete old apk folder before rebuilding.

Mete
  • 2,805
  • 1
  • 28
  • 38
3

For my case, I resolve it by -

  1. First, close the app if it is running in the emulator.
  2. Then run the command gradle --stop in the Terminal in Android studio.
  3. Then clean project and rebuild project
Gk Mohammad Emon
  • 6,084
  • 3
  • 42
  • 42
2

Some times intermediates creates problem so delete it and Rebuild project

OR

simply run cmd command - > gradlew clean

in your project folder in work space (its work for me)

Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
2

As suggested in the bug report, uncommenting the line

idea.jars.nocopy=false

in the idea.properties file has solved the issue for me.

Note that this needs to be done every time Android Studio updates.

Jose Gómez
  • 3,110
  • 2
  • 32
  • 54
2

In Android Studio 3.0, I had the same issue. This fixed it:

  1. Close Studio
  2. Delete projectRoot/build/ and projectRoot/app/build/
  3. Restart Studio
Joseph Bani
  • 644
  • 6
  • 16
2

react native devs

run

sudo cd android && ./gradlew clean

and if you want release apk

sudo cd android && ./gradlew assembleRelease

Hope it will help someone

Muhammad Ashfaq
  • 2,359
  • 5
  • 20
  • 46
2

i simply run cmd command - > gradlew clean , And it fix the issue

1

If you are testing with a local backend (java servlet on local google app engine) the running process blocks some files. So you are not able to live deploy. So in this case you can solve this by stopping the local backend before starting clean or build. You find the option under "Run -> Stop backend".

Tino
  • 299
  • 3
  • 15
1

I had the same error, tried multiple ways but the solution worked for me was to delete build folders from /android and /android/app directories.

run react-native run-android worked for me.

Eltaf Hussain
  • 3,071
  • 1
  • 14
  • 20
1

I solved it by global searching the missing directory in the project. then delete any files containing that keyword. it can clean successfully after I remove all the build and .externalNativeBuild directories manually.

wdanxna
  • 10,699
  • 2
  • 23
  • 24
1

I had the same issue after moving my project from D: to G: drive but disk checking solved my issue

I used chkdsk /f /r /x G: ** here some command line switches were used:**

/F Fixes errors on the disk

/R Locates bad sectors and recovers readable information (implies /F)

/X Forces the volume to dismount first if necessary (implies /F) (important)

Note: /X is important because it will dismount the drive and you can manually delete the build directory of your project,

now rebuild the project

1

My Solution for this was very very simple ( if you are using Windows). 1- Close Android Studio. 2- Run Android Studio as Administrator. 3- That is it.

1

I just solved this exact problem for myself.

The problem was that somebody else had created the file so even though I have admin rights on the computer I was unable to make changes to the file or files. You need to go into the properties of the file or folder and change the ownership or add ownership. This web page explains it well step by step what you need to do.

Once I did the above, I found the file in file explorer and manually extracted it. I don't think it's needed in the android studio project if it was trying to delete it anyway.

Adam Higgins
  • 705
  • 1
  • 10
  • 25
1

Another application that can be using the resources is apparently Android Studio's Kotlin REPL. I closed that, and then I could build again no problem.

1

I have tried to remove all the Java services but still it is failing while executing the gradlew clean build command from IntelliJ terminal. I even verified the other applications I have opened somewhere the same path which I am trying the delete. But could not find any of such. So, finally restarted the IntelliJ IDE and able to proceed build smoothly.

Nallamachu
  • 1,420
  • 18
  • 34
0

This issue appeared to me in android studio 2.0 stable channel and the solution was due to a problem happened while updating my android studio i solved this by installing a fresh android studio. after deleting all old files for the old installation. and to keep the very nice feature of Instant Run

Mohamed
  • 761
  • 9
  • 19
0

if you're still having issues then update to the latest version of Android Studio(its 2.1 as of now), might be it was a bug with older version of Android Studio. Its resolved for me now.

Ishant Sagar
  • 228
  • 5
  • 13
0

Cleaning the project in Android studio and running again fixed the issue. May be do "Make Project" as well.

sofs1
  • 3,834
  • 11
  • 51
  • 89
0

I had the same problem and I solved it by moving project folder into ext4 partition

  • Yeah, I've read elsewhere that the Android Studio/gradle/React Native/packager/watchman system has issues (for some reason) on NTFS file-systems. – Venryx Nov 21 '17 at 19:33
0

I am using Android Studio 3.0 Beta 7. Clean Project, Invalidate caches, restart studio did not work but Updating Kotlin version to 1.1.51 and restart studio solved this issue for me.

adithya reddy
  • 441
  • 2
  • 7
0

the thing that worked for me was to close visual studio code and open it again ... i guess the apk was folder was opened somewhere in code .... so just try it might work for you as well

MSD
  • 437
  • 6
  • 18
0

In my case node.js was using some resources in build folder (my app in reactnative). So I killed node.js and it solved.

Ramin Bateni
  • 16,499
  • 9
  • 69
  • 98
0

simple solution :

in android or any jetbrains products when you click to you will find 'invalidate cache and restart ' click on it and all the problems will be solved

Farouk Benarous
  • 897
  • 8
  • 8
  • 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 Oct 21 '21 at 09:30
0

Though this has already been answered, I came from a non-android project and found this, but it could not solve my issue. If someone does see this error, delete the "build" folder manually through your file manager. For me, this repaired the code failing to compile in Kotlin.

Corie LeClair
  • 51
  • 1
  • 9
0

try to remove these files manually

In my case I just removed all build directories from project and subprojects

yoAlex5
  • 29,217
  • 8
  • 193
  • 205
0

Here in 2022 I having the same unable to delete file gradle error while trying to build my android project in react-native. When working on the project on a mac I wasn't getting any issues, but when I switch to Windows my build would fail. I've found cleaning the project often resolve build failures and I'm lazy so I had added npm scripts for cleaning my project for both windows and mac, which was part of my problem.

On macOS my clean script included watchman watch-del-all and my Windows did not. Me being me, had totally forgot that I had installed the watchman beta and that somehow was why my project was unable to delete.


Long story short, if you are using react-native and you get this error watchman watch-del-all may help, even on Windows

PhantomSpooks
  • 2,877
  • 2
  • 8
  • 13
-3

Follow these Steps From Build Option tab: 1) Clean Project 2) Rebuild Project 3) Make Project

saurabh yadav
  • 567
  • 6
  • 14