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

后端 未结 11 1574
伪装坚强ぢ
伪装坚强ぢ 2020-12-16 10:12

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 sampl

11条回答
  •  误落风尘
    2020-12-16 10:30

    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.(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
    }
    

提交回复
热议问题