错误: 在锁文件中发现意外的锁协议。预期3,发现0

在将 Android Studio 升级到1.3之后,我不能再编译我的项目了。我正在接收 Unexpected lock protocol found in lock file. Expected 3, found 0.。我已经尝试删除我的 .gradle文件夹,但这并不能解决问题。还有什么我能做的吗?

109247 次浏览

Go into your local file project and search for the ".gradle" folder

Erase it and restart your IDE

I had the same problem. And deleting the project's .gradle folder didn't fix the problem.

I solved the problem by deleting the C:\Users\<username>\.gradle folder and made studio refresh the entire gradle build. I found I had 4 different versions of gradle installed from my upgrade path.

In android studio, Click on the "Invalidate caches/Restart.." and confirm.

All of these solutions did not work for me on Android Studio 2.1. What worked was me opening gradle-wrapper.properties in project folder and changing my gradle reference on the line below:

distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip

This was changed to:

distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-all.zip

After which gradle upgraded and the app started working!

  1. Get into the .gradle folder in the root of the project.

  2. There, you might see a couple of versions. Pick the gradle version that you're using in the porject.

  3. cd into taskArtifacts folder.

  4. clear the cache.properties.lock file.

Finally, build the project. Hope that helps.

You must erase /.gradle folder from user directory, not from project, it`s a hidden directory, if you used linux-based system it is in /home/user/.gradle and press "alt"+"." to show hidden folders

I have solved it by deleting the folder

c: \ users \ [myusername] \ .gradle \ caches \ 2.14.1

After reopening Android studio it was all fine

None of the above worked for me ....

You just need to go to File -> Project Structure or jus press Ctrl+Alt+Shift+S

and then on the top left click Project Make sure that your Android Plugin Version is the same as you Android Studio Version .. This should fix it

additionally

You can also create a new Project and check under Project view initially you'll be in Android .. change the view to ... Project - > gradle (Note .. not .gradle only gradle ) ->wrapper ->gradle-wrapper properties.. open this file and you can see the last line ie ..

distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip

make sure you have the same gradle version in your old files as well

Check out this Youtube Video i made regarding this issue

https://youtu.be/-Ngn4YvbKtw

None of the above resolutions worked for my instance of this error. I had just started the project, so I really wasn't concerned about the loss of it. I completed the following:

  1. Selected "File\Close Project" and the project closed.
  2. The opening screen to create a new project came up.
  3. I deleted the project from the list just to have it not show in the list.
  4. Created a new project with a slightly different name.
  5. Ran the new project and it worked fine.

From File menu click on Invalidate Cache / Restart. And then Click on Invalidate and Restart. It will fix your problem.

Invalidate Caches

It doesn't work none of above all.

  1. Close the project.
  2. Remove from project list
  3. Re-import the project again. (If you have a whole source file of a project, you should clean previous project and extract the project again)
  4. Open intellij and click open the project.
  5. Select the extracted folder/ project folder.

It is done.

okay here's very lightweight solution with steps:

  • Ctrl+Alt+Shift+S or File->Project Structure

  • Select Project from left panel

  • Now check your Gradle Version at right panel

  • In my case my Gradle Version is 3.3

  • Navigate C:\Users\ashish\.gradle\caches and delete your Gradle Version - 3.3 named folder.

  • Now restart or open your project again from workspace.

Greetings...your problem is solved.

- Go into your local file project and search for the ".gradle" folder,(folder from user directory, not from project, it`s a hidden directory, if you used linux-based system it is in /home/user/.gradle and press "alt"+"." to show hidden folders,)

  • Erase it and restart your IDE

if this doesnt work then, All of these solutions did not work for me on Android Studio 2.1. What worked was me opening gradle-wrapper.properties in project folder and changing my gradle reference on the line below:

distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip

This was changed to:

distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-all.zip

After which gradle upgraded and the app started working!

Open your previous project and open file>ProjectStructure>project check this gradle version,android plugin version of previous project and set it to your current project .and click invalidate/restart

For the persons who have this problem on <Android Studio 3.1.2>. After I got this problem, I done all solutions in this topic. But each of the solutions alone did not solve my problem. So I've combined some strategies.


  1. First I removed application from Android Studio Launcher List.
  2. Second I deleted all the content from .gradle (FOLDER FROM USER DIRECTORY).
  3. Third I import the project again (after sync i got the error again).
  4. Fourth while project is open I used <Invalidate Cache / Restart> option from FILE menu.

I hope this solution work for you too.

We successfully resolved this error.. Go to your project location in my case it was F:=> PROJECT NAME=> .gradle.. and we deleted the the .gradle file.. then restarted android studio and loaded the project again.. and this time the the gradle build successfully.

Delete C:\Users\user\.gradle\caches folder and restart the project.

Removing the .gradle directory from the user directory is not enough. Make sure to delete the .gradle directory from the project directory as well. In fact I would start with the project and only if that isn't enough should you delete at the user level.

Deleting cache folder take long time because it have more than 200k files which is more than 3GB. There is quick view as Ashish mentioned.

I also faced this error in only one app.

Go through the step.

  1. In you project open gradle-wrapper.properties file. Which is located in Gradle Script in Android structure. As shown in below image.

    enter image description here

  2. In that file check distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip. Here gradle version is 4.6.

enter image description here

3.So all cache for that project will be store in your C:\Users\Your PC Name or User Name\.gradle\caches\4.6. As shown in below image.

enter image description here

  1. Close your android studio and delete the 4.6 folder. After deletion open android studio.

That's all. It took mine 6 hours to figure out and deleting cache folder. So I decide to post this answer. So people will not waste same time.

In my case, GRADLE_USER_HOME was not set due to a machine change. After re-setting this env variable, everything works fine again.

You dont need to delete all your gradle files. Your just need to delete C:\Users\(your user name)\.gradle\daemon\(gradle version)\registry.bin.lock and you are done!

In android studio, Click on File and then Click "Invalidate Caches/Restart" and then confirm.

  1. Go to File -> Project structure
  2. Select Project
  3. On the right panel change the Gradle Version

I was on 5.6.4 when this problem occurred and I downgraded it to 5.1.1. Sync the project and it should work after that.

Changing the gradle version from project structure worked for me

  1. File->Project Structure
  2. Select Project from left panel
  3. Now change your Gradle Version at right panel

I found a root cause of this problem for my case under Ubuntu 16.04. Both .gradle and project directories should have proper permissions. I've created this directories by myself and that's why they had more restricted permissions. I just let Android Studio to create both this directories bu itself and everything was working as expected.

None of the solutions here worked for me. For anyone who might run into my situation, here's what worked for me:

In addition to the steps here, I also

  1. Deleted ~/.gradle/daemon/[GRADLE_VERSION]
  2. Deleted ~/.gradle/wrapper/dists/gradle-[GRADLE_VERSION]-all
  3. Deleted [PROJECT_PATH]/.gradle

Restarted Android Studio

This happened after my computer froze and I had to force it to restart. To fix it, I ran this and then everything worked. No Android Studio restart required:

cd path/to/.gradle/
find . -name "*.lock" -delete

I faced the same problem in Android Studio 4.0.1 when my computer froze and I had to force shut down.

What I did to resolve the issue was delete .gradle folder from the project directory and build the project again.

  1. Open the Project pane, if not open already.
  2. Select the Project view (not Android) from the drop-down menu
  3. The first folder (in red colour) will be .gradle, Right Click > Delete.
  4. Build the project again.

enter image description here

My problem was used the same Gradle file in Module Gradle and app Gradle module Gradle file was added with 'api' .

In my case, none of the previous answers worked for me. The solution was to delete the entire gradle folder (not the .gradle folder inside the project) and replaced it with a gradle folder backup, independenly where it's located in the operative system.

I had tried deleting only the cache folder but the problem persisted maybe because I was using a customized gradle folder provided from my job.

I do not recommend this solution in general but if none of the others works, then replace the complete gradle directory or reinstall could be a solution.

If this error occurs while you are trying to run an app on your mobile device via phone USB, make sure the compiled compileSdkVersion and buildToolsVersion are up to date, if they are, just uninstall the app in your phone and run the app again. If the error persists then go-to C:\Users\name.gradle\caches\5.6.4\javaCompile and deletes the javaCompile.lock file then runs the app again. Hope that solves your problem.

I just solved this problem by simply Cleaning the Project and then Rebuilding it.

A Simple Solution

In my case just change distribution URL to latest

Go to gradle-wrapper.properties and change distributionUrl to latest

distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip

And yet not solve this Error just do Clean Project Rebuild Project and Restart Android Studio.

Computer froze while upgrading to Gradle 7.5.1 and I'm using buildSrc

In my case the corrupted lock file was inside buildSrc/.gradle, deleting this folder worked