Bryony
Bryony

Reputation: 2399

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

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:

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

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

Upvotes: 198

Views: 281251

Answers (30)

Fred
Fred

Reputation: 1748

If you unsuccessfully tried all the other answers and happen to have this problem on Ubuntu/Debian, this might be the issue:

You have installed Android Studio by copying the android-studio folder from inside the .tar you have downloaded from the official website to /opt (or any similar location that requires root privileges to be altered). You had to use sudo in order to copy to that location and now /opt/android-studio is owned by root. Android Studio will now create build artifacts with root permission but try to delete it without root permissions, which causes the error.

Fix it by running sudo chown -R <your username>:<your username> /opt/android-studio. After that you have to delete all files and folders that Android Studio manually (using sudo).

After that, the error should be gone.

Upvotes: 0

CHARAN
CHARAN

Reputation: 11

In Android Studio > File > Invalidate Caches / Restart. enter image description here

Upvotes: 0

Vladimir Salguero
Vladimir Salguero

Reputation: 5947

With React Native the commands TASKKILL /F /IM java.exe and gradlew --stop not working, for me the solution was to delete the node_modules directory and then run npm install and that's it.

Upvotes: 0

PhantomSpooks
PhantomSpooks

Reputation: 3540

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

Upvotes: 0

yoAlex5
yoAlex5

Reputation: 34175

try to remove these files manually

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

Upvotes: 0

Nallamachu
Nallamachu

Reputation: 1488

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.

Upvotes: 1

Gk Mohammad Emon
Gk Mohammad Emon

Reputation: 6938

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

Upvotes: 5

ABDESSAMAD FELOUACH
ABDESSAMAD FELOUACH

Reputation: 435

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

Upvotes: 2

user2061182
user2061182

Reputation: 29

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.

Upvotes: 1

Corie LeClair
Corie LeClair

Reputation: 61

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.

Upvotes: 0

Muhammad Ashfaq
Muhammad Ashfaq

Reputation: 2511

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

Upvotes: 2

Dmytro
Dmytro

Reputation: 2309

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.

Upvotes: 5

Adam Higgins
Adam Higgins

Reputation: 754

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.

Upvotes: 1

Biswajit Karmakar
Biswajit Karmakar

Reputation: 9897

Clean project from Terminal using this command gradlew clean.

enter image description here

Upvotes: 31

Rick
Rick

Reputation: 421

I solved with command:

taskkill /F /IM java.exe

and then:

gradle assembleDebug 

Upvotes: 26

Farouk Benarous
Farouk Benarous

Reputation: 897

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

Upvotes: 0

saurabh yadav
saurabh yadav

Reputation: 575

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

Upvotes: -4

Ramin Bateni
Ramin Bateni

Reputation: 17395

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

Upvotes: 0

msd117
msd117

Reputation: 386

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

Upvotes: 0

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.

Upvotes: 1

Sumanpal Singh Saggu
Sumanpal Singh Saggu

Reputation: 461

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

Upvotes: 1

benallansmith
benallansmith

Reputation: 816

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

Upvotes: 4

Joseph Bani
Joseph Bani

Reputation: 684

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

Upvotes: 2

M. Chavoshi
M. Chavoshi

Reputation: 1021

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

Upvotes: 6

adithya reddy
adithya reddy

Reputation: 451

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.

Upvotes: 0

wdanxna
wdanxna

Reputation: 10949

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.

Upvotes: 1

Johan Lund
Johan Lund

Reputation: 133

I had this problem in Android Studio 2.3.

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

Upvotes: 5

Eltaf Hussain
Eltaf Hussain

Reputation: 3401

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.

Upvotes: 1

Chetan Joshi
Chetan Joshi

Reputation: 5711

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)

Upvotes: 2

Ahmed Esameddeen
Ahmed Esameddeen

Reputation: 89

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

Upvotes: 0

Related Questions