Android Studio的编译问题“该进程无法访问该文件,因为该文件正在被另一个进程使用." [英] Android Studio problem with compiling "The process cannot access the file because it is being used by another process."

查看:314
本文介绍了Android Studio的编译问题“该进程无法访问该文件,因为该文件正在被另一个进程使用."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道之前曾有人问过这个问题,但是没有一个令人满意的答案,而上一帖是三年前的.我在Windows 10中使用android studio 3.6.2.每次尝试编译程序时,都会收到错误消息该进程无法访问该文件,因为该文件正在被另一个进程使用."

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."

现在我知道是什么导致了错误.无法删除以前编译的R.jar文件,因为android studio锁定了该文件.它位于\ app \ build \ intermediates \ compile_and_runtime_not_namespaced_r_class_jar \ debug

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

现在要解决这个问题,我一直在关闭android studio.然后删除R.jar,然后重新启动android studio,然后进行编译.此过程需要花费几分钟,因此每次都必须这样做.想象一下,每次必须要做一个程序时,要花多长时间.

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.

当在堆栈溢出中被问到这个问题时,以前的建议是无效的缓存/重启,但这并不比我上面做的要快,只能工作一次.也尝试过Clean Project,但这没有效果.

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.

这些年来,我已经使用了许多编译器,例如Visual Studio,代码块等,但是这种事情以前从未发生过,那么android studio为什么这么糟糕?有人找到解决这个问题的方法了吗?

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?

推荐答案

我在Windows 10的Android Studio 4.x中也遇到了相同的问题.对我不起作用.有时,删除 build 文件夹会起作用,但是我不能多次构建该项目.这是我的解决方案:

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:

具有多个守护程序可能会导致问题,如 Gradle 中的文档所述.为了解决这个问题,

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

  1. 在Android Studio中打开您的项目
  2. 找到项目中使用的JDK位置(文件>项目结构> SDK位置> JDK位置)
  3. 打开命令提示符.假设$ ANDROID_JDK为您在步骤2中找到的路径.键入 $ ANDROID_JDK/bin/java.exe --version 找出Java版本.
  4. 键入 java.exe --version 来查找系统使用的默认Java版本.
  5. 比较步骤3和步骤4中的版本信息是否相同.
  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.

使用 app 配置进行构建

空可组合活动模板将创建两个构建配置.您应该将构建配置更改为 app ,如下图所示.这解决了我的问题.

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.

如Bilal Aslam的解决方案所示,我们可以编辑构建配置(运行>编辑配置).选择撰写预览>后,默认预览,您将在启动前面板中看到 Build ,然后是 Gradle-aware Make .删除 Build 应该可以解决问题.副作用尚不清楚.

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.

这篇关于Android Studio的编译问题“该进程无法访问该文件,因为该文件正在被另一个进程使用."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆