本文介绍了当 Gradle 具有更改 APK 名称的自定义逻辑时,Android Studio 将无菌 APK 上传到设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 Android Studio 时,我遇到了以下错误:

While using Android Studio I encountered the following error:

APK 的日期比当前日期早 2 天(Gradle 用于将构建日期附加到 APK 名称).我检查了文件夹 \build\outputs\apk,确实生成了具有正确日期的 APK:

The date of the APK is 2 days older than the current one (Gradle is used to append the build date to APK name). I've checked folder \build\outputs\apk, and an APK with the correct date has indeed been generated:

已尝试以下方法:

  • 构建 -> 清理项目
  • 构建 -> 重建项目
  • 文件 -> 使缓存无效/重新启动...

如何解决此问题,以便将正确的 APK 上传到设备以运行?

How do I fix this so the correct APK can be uploaded to device to run?

更改了标题以更好地反映发生此问题的情况.似乎 Android Studio 在与 Android Studio 同步时会记住 Gradle 提供的 APK 名称,但在运行 Gradle 以创建实际文件时不会更新 APK 名称.

Changed the title to better reflect the circumstances where this issue would occur. It appears that Android Studio would remember the APK names provided by Gradle when it's been synced with Android Studio, but does not update the APK names when Gradle is ran to create the actual files.

在我的情况下,这是由当前日期附加到 APK 名称这一事实引起的,所以如果我在 12/3 Android Studio 上将 Gradle 同步到 Android Studio 会记住 APK 文件名是 app_2014-12-03.apk).然后使用 Gradle 构建并在 12/4 上运行应用程序会导致 Android Studio 和 Gralde 之间断开连接,其中 Gralde 生成了 app_2014-12-04.apk 和 Android Studio还记得 APK 文件名是 app_2014-12-03.apk.

In my situation it's caused by the fact that the current date is appended to the APK name, so if I synced Gradle to Android Studio on 12/3 Android Studio would remember the APK filename to be app_2014-12-03.apk). Then using Gradle build and run the app on 12/4 would cause the disconnection between Android Studio and Gralde, where Gralde generated app_2014-12-04.apk and yet Android Studio still remembered the APK filename to be app_2014-12-03.apk.

推荐答案

Gradle 和 Android Studio 之间的同步问题.按 Gradle Sync 图标(它是 Project Structure 和 AVD Manager 之间的图标),它会解决问题.

It is a problem with sync between Gradle and Android Studio. Press Gradle Sync icon (it is icon between Project Structure and AVD Manager) and it will solve the issue.

(在您要求解释的评论中 - 我在几周前找到了这个答案,当时我也遇到了这个问题[但现在我找不到这个页面].如果我没记错的话是信息,在同步 Gradle 期间通知 AS关于不同的信息,包括有关 apk 路径的信息.在您的情况下[和我过去的情况] 路径每天都在变化,因为我们在 apk 名称中有日期,因此我们必须进行同步)

(In comments you asked for explanation - I found this answer few weeks ago, when I had also issue with this[but now I cannot find this page]. If I remember correctly was information, that during Sync Gradle is informing AS about different information including info about apk path. In your case [and mine in past] path is changing every day, as we have date in name of apk and due to this we have to make Sync)

这篇关于当 Gradle 具有更改 APK 名称的自定义逻辑时,Android Studio 将无菌 APK 上传到设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 11:04