Bruce E
Bruce E

Reputation: 701

Android Studio I keep getting Gradle error "The process cannot access the file because it is being used by another process"

I can import a sample app project, try to build it and I keep getting this error. Not always the same file but a new temp file each time. I've tried several different samples. I've been programming for 30+ years and feel completely clueless. Only clue seems to be the Windows Task Manager shows 99% to 100% CPU usage when it gives the error and ends the build.

Upvotes: 70

Views: 70064

Answers (21)

Sadique Khan
Sadique Khan

Reputation: 320

Close file explorer by clicking on close window option and then run the program

Upvotes: 0

gianpaolo
gianpaolo

Reputation: 903

When this happens to me on Windows 8, I open the task manager

task manager

and simply kill all the instances of "openjdk platform binary"

edit Feb 2024: from powershell I now use this command to kill openjdk (java):

 Get-Process -Name "java" -ErrorAction SilentlyContinue | Stop-Process -Force

Upvotes: 45

C.F.G
C.F.G

Reputation: 1463

If you are using External Emulator:

Restarting Android Studio with cache invalidation doesn't helped.

The file that caused the problem is being used by the emulator and if you are using an external emulator (BlueStacks) like me, restarting Android Studio will not help. I restarted the emulator and the problem went away.

Upvotes: 0

Cedric
Cedric

Reputation: 195

In my case, deleting the /.gradle/ directory and rebuilding resolved the issue.

Upvotes: 0

Umair Mustafa
Umair Mustafa

Reputation: 218

Above suggested solutions may fixed this error but they are time consuming so the best solution to fix this problem is open command prompt and run the following command.

taskkill /im java.exe /f

screenshot of command prompt

Upvotes: 1

Astro
Astro

Reputation: 564

Terminating "OpenJDK platform binary" from Task Manager then deleting src/build and finally rebuilding the project should make it work.

Upvotes: 1

Daniel
Daniel

Reputation: 37051

Had the same on Windows 10, it was resolved by closing all "Command Prompt" (cmd) windows 🤷‍♂️

Upvotes: 0

Ryan Chen
Ryan Chen

Reputation: 224

enter image description here

Choose app instead of DefaultPreview. Now if you build, then you should not see this issue.

Upvotes: 8

Koen van der Marel
Koen van der Marel

Reputation: 208

For those which are still searching for this answer in 2022, remove the build folder and rebuild...

Upvotes: 0

Braian Coronel
Braian Coronel

Reputation: 22867

Invalidating the IDE cache is NOT enough.

  1. To delete the folder app\build we should first stop locking process - OpenJDK Platform binary.
  2. Delete the folder app\build
  3. Invalidate Caches / Restart...

Context: Using Coroutines in Android Instrumented Test.

GL

Upvotes: 4

CoolMind
CoolMind

Reputation: 28773

Restart of Android Studio with cache invalidation doesn't help. As others said, we should delete a <module>\build folder. Because a file is locked by OpenJDK Platform binary, we should stop this process in Task Manager (or unlock the file via LockHinter).

enter image description here

enter image description here

Upvotes: 2

Kobi
Kobi

Reputation: 455

try to delete the file (the folder inside builder message) the message will contain process name ( i dont remember the name) find the prcess in task manager and stop it

Upvotes: 1

L&#234; Quốc Anh
L&#234; Quốc Anh

Reputation: 41

Kill task "openjdk platform binary" works for me

Upvotes: 2

squirrel
squirrel

Reputation: 5488

If the error is recurring, you can try a file leak detector to determine what part of the gradle process is holding your files. As suggested here,

  1. Download file leak detector

  2. Check with resmon what process is holding on to the file

  3. Depending on where the file is locked, do the following:

    • Android Studio: In studio, go to HelpEdit custom VM options and add the following line:

      -javaagent:path/to/file-leak-detector.jar=http=19999
      
    • Gradle: Add to your gradle.properties file the following line:

      org.gradle.jvmargs=-javaagent:path/to/file-leak-detector.jar=http=19999
      
  4. When the problem occurs again, open localhost:19999 in your browser and search for locked files.


In my case, I found out that a file was held by AspectJ compiler/weaver (ajc):

#115 C:\Users\me\StudioProjects\myapp\app\build\intermediates\compile_and_runtime_not_namespaced_r_class_jar\debug\R.jar by thread:Execution worker for ':' Thread 3 on Mon Oct 19 18:41:06 BST 2020
    at java.util.zip.ZipFile.<init>(ZipFile.java:156)
    ...
    at org.aspectj.tools.ajc.Main$run.call(Unknown Source)

I was running it directly from Gradle:

new Main().run(args, handler)

I don't know if it's possible to make ajc release the files. So I resolved my issue by running it in a separate process. I added a new configuration to get aspectjtools onto classpath:

val weaving: Configuration by configurations.creating

dependencies {
    weaving("org.aspectj:aspectjtools:1.9.6")
}

And instead of the above code, I did:

javaexec {
    classpath = weaving
    main = "org.aspectj.tools.ajc.Main"
    args = arguments
}

Upvotes: 1

nvasilescu
nvasilescu

Reputation: 124

I had 2 jre's installed when I installed android studio. Uninstall the one that you find from control panel and that is located something like c:\program files\java etc.

On the next opening of android studio 4.0 + it will fallback to "C:\Program Files\Android\Android Studio\jre" and the process lockdown after each succesfull build will stop

Upvotes: 0

Quote Spk
Quote Spk

Reputation: 9

It happened me many times while building or Cleaning the Project The solution is simple:

  1. Open the Specific Directory
  2. Close the Android Studio
  3. Delete the particular Directory
  4. Now run the Android Studio

Upvotes: -1

i.am.jabi
i.am.jabi

Reputation: 678

Invalidate Caches / Restart doesn't work all the time and it's not the better idea to close the whole Android Studio just we couldn't figure out what process was using it.

Please follow the below steps to figure out which process is using your file.

You can use the Resource Monitor for this which comes built-in with Windows 7, 8, and 10.

  1. Open Resource Monitor, which can be found By searching for Resource Monitor or resmon.exe in the start menu, or As a button on the Performance tab in your Task Manager
  2. Go to the CPU tab
  3. Use the search field in the Associated Handles section

When you've found the handle, you can identify the process by looking at the Image and/or PID column.

You can then try to close the application as you normally would, or, if that's not possible, just right-click the handle and kill the process directly from there.

Ref : https://superuser.com/questions/117902/find-out-which-process-is-locking-a-file-or-folder-in-windows

Upvotes: 1

Shibi G.krishnan
Shibi G.krishnan

Reputation: 111

I had the same problem and in my case it is due to multiple Java Run times (JRE's). Try switching between JRE's in android studio or use only one Java runtime.

Hope it fix the problem. Happy Coding..

enter image description here

Upvotes: 1

Saddan
Saddan

Reputation: 1655

I have the same issue's.Even I tried all the solution mentioned here it didn't work.Even I can't delete the signin file as running android studio administrative mode.I see a notification at bottom right side of the IDE update gradle pluggin.So update gradle pluggin usually worked for meenter image description here

Upvotes: 2

shayekh
shayekh

Reputation: 99

Just clean the project and have a run ! It would work ....

Upvotes: 9

saigopi.me
saigopi.me

Reputation: 14908

Do This

File -> Invalidate Caches / Restart.. -> Invalidate/ Restart

Upvotes: 73

Related Questions