Search code examples
javaandroidandroid-studioandroid-gradle-pluginandroid-build

"Could not delete path" on various folders under "build/generated/source/r"


For the last few months, I have been getting a sporadic, moving, build-time error “Could not delete path” for a folder in the “build/generated/source/r” folder (the exact folder changes). For example:

enter image description here

If I look at the build folder, I can see that

  • Offending folder is present
  • Offending folder is the first folder under the "r" folder
  • Offending folder is empty
  • Subsequent folders are present, and each contains 1 file: the R.file

For example, if the offending folder is “localbroadcastmanager”:

enter image description here

If I try to build again (Build, Make Project), it will successfully delete the offending folder, and several subsequent folders. And then it will (often) encounter the problem for another folder “later” in the file tree.

If I keep trying to build, it will eventually succeed, sometimes after failing ~20 times.

If I manually delete the “build” folder (either in Android Studio, or via File Explorer) the next build process completes without error. Unfortunately, that is a lot of overhead to incur for each build iteration.

My theory is that the gradle build task is recursively deleting the structure under “source/r”. For each folder, the contents of the folder (R.java) are deleted, and then the folder is deleted. Sometimes the folder deletion is initiated before the file deletion has completed, resulting in a lock on the folder.

System details

  • Windows 10
  • Android Studio 3.1.4
  • Anti-virus: Trend Micro Internet Security 12.0

Things I've tried

  • Android Studio
    • Build, Clean Project
    • Invalidate Caches/Restart
    • Delete "build" folder (works, but is not a tenable solution)
  • Anti-virus (Trend Micro)
    • Added "build" folder to "Exception List"
    • Disabled anti-virus
  • Used Resource Monitor and Process Explorer to see if other process has lock on folder
  • Tried suggestions posted here: StackOverflow post: Could not delete path

Solution

  • I figured it out. I use a backup tool (Genie Timeline Home 2017). The tool works by continually monitoring the files specified in the save set, and periodically (~every 6 hours) making a snapshot of the recent changes.

    Apparently there was some kind of intermittent synchronization issue with the monitoring, when deleting the contents of a folder, and then deleting the folder. Removing the "build" folder from the save set solved the problem.

    I've been using Genie Timeline for years, and have been having this problem only for the last few months. Not sure what changed.