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.
Any ideas?
Thanks in advance.
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
For me, this happened because of an active debugging process. So, before to clean or rebuild, make sure to kill all active processes. To nail down the success execute Invalidate Cashes/Restart.
Cleaning the project in Android studio and running again fixed the issue. May be do "Make Project" as well.
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)
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.Clean project from Terminal using this command 'gradlew clean'.
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.