[android] 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:

  • 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

The answer is


I solved with command:

taskkill /F /IM java.exe

and then:

gradle assembleDebug 

try to remove these files manually

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


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


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)


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

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".


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.


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

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.


I had this problem in Android Studio 2.3.

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


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.


Deleting the directory intermediates is a quick fix for problem.

The directory will be rebuilt when the project is rebuilt.


Clean project from Terminal using this command gradlew clean.

enter image description here


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


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.


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


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


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


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.


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.


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.


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


Try File -> Invalidate Caches and Restart

This worked for me


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

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.


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


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.


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


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.


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.


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


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.


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


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.


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


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.


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.


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.


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


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.


Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to android-studio

A failure occurred while executing com.android.build.gradle.internal.tasks "Failed to install the following Android SDK packages as some licences have not been accepted" error Android Gradle 5.0 Update:Cause: org.jetbrains.plugins.gradle.tooling.util This version of Android Studio cannot open this project, please retry with Android Studio 3.4 or newer WARNING: API 'variant.getJavaCompile()' is obsolete and has been replaced with 'variant.getJavaCompileProvider()' Flutter plugin not installed error;. When running flutter doctor ADB.exe is obsolete and has serious performance problems Android design support library for API 28 (P) not working Flutter command not found How to find the path of Flutter SDK

Examples related to android-gradle-plugin

WARNING: API 'variant.getJavaCompile()' is obsolete and has been replaced with 'variant.getJavaCompileProvider()' Android Material and appcompat Manifest merger failed Unable to resolve dependency for ':app@debug/compileClasspath': Could not resolve Failed to resolve: com.google.firebase:firebase-core:16.0.1 com.google.android.gms:play-services-measurement-base is being requested by various other libraries Invoke-customs are only supported starting with android 0 --min-api 26 error: resource android:attr/fontVariationSettings not found Exception : AAPT2 error: check logs for details Could not resolve com.android.support:appcompat-v7:26.1.0 in Android Studio new project Failed to resolve: com.android.support:appcompat-v7:27.+ (Dependency Error)

Examples related to kotlin

No Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator How to allow all Network connection types HTTP and HTTPS in Android (9) Pie? Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0 Default interface methods are only supported starting with Android N Error : Program type already present: android.support.design.widget.CoordinatorLayout$Behavior Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6 startForeground fail after upgrade to Android 8.1 How to get current local date and time in Kotlin How to add an item to an ArrayList in Kotlin? HTTP Request in Kotlin

Examples related to kotlin-android-extensions

How to add an item to an ArrayList in Kotlin? Error: Execution failed for task ':app:clean'. Unable to delete file