DEV Community

Cover image for A gradle Task to Save your time
Mahmoud Mabrok Fouad
Mahmoud Mabrok Fouad

Posted on • Edited on

A gradle Task to Save your time

Recently, while running Android Studio I got next error from time to time which waste much time which can be increased based on project size (at least 1 minute, so this amount is multiplied with each build we do):

The process cannot access the file because it is being used by another process.

This can be caused when some plugins run another Java instance so they lock files from one to the other So solution is to kill java process by using:
TASKKILL /F /IM java.exe.

So I searched to make this automated somehow And found we can declare simple gradle task to do this job, it as next( write it at build.gradle)

task killJavaProcess(type:Exec ){
    commandLine 'cmd', '/c', 'TASKKILL /F /IM java.exe'
}
Enter fullscreen mode Exit fullscreen mode

Then to make it be done before each build go to build task at right side of gradle and choose to run before build as next:
image with steps

Have nice coding.

Resources:

gradle Exec Task type

Top comments (0)