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:
gradle
and .gradle
directories in the project directory;.gradle
directory in my user directory;gradlew clean
from the AS terminal;Things that I've tried with a little success, but only let me perform one more clean and rebuild before the error occurs again:
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.
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.
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
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.