在名称中使用时间戳时,Android Studio 无法在构建后启动应用程序 [英] Android Studio fails to start app after build when using a timestamp in the name

查看:20
本文介绍了在名称中使用时间戳时,Android Studio 无法在构建后启动应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 gradle 和 Android Studio 时遇到问题,只有在 Android Studio 中构建时才会出现此问题(BuildServer 和 Commandline 工作正常)

I am having an issue with gradle and Android Studio, which only appears when building in Android Studio (BuildServer and Commandline work just fine)

applicationVariants.all { variant ->
            def file = variant.outputFile
            variant.outputFile = new File(file.parent, file.name.replace("app-", getDate() + "_myapp_" + getGitCommit() +"_"));
        }

def getDate() {
        def dateNow = new Date()
        def formattedDate = dateNow.format('yyyy-MM-dd_HH-mm-ss')
        return formattedDate
}

构建有效,但是当 AS 想要将 apk 复制到设备时,它会抛出一个本地路径不存在. 错误.

The build works, but when AS wants to copy the apk to the device it throws a Local path doesn't exist. error.

问题是生成的文件看起来像:

The problem is that the files that are generated look like:

2014-03-17_16-17-41_myapp__debug-unaligned.apk

但 AS 寻找:

2014-03-17_16-17-18_myapp__debug-unaligned.apk

以某种方式由 AS 特定的构建步骤生成,导致重新计算日期.我尝试使用构建日期的外部属性修复它,该属性在整个构建过程中应该保持不变,但可能由于我缺乏 gradle 技能,这没有帮助.

which is somehow generated by an AS specific build step resulting in a recalculation of the date. I tried to fix it with an external property for the build date which should remain the same throughout the build process, but probably due to my lack of gradle skills, this did not help.

也许有人为我提供了一种解决方法,可以让我在 Android Studio 中进行构建.

Maybe someone has a workaround for me to make my build work in Android Studio.

推荐答案

这很正常,虽然很不幸.

This is normal, though unfortunate.

当 Studio 打开项目时,它会向 Gradle 查询项目的模型.这包括模块列表以及每个模块的源文件夹、依赖项和构建的输出.在这种情况下,APK.

When Studio opens the project, it queries Gradle for the model of the project. This includes the list of modules and for each module, their source folders, their dependencies, and the output of their build. In this case, the APK.

因此,当 Studio 向 Gradle 查询模型时,我们的插件将构建模型,其中包括运行重命名 APK 文件名的自定义代码.然后将其发送到 Studio 进行记录.

So when Studio queries Gradle for the model, our plugin will build the model, which includes running your custom code that renames the APK filename. This is then sent to Studio which records it.

但是,无论何时构建,Studio 都会告诉 Gradle 构建但不传递任何其他信息.Gradle 实际上会再次重新创建模型,并再次运行您的代码.

However whenever you build, Studio will tell Gradle to build but not pass it any other information. Gradle will actually re-create the model again, and run your code again.

这意味着在每次构建时,APK 文件名都不同(因为您的 APK 文件名包含到第二个的日期),但它们都与项目导入期间创建的文件名不匹配.这会导致部署失败.

This means that at every build the APK file name is different (Since your APK filename contains the date to the second), but none of them match the filename created during the project import. This makes the deployment fail.

目前无法让 Gradle 向 Studio 发送生成的 APK 的文件名.

There is no way right now to have Gradle send Studio the filename of the generated APK.

更好的方法是保留当前输出,但将其复制到新文件中以妥善保管.

A better way to do this would be to keep the current output but copy it in a new file for safekeeping.

我会做这样的事情:

android.applicationVariants.all { variant ->
   def file = variant.output

   // create the new task
   def copyTask = project.tasks.create("copy${variant.name}Apk", Copy)
   copyTask.from = file
   copyTask.into = file.parent
   copyTask.rename("app-", getDate() + "_myapp_" + getGitCommit() +"_")

   // set up task dependencies
   // first make the assemble task depend on copyTask to make sure it gets called.
   variant.assemble.dependsOn copyTask

   // then make copyTask depend on the actual packaging task.
   copyTask.dependsOn variant.packageApplication
}

请注意,复制任务需要一个文件夹作为目标,因此我们必须使用重命名规则.

Note that the copy task expect a folder as destination so we have to use a rename rule.

这篇关于在名称中使用时间戳时,Android Studio 无法在构建后启动应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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