Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get “file is used by another process” errors when I run the project within Android Studio 3.6.1?

Tags:

android

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!

like image 940
Alex Crisan Avatar asked Mar 05 '20 14:03

Alex Crisan


2 Answers

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.

like image 104
lordmegamax Avatar answered Oct 07 '22 20:10

lordmegamax


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.

like image 4
PigHearted0xD9B4BEF9 Avatar answered Oct 07 '22 20:10

PigHearted0xD9B4BEF9