Android Studio problem with compiling "The process cannot access the file because it is being used by another process."
Asked Answered
J

2

7

I know this question has been asked before, but there wasn't a satisfactory answer, and the last post was three years ago. I am using android studio 3.6.2 in windows 10. Every time I try to compile my program I get the error "The process cannot access the file because it is being used by another process."

Now I know what is causing the error. The R.jar file that was previously compiled cannot be deleted because android studio is locking the file. It is in a folder \app\build\intermediates\compile_and_runtime_not_namespaced_r_class_jar\debug

Now to get around the problem I have been closing down android studio. Then deleting the R.jar, then restarting android studio and then compiling. This process takes a good few minutes and I have to do it every time. Imagine how long it takes to write a program when you have to do this every time.

When this was asked on stack overflow previously suggestions were to Invalid caches / restart, but that is no quicker than what I did above, and only works once. Also tried Clean Project, but that has no effect.

I have used many compilers over the years like visual studio, codeblocks etc but this kind of thing never happened before, so why is android studio so bad? Has anyone found a solution to this problem yet?

Journalist answered 17/4, 2020 at 14:29 Comment(5)
So you can't run gradle commands like clean?Convoy
No idea how to run a gradle command. I just click the green triangle at the top of Android Studio to compileJournalist
Try building and deploying your app via the commandline with gradle. I'm not entirely sure how to execute the gradle wrapper on windows, but on Linux you'd run ./gradlew clean or ./gradlew build etc. from the project directory. It will probably look similar.Convoy
related here and hereHaugh
please check #60490568Undeceive
Q
3

[Remove Build][Go to Edit Configurations]

Go to Edit Configurations check that you are not building before each launch. Remove "Build"

Queri answered 2/12, 2020 at 18:41 Comment(0)
N
1

I also encountered the same problem in Android Studio 4.x in Windows 10. Simply using "Restart and Invalidate" does not work for me. Sometimes, deleting the build folder will work but I cannot build the project more than once. Here is my solution:

Check Java Runtime Version Used by Gradle and System Default

Having multiple daemons may cause problems as described in documentation from Gradle. To solve this problem,

  1. Open your project in Android Studio
  2. Find the JDK location used in your project (File > Project Structure > SDK Location > JDK Location)
  3. Open a command prompt. Let $ANDROID_JDK be the path you found in Step 2. Type $ANDROID_JDK/bin/java.exe --version to find out the Java version.
  4. Type java.exe --version to find out the default Java version used by the system.
  5. Compare if the version information from Step 3 and Step 4 are the same.

Build with app Configuration

Empty Composable Activity template will create two build configurations. You should change the build configuration to app as shown in the figure below. This solves my problem.

build configuration

Proper Launching Procedure

As illustrated in Bilal Aslam's solution, we can edit the build configurations (Run > Edit Configurations). After selecting Compose Preview > Default Preview, you will see Build then Gradle-aware Make in the Before launch panel. Remove Build should be able to solve the problem. The side-effects are unclear.

Nanceenancey answered 25/12, 2020 at 7:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.