Android Studio 3.1“ Run”没有编译代码

当我尝试安装一个应用程序时,有两种可能性:

  1. 运行 app时,从生成文件夹安装现有 APK
  2. 执行 clean build后安装 APK

对于构建文件夹中现有的 APK,应用程序工作正常。但是,当我使用 Build-> Clean Project清理项目,然后尝试运行 app(即将应用程序安装到我的仿真器或物理设备上) ,它显示我的错误:

磁盘上不存在 APK 文件/Users/MyApplicationName/app/build/output/APK/app-debug. APK。

注意: 这种行为只发生在我清理项目时,而不是当我已经有一个预先构建的应用程序 APK 在我的构建文件夹

我曾经提到过: 磁盘上不存在 APK 文件但是我的观点是,当我们通常在清理项目之后运行应用程序时,我们从来不需要构建它,如果 APK 不存在于构建文件夹中,它会自动生成并安装最新的一个。

我尝试过的事情:

  1. 当构建文件夹中存在 APK 文件时运行应用程序(工作完全正常)
  2. 运行 Clean Project-> 运行应用程序(预计项目将被构建,应用程序将被安装,但它显示上述错误!)
  3. 同样的过程清洁和运行后,做 Invalidate Caches/Restart的应用程序
23073 次浏览

clean your project and run again

If it's not work then

  • Step 1 Close your project.
  • Step 2 Go to your project folder and delete all build folder.
  • Step 3 run your project.

The APK file /Users/MyApplicationName/app/build/outputs/apk/app-debug.apk does not exist on disk.

May be bug. You should change Settings.

You should open the Run/Debug Configurations dialog & select Run > Edit Configurations

Make sure, Gradle-aware Make is Added in TaskList or not. If not then click + & select from the options.

enter image description here

Note

If you receive

Shutdown finished in 0ms Error while generating dependencies split APK com.android.ide.common.process.ProcessException: Failed to execute aapt at com.android.builder.core.AndroidBuilder.processResources(AndroidBuilder.java:809) at com.android.builder.core.AndroidBuilder.processResources(AndroidBuilder.java:797) at com.android.build.gradle.internal.transforms.InstantRunSplitApkBuilder.generateSplitApkResourcesAp(InstantRunSplitApkBuilder.java:373)

You should Un-Check INSTANT RUN.

File-- Settings-- Build, Execution, Deployment -- Instant Run and uncheck Enable Instant Run.

I have solution for your issue.

Also confirmed by Android Studio on Twitter : https://twitter.com/androidstudio/status/981914632892960768

  1. Edit your app configuration as below.

enter image description here

  1. Here you can see your app configuration as below.

enter image description here

  1. Here is missing Gradle-aware make attribute in before launch configuration. You can see here.

enter image description here

  1. Please add this Gradle-aware Make attribute through this way. Click on + icon and select Gradle-aware Make as seen in this screen.

enter image description here

  1. You can add this without writing any task just press OK button and task will be added and now it should look like this. Now apply changes and run your application.

enter image description here

It will solve this old apk installing issue on clean build in new Android Studio 3.1 issue.

Note : This issue is resolved in new Android Studio 3.1.1 Stable release.

This issue has been fixed with Android studio 3.1.1 (April 2018) release. Update your android studio to 3.1.1.

Here is there explanation regrading the issue details, cause and solution:

In some cases, when a project created in Android Studio 3.0 was opened for the first time in Android Studio 3.1, the Gradle-aware Make task was removed from the Before launch area in Run/Debug Configurations. The result was that projects did not build when the Run or Debug button was clicked, which in turn caused failures such as deployment of incorrect APKs and crashes when using Instant Run.

To solve this problem, Android Studio 3.1.1 adds the Gradle-aware Make task to the run configuration for projects that are missing this entry. This modification occurs after the first Gradle sync when the project is loaded.

Official release notes: https://developer.android.com/studio/releases/index.html#3-1-0

For all people seing this and having the issue on Android Studio +4.0.0:

Run -> Edit configurations... -> disable check box (Allow parallel run)

It may slow your build a little but it better than running the app more than one time.