233

I want to do some basic functions. I'm running/building it in command line. I am referring User guide. And, doing some simple tasks.

When I start to run simple tasks, it was build successfully. But, After some times, it is showing "It is currently in use by another Gradle instance" and becomes BUILD FAILED. I'm using a terminal for run the task. I didn't run anything at that time.

If I closed the terminal which generate the error, this problem would be solved. But, I know that it is not the solution for that.

Why is this error coming frequently?

How to solve it?

And, Please refer any link to improve my knowledge on Gradle Build system in Android.

starball
  • 20,030
  • 7
  • 43
  • 238
Dhasneem
  • 4,037
  • 4
  • 33
  • 47
  • 15
    Do you use gradle in _daemon mode_ by any chance? You can check by running command `gradle --stop`. You would expect to either get output _No Gradle daemons are running_ or _Gradle daemon stopped_. – topr Feb 04 '14 at 10:48
  • @topr I didnt use daemon mode.. – Dhasneem Feb 06 '14 at 06:42
  • 4
    Solution : (That worked for me) Step 1. Start task manager find/ verify that there are NOT TWO instances of java.exe running. Step 2. Just kill any one java.exe or both and start Android Studio again. Enjoy – Lakhwinder Singh Dhillon Jan 30 '16 at 10:57

36 Answers36

587

Remove the lock files in the gradle cache by executing something like this:

find ~/.gradle -type f -name "*.lock" -delete
simPod
  • 11,498
  • 17
  • 86
  • 139
n1ghtm4n4g3r
  • 6,045
  • 2
  • 14
  • 15
  • 19
    Thanks! It worked after I additionally ran `find /path-to-project-dir/.gradle -type f -name "*.lock" | while read f; do rm $f; done` – Tyler Jan 05 '17 at 19:11
  • 6
    Today I was back at this question, and this time I needed to `kill -9` a process, in addition to deleting the lockfiles. The message in the question title was immediately followed by the PID: _What went wrong: Timeout waiting to lock task history cache (,,,). It is currently in use by another Gradle instance. Owner PID: 4354 Our PID: 9500_ – Joshua Goldberg May 24 '17 at 03:56
  • @JoshuaGoldberg Did you check how and when process#4354 was started before killing it? (I am just curious.) – n1ghtm4n4g3r May 25 '17 at 08:54
  • I did not, but it may have been left behind somehow during a failed build. (Failed builds are common, and leftover processes are very rare.) – Joshua Goldberg May 25 '17 at 14:38
  • Here is little scriptlet to kill daemons: ps -ef | grep [G]radleDaemon | awk '{ print "Killing gradle daemon with pid " $2; system("kill " $2); }' – n1ghtm4n4g3r May 29 '18 at 09:51
  • 2
    Thanks for sharing that command, I had never had that problem before and after remove the lock files for gradle, I was able to run my server again. This should be marked as "Best answer" – Raul Rivero May 29 '18 at 16:32
  • `find ~/.gradle -type f -name "*.lock" -exec rm {} \;` – nanusdad Aug 09 '18 at 05:55
  • 1
    @shashank-chandak in a bash shell (Terminal or Cygwin) – n1ghtm4n4g3r Sep 10 '18 at 06:44
  • I got this error after deletion of lock files : org.gradle.internal.service.ServiceCreationException: Could not create service of type FileHasher using BuildSessionScopeServices.createFileSnapshotter(). – Nininea Mar 12 '20 at 09:04
  • @Nininea This maybe of help: https://stackoverflow.com/questions/45177977/gradle-could-not-create-service-of-type-filehasher/46094804 ? – n1ghtm4n4g3r Apr 14 '20 at 10:50
  • Now I get an error: **Unable to start the daemon process** – IgorGanapolsky Apr 05 '21 at 22:24
  • Close Android Studio and probably unload Java from memory to remove files. – CoolMind Sep 23 '22 at 09:20
67

Sometimes fast way comes in a non-elegant deal, there we go:

Find first the # of all gradle process running on

ps -A | grep gradle

After that, get the IDs, Kill 'em all one by one

sudo kill -9 <process ID>

Now you're good ;-)

PYK
  • 3,674
  • 29
  • 17
37
gradlew --stop

I had a similar error, running the above command worked for me.

Robin
  • 391
  • 3
  • 7
  • for me, i ran `./gradlew --stop` on another project, yeah i opened 2 android studio project, then the project i want to build worked – user3463521 Aug 15 '23 at 02:44
33

I was stuck with this error and had to blow away the contents of my user's .gradle/caches/ folder and the contents of my project's .gradle/ folder to get going again.

Not sure what caused it, possibly a failed build that kept these files in a locked state?

Ross Hambrick
  • 5,880
  • 2
  • 43
  • 34
  • This problem is noted here -- https://issues.gradle.org/browse/GRADLE-2795 -- While the gradle bug exists, I think this is the best solution. – Joshua Goldberg May 19 '15 at 15:07
  • 4
    Actually I had success just deleting ~/.gradle/caches/* without touching the project's .gradle directory. – Joshua Goldberg May 19 '15 at 15:14
  • Lucky for you @JoshuaGoldberg, but in my case I even cannot delete chaches folder. what could be issue? – Attiq Ur Rehman Jul 24 '15 at 10:52
  • I'd deleted .gradle/caches/** and it works again. Strange thing is that I only use the ./gradlew which shouldn't use the local gradle version right? – Ben Groot Sep 03 '15 at 12:16
  • 1
    @BenGroot AFAIK all gradle daemons with the same gradle version share the same cache (if not explicitly forced otherwise), no matter where they were started from. – ubuntudroid Jun 25 '20 at 12:34
17

For Windows machine:

Open Task Manager(Ctrl-Alt-Delete and check Process)

-> If you run two instance of android studio, Close One (End Task)

-> Then close OpenJDK Platform Binary

-> Gradle sync now on AndroidStudio and it will work now

Yog
  • 817
  • 1
  • 10
  • 19
12

Just kill java.exe process fix my problem.

-- Edit --

if not fixed then:

  • kill OpenJdkPlatform proccess.

if not fixed then:

  • remove all ".lock" file from .gradle directory and sub directories (windows: C:\Users\Username\.gradle).

if not fixed then:

  • rename or delete .gradle directory and try again.
Ahmad Dehnavi
  • 1,483
  • 1
  • 17
  • 20
  • After reading your answer, I deleted the .gradle directory and now after re-compiling the app is completely broken. – red-devil Nov 21 '17 at 19:53
  • After reading your answer, I deleted the .gradle directory and now after re-compiling the app is completely broken. Font sizes have increased, padding has increase, getting random validation errors. What can I do now? – red-devil Nov 21 '17 at 20:01
  • 2
    Are you sure just delete .gradle directory? remove .gradle must doesn't have any side effect to your app code. it is just gradle cache and if you compile your app again. gradle will download necessary files. try clean build. – Ahmad Dehnavi Nov 23 '17 at 09:32
11

That Gradle failure message is usually followed by the relevant process IDs, so it would be

Gradle sync failed: Timeout waiting to lock daemon addresses registry [...].
It is currently in use by another Gradle instance.

Owner PID: 1234
Our PID: 5678
Owner Operation: ...

In order to unlock, just kill the owner process:

sudo kill -9 1234
saschoar
  • 8,150
  • 5
  • 43
  • 46
11

sometimes you need to run this code a few times after each build error

for me it takes two times first i get lock error I run this code then again i get the lock error for another file so I run the code again and at the end it works properly.

find ~/.gradle -type f -name "*.lock" -delete
tahamv
  • 1,121
  • 9
  • 7
10

For MAC machines

Please open the "Activity Monitor" and select the "memory" option and force quiet the Android studio and other running java processes, it will solve this issue.

Vinod Pattanshetti
  • 2,465
  • 3
  • 22
  • 36
8

This may be happening because there is more than just once instance of Android Studio.

Charlie
  • 8,530
  • 2
  • 55
  • 53
Justin
  • 99
  • 1
  • 3
7

On windows . In Android Studio open Terminal window (used to start in project's root folder) and tipe in

 .\gradlew --stop

mine responded:

Welcome to Gradle 7.3.3!

Here are the highlights of this release:
 - Easily declare new test suites in Java projects
 - Support for Java 17
 - Support for Scala 3

For more details see https://docs.gradle.org/7.3.3/release-notes.html

Stopping Daemon(s)
1 Daemon stopped

And now it plays ok with builds and runs as previously

CodeToLife
  • 3,672
  • 2
  • 41
  • 29
6

I could be late but look like quick and efficient solution.

Execute following steps.

  1. Execute ps -ef | grep gradle
  2. Open terminal and move to ~/.gradle/caches path
  3. Execute find ~/.gradle -type f -name "*.lock" | while read f; do rm $f; done
  4. Delete .gradle and .idea folder from Android studio.

It's done.

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151
6

I was facing the same issue in my project but I resolved this by invalidating the project, so follow these steps in the android studio

File -> Invalidate Caches / Restart...

Android studio will restart and your problem should be fixed.

vincenzopalazzo
  • 1,487
  • 2
  • 7
  • 35
5

I had the same issue on mac:

take care, that your project is NOT on an external hard disk. The location of your project must be on the hard disk with your operating system.

(Building an Ionic app)

Sean Stayns
  • 4,082
  • 5
  • 25
  • 35
  • 1
    It seems that in my case, the problem is this too. The other day I transferred the project to an external drive. – DmitryKanunnikoff Jun 15 '20 at 14:06
  • This solved the issue for me as well. On Windows. – Robert May 05 '21 at 07:43
  • I had a problem with the file system of the SSD external disk on the MacBook Pro m2 system, which was ExFAT. The MacBook internal disk worked fine. This seems to be the biggest reason for generating the dreg files on MacOS. – minias Jun 08 '23 at 07:58
5

Having the same issue on a Mac, none of the above worked for me. What worked , though, was opening the Activity Monitor and closing a running java process. I also noticed that an .hprof file was generated, which I deleted. After that, I ran my project and it worked.

3

press CTRL + SHIFT + ESC to open task manager now find OpenJDK and kill it goto where the error is pointing to like

D:\.gradle\daemon\4.10.1\registry.bin.lock

delete the file and resynce and you're good to go.

Steve Moretz
  • 2,758
  • 1
  • 17
  • 31
3

Just delete yours .gradle folder inside android folder

If it is not available in your File Explorer(WINDOWS) or Finder(MAC)

For Windows : go to view option and check show hidden files

For Mac

just press cmd + shift + .

Now run your app.

Rohit Kumar
  • 728
  • 9
  • 13
2

For Windows OS:

I figured it had something to do with a lock file so i simply went to .gradle\buildOutputCleanup in the project folder and deleted the cache.properties.lock file and rebuilt the project. It was working perfectly.

kayleb
  • 19
  • 3
2

Along with the top answers here, you need to figure out why the gradle processes exit without releasing the locks to fix it permanently.

In my case, I did all *.lock file deletions and Android Studio's Invalidate Cache option. But the problem kept coming back.

Running gradlew manually in the terminal showed me that gradle processes prematurely exit when the JVM heap space is exhausted. This answer helped to fix it.

./gradlew build #to detect what crashes gradle processes
./gradlew --stop #to stop any running gradle processes

After fixing the problem (eg: heap exhausted)

find .gradle/ -type f -name "*.lock" -delete #delete lock files
./gradlew build #Try building the project
Dumi Jay
  • 1,057
  • 10
  • 10
1

I was too stuck with the same thing. But it was sorted soon. Just delete the file:

D:\.gradle\daemon\6.1.1\registry.bin.lock

also, I've ended OpenJDK from task manager. Idk it helped or not but both of them solved the problem.

Gyanendra
  • 25
  • 1
  • 6
1

For Mac users on first Gradle sync

I had the same issue when cloning from GitLab using IntelliJ. In fact, as mentioned here : https://stackoverflow.com/a/53269914/2231299 you need to specify an internal hard disk.

So here were the steps in my case:

• Clone locally

• Wait for 1st gradle syncing to perform

• move directory to external hard disk

• reopen project at new location, and then gradle should work fine, even on external hard disk.

Comments: Don't be afraid to re-commit if it looks like the files have some changes. It might just be indentation or line breaks issues, since your external disk might have a different format than your original mac disk.

Myoch
  • 793
  • 1
  • 6
  • 24
0

It was not necessary for me to remove any files to fix this error.

The issue I had is that Gradle instances had hung and were 6 command prompt entries in my task list that were spawned by Java.

Forcing the command prompt instances to close allowed me to continue work as usual.

Knossos
  • 15,802
  • 10
  • 54
  • 91
0

Its Work for me for Problem:-

"Error:Timeout waiting to lock jars It is currently in use by another Gradle instance. Owner PID: 6896 Our PID: 5048 Owner Operation: Our operation: Lock"

Solution:-

  1. close the Android Studio and Open Task manager and end the process java.exe
  2. open the derectory ....gradle\caches.
  3. Delete the jar2.lock file.
  4. Open android Studion and clean the project.
DanielBarbarian
  • 5,093
  • 12
  • 35
  • 44
0

Have tried all this answers, including kill java process, delete .gradle in HOME_DIR and in project dir, restarted Android Studio, and even restarted OS.

It started to work normally only after I launched gradle sync.

B-GangsteR
  • 2,534
  • 22
  • 34
0

Very simple solution:

no need to kill Android Studio , just Kill OpenJdkPlatform process, then delete the .lock file located in:

rootProject/.gradle/<version>/taskHistory
Amir Hossein Ghasemi
  • 20,623
  • 10
  • 57
  • 53
0

While killing the process and/or removing the lock file works, Gradle should be able to handle this on its own, solving the problem permanently. This problem report hints at the solution: Gradle cannot communicate with the gradle service.

Have a look at your local network firewall. In my case,

  • allowing outbound traffic from 127.0.0.1 to 127.0.0.1,

    sudo iptables -A OUTPUT -p udp -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT
    
  • allowing incoming traffic from 127.0.0.1

    sudo iptables -A INPUT -s 127.0.0.1 -p udp -j ACCEPT
    

solved the issue. A discussion of a similar problem at github.

And, Please refer any link to improve my knowledge on Gradle Build system in Android.

The udacity gradle course, if you have some time.

serv-inc
  • 35,772
  • 9
  • 166
  • 188
0

open terminal and move to project path then run depending on the operation system: gradlew clean or ./gradlew clean

Abdo Driowya
  • 129
  • 3
  • 11
0

I had same problem and i did two steps and it became solved.

  1. My JDK was not installed on Mac (i forgot to install) i installed it.
  2. I deleted all items from caches folder located in this path /Users/icon_developer/.gradle/ and rebuilt the project. It worked!
Intsab Haider
  • 3,491
  • 3
  • 23
  • 32
0

For Windows Users:

That Gradle failure message is usually followed by the relevant process IDs, so it would be Gradle sync failed: Timeout waiting to lock daemon addresses registry [...]. It is currently in use by another Gradle instance.

Owner PID: 8080
Our PID: 89765
Owner Operation: ...
In order to unlock, just kill the owner process:

taskkill /PID 8080 /F
taskkill /PID 89765 /F
DIA
  • 9
  • 1
0

In my case the culprit was restrictive firewall, part of the antivirus system.

According to the https://discuss.gradle.org/t/timeout-waiting-to-lock-file-hash-cache/21961/2 and https://github.com/elastic/elasticsearch/issues/50954#issuecomment-576772154, gradle-issue-8750, gradle server is keeping the lock and releases it on request of other gradle process. The communication goes over UDP socket on localhost. Thus if there is a firewall blocking this communication, gradle keeps file locks and other gradle processes cannot continue.

Try disabling AV / firewall to see if problem vanishes. Then run gradle as following to get better insight what is going on.

gradle --debug --no-daemon

The debug log shows owner of the lock, if the problem persists, kill the owner of the lock.

2021-05-20T19:32:41.105+0200 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] The file lock is held by a different Gradle process (pid: 67494, lockId: 628651820260732672). Pinged owner at port 58442
2021-05-20T19:32:42.330+0200 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] The file lock is held by a different Gradle process (pid: 67494, lockId: 628651820260732672). Pinged owner at port 58442

Then kill -9 67494 terminates the owner and releases the lock.

Otherwise after a while gradle terminates with an error (stating here so it is easy to find):

* What went wrong:
Gradle could not start your build.
 > Could not create service of type ResourceSnapshotterCacheService using GradleUserHomeServices.createResourceSnapshotterCacheService().
   > Timeout waiting to lock file hash cache (/Users/myusername/.gradle/caches/6.9/fileHashes). It is currently in use by another Gradle instance.
     Owner PID: 67494
     Our PID: 68313
     Owner Operation: 
     Our operation: 
     Lock file: /Users/myusername/.gradle/caches/6.9/fileHashes/fileHashes.lock
ph4r05
  • 1,826
  • 1
  • 18
  • 15
0

Citing a scenario to @Sean Stayns's answer in case it helps anyone: If you're saving your projects in a Google Drive folder -- don't. Simply moving the project(s) over to the local drive resolves this problem.

In the case we were trying to solve, were were also getting the "It is currently in use by another Gradle instance" error and nothing we tried here worked. The process ID was unknown as well. It didn't immediately occur to me to ask where the project was being saved, so in case anyone new to Android Dev is saving their projects to a Google Drive folder, just move over to your local drive. I would have just left this as a comment in @Sean Stayns's answer, but I can't comment. Hopefully it benefits someone.

0

For Mac Users:

you need to give gradle permission:

sudo gradle
sFritsch09
  • 424
  • 4
  • 10
0

org.gradle.cache.LockTimeoutException: Timeout waiting to lock journal cache (C:\Users\unknown.gradle\caches\journal-1). It is currently in use by another Gradle instance

I was facing the same issue and I fixed it by opening Task Manger and check running instance of "OpenJDK Platform binary" in background processes except Android Stduio just "End Task" this task and work for me

Zahid Iqbal
  • 394
  • 6
  • 12
0

Make your $JAVA_HOME to point to the same JDK version as your Android Studio.

enter image description here

All kudos to Vairavan Srinivasan from Medium

0

With old Flutter project, run with newer version Flutter SDK. You can follow my steps below:

Go to android/build.gradle update kotlin and gradle to new version:

ext.kotlin_version = '1.6.10' >> ext.kotlin_version = '1.7.20'
classpath 'com.android.tools.build:gradle:4.0.1' >> classpath 'com.android.tools.build:gradle:7.1.2'

Next go to android/gradle/wrapper/gradle-wrapper.properties update:

distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-all.zip >> distributionUrl=https://services.gradle.org/distributions/gradle-7.6-all.zip

After

flutter clean
flutter pub get
// remove lock file
rm /Volumes/T7 Shield/FlutterCodeCanyon/flutter-tiktok-triller-clone-short-video-streaming-mobile-app-for-android-ios/flutter_app/android/.gradle/7.6/dependencies-accessors/dependencies-accessors.lock
// stop gradle
cd android
./gradlew --stop
// or
ps -ef | grep gradle
kill -9 123

Finally run Flutter project again


If you run Project in exFAT SSD on MacOS, maybe error below:

Gradle Faild on ExFat SSD

Doan Bui
  • 3,572
  • 25
  • 36
-1

everyone finally, I have complete solution

Error :- Timeout Gradle cashe its running in other gradle


distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

Please add this Line in Wrapper.Propertis and Sync now
After Sync the project When 
Invalidthcast\restart   to get the requesting componants