Gradle build fails and prompts .lock file, solution

After Gradle build fails, AndroidStudio is sometimes forced to close. When the build is opened again, various .lock file problems will be prompted. If one is deleted, there will be another one, and the path is different.
Insert image description here

Under normal circumstances, it is the lockfiles in these two folders that affect the continued build.

  • Under %GRADLE_HOME%/caches (the location where gradle is installed, the library you downloaded will generally be retained)
  • Under the project directory (files generated by the project build process, cache, etc.)
    • /.gradle/
    • /.buildCahceDir/ under

General solution

  1. Clear all .lock files; according to my experience, there may be dozens or hundreds of them, and they need to be deleted one by one. Of course, for gradle, you can directly delete the caches folder, but you will find that building again will re-download the jar package and other dependent files.
  2. Sync the project and build again. If you delete the caches folder in the previous step, all dependencies will be downloaded here, which will take a long time. If you deleted the lockfiles one by one in the previous step, it would take a lot of time. If you realize it for yourself, it is better to delete the caches directly.

Targeted deletion of lockfile solution

Now that we know that deleting lockfiles can solve this problem, is there any way to directly delete these lockfiles? Of course there is! The solution to deleting the lock is mainly to find the lock file which is quite laborious, so I can just write a script to help me find and delete it.

Used to clean up the lock file generated after gradle build fails.

clean_lock.sh

#!/bin/bash
removeFiles() {
    
    
    if test -d "$1"
    then
        echo "开始清理"
        removeFileInDirectory $1
        echo "清理完成"
    else
        echo "输入目录不存在"
    fi

}

removeFileInDirectory() {
    
    
    find $1 -name "*.lock" -print  -type f -exec rm -f {
    
    } \;
}

removeFiles $1 

After writing the above code, save it as clean_lock.sh and then execute the command. Make your command executable: Make your .sh script file executable.

The command is as follows:

./clean_lock.sh [path]     

When using it, replace path with the path you want to delete:

删除gradle缓存汇总的lock文件
./clean_lock.sh /User/me/Desktop/gradle_home/caches/     
删除项目中的lock文件
./clean_lock.sh /User/me/Desktop/my_project/  

Click here to download directly

Guess you like

Origin blog.csdn.net/yztbydh/article/details/131980910