错误:任务 ':app:clean' 的执行失败.无法删除文件

时间:2023-02-02
本文介绍了错误:任务 ':app:clean' 的执行失败.无法删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试重建我的 Android Studio Gradle 项目(主要包含 Kotlin 代码),但它在清理/重建过程中开始抛出 UnableToDeleteFileException:

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:UsersUserKotlinGameEngineappuildintermediatesexploded-aarcom.android.supportappcompat-v723.0.1jarsclasses.jar

这在我尝试更改项目的包结构后开始发生.不幸的是,我通过重命名和移动源文件夹而不是通过 Android Studio 重构来做到这一点,这是一个坏主意.

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 同步;
  • 重新安装 Java JRE 和 Java SDK;
  • 重新安装最新版本的 Android Studio (1.4);
  • 回滚到之前的 AS 版本 (1.3);
  • 使AS缓存失效并重启;
  • 删除项目目录下的gradle.gradle目录;
  • 删除我的用户目录下的.gradle目录;
  • 从 AS 终端运行 gradlew clean
  • 手动将源代码复制到新项目中(奇怪的是它会以某种方式跨项目持续存在...)

我尝试了一些成功的事情,但只让我在错误再次发生之前再执行一次清理和重建:

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

  • 关闭AS,手动删除构建文件,再次打开;
  • 在 AS 运行时杀死 java.exe 进程(从技术上讲,每次都可以这样做,但它很乏味并且会减慢构建过程)
  • 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)

所以看起来编译 Java 进程可能会出于某种原因锁定构建文件,但它也可能与 Kotlin 有关.我有一个(更成熟的)Java Android 项目,我正在处理它,但在清理它时我无法重现此错误.这似乎只发生在我的 Kotlin 项目中.

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.

我发现问题是由 Kotlin Android 插件引起的.当我从模块的 build.gradle 文件中删除 apply plugin: 'kotlin-android' 并在我重新插入时又回来了,问题就消失了.随时提供对此的任何见解.

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.

最后一次更新不是原因.我发现如果一个项目包含一个 Kotlin 文件,那么重建和清理就会失败.即使删除了所有 Kotlin 文件,它也会继续失败,直到后台 Java 进程被杀死,这意味着它对构建文件有某种锁定.我在这里提交了一个错误,其中包含更多详细信息和重现错误的步骤:KT-9440

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

推荐答案

试试文件 -> 使缓存无效并重新启动

Try File -> Invalidate Caches and Restart

这对我有用

这篇关于错误:任务 ':app:clean' 的执行失败.无法删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:是否可以在 android gradle 中将 git 存储库声明为依赖项? 下一篇:如何更改每个 Gradle 构建类型的应用程序名称

相关文章

最新文章