9

I encounter this error after running the second time the project.

...\app\build\intermediates\compile_and_runtime_not_namespaced_r_class_jar\debug\R.jar: The process cannot access the file because it is being used by another process

So far Clean project works once, restart the Android Studio works once and the last resort is to restart the pc(running windows 10).

Open to suggestions!

Alex Crisan
  • 93
  • 1
  • 6
  • 1
    I am also facing the same issue. I close the studio and delete the file R.jar from the path and restarting the studio. I din't find any perfect solution for this. Big headache is my project usally takes 5 mints to complete gradle sync so deleting file and restarting the studio takes 10 mints and I am facing this issue 2-3 times per day. – Gevaria Purva Mar 25 '20 at 12:22
  • related [here](https://stackoverflow.com/questions/60490068/android-studio-3-6-1-build-issue) and [here](https://stackoverflow.com/questions/61274052/android-studio-problem-with-compiling-the-process-cannot-access-the-file-becaus) – Richard Le Mesurier May 06 '20 at 13:44
  • please check https://stackoverflow.com/questions/60490068/android-studio-3-6-1-build-issue – ashish May 07 '20 at 03:50

2 Answers2

18

Some workarounds can be found here Error: Execution failed for task ':app:clean'. Unable to delete file

When this happens, I use taskkill /im java.exe /f in cmd (windows 10). It kills all java processes and releases that file. This is not the best solution, but it's way faster to click on .bat file instead of invalidating the whole cache|restart|clean|etc.

I switched from OpenJDK to Oracle JDK, not sure if this will work with OpenJDK.


Another workaround is to stop using gradle's daemon: https://docs.gradle.org/current/userguide/gradle_daemon.html

Disabling the Daemon

You can disable the Daemon for a specific build using the --no-daemon argument, or disable it for a specific project by explicitly setting org.gradle.daemon=false in the project's gradle.properties file.

lordmegamax
  • 2,684
  • 2
  • 26
  • 29
4

Manually delete the R.jar file in the file explorer then press rebuild.

The system (Android Studio Gradle or another debug process) might be using it automatically even when you haven't asked it to build and the (automatic) process blocks your second build process from using it.

Let me know if this works, apparently, Android Studio 4.0.1 has similar problems.

AgentP
  • 6,261
  • 2
  • 31
  • 52