Unlocking the Power of Gradle

Clearing Lock Files from Gradles Caches

Tobias Wissmueller
2 min readFeb 6

--

Photo by JESHOOTS.COM on Unsplash

Here is a quick one that might help you after a Gradle build that, for some reason, got interrupted.

Gradle builds use locks to ensure that only one process accesses a particular resource at a time. Interrupting a Gradle build can result in lock files being left behind, which prevent subsequent builds from accessing the resources that were being used at the time of interruption.

Now, if the build process gets interrupted, the process may not have had a chance to clean up after itself before it was interrupted, leaving lock files in place that prevent other builds from accessing the resources. Therefore, you won’t be able to run another build.

My Android Studio for example needs a minute or so to detect the first lock file. After I have deleted it by hand, I restart the build and it will take a long while after it reports the existence of yet another lock file. After removing that lock file, the process repeats and repeats until all lock files have been removed.

I am assuming you are only trying to build one project at a time.

There are two directories that needs to be cleared from the lock files.

  • There is a .gradle-directory in your home directory
  • and another one in your projects directory.

The following command cleans the one from your home-directory.

find ~/.gradle/ -name "*.lock" | xargs rm

The next command is basically the same, just the directory has been changed to the one with the projects .gradle-directory.

find /Path/To/MyProject/.gradle -name "*.lock" | xargs rm

After that, you can trigger your gradle-build again and it will run through with its normal speed.

Hope that helps!

Thank you for reading!

--

--

Tobias Wissmueller

Documenting my Tech-Stack: Android with Jetpack Compose and Kotlin, iOS with SwiftUI and some backend topics here and there.