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:
If I look at the build folder, I can see that
For example, if the offending folder is “localbroadcastmanager”:
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
Things I've tried
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.