Alex Crisan
Alex Crisan

Reputation: 103

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

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!

Upvotes: 10

Views: 6573

Answers (2)

PigHearted0xD9B4BEF9
PigHearted0xD9B4BEF9

Reputation: 51

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.

Upvotes: 4

lordmegamax
lordmegamax

Reputation: 2694

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.

Upvotes: 18

Related Questions