Error: Execution failed for task ':app:clean&#

2019-01-08 05:55发布

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

30条回答
Root(大扎)
2楼-- · 2019-01-08 06:09

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

查看更多
太酷不给撩
3楼-- · 2019-01-08 06:09

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.

查看更多
\"骚年 ilove
4楼-- · 2019-01-08 06:10

Try File -> Invalidate Caches and Restart

This worked for me

查看更多
一纸荒年 Trace。
5楼-- · 2019-01-08 06:11

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

查看更多
兄弟一词,经得起流年.
6楼-- · 2019-01-08 06:13

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.

查看更多
太酷不给撩
7楼-- · 2019-01-08 06:13

I had this problem in Android Studio 2.3.

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

查看更多
登录 后发表回答