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

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

问题描述



 <$> 

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

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

构建工作,但是当AS想要将apk复制到设备,它会抛出本地路径不存在。错误。



问题是生成的文件如下所示:

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

但AS寻找:

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

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



也许有人对我的Android Studio中的构建工作有一个解决方法。 解决方案

这很正常,虽然不幸。

当Studio打开该项目时,它会向Gradle查询该项目的模型。这包括模块列表和每个模块,它们的源文件夹,它们的依赖关系以及它们的构建输出。因此,当Studio向模型查询Gradle时,我们的插件将构建模型,其中包括运行重命名APK文件名的自定义代码。然后发送到Studio中记录它。



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

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



现在没有办法让Gradle将Studio生成的APK的文件名发送给它。



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



我会做这样的事情:

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

//创建新任务
def copyTask = project.tasks.create(copy $ {variant.name} Apk,复制)
copyTask.from =文件
copyTask.into = file.parent
copyTask.rename(app-,getDate()+_myapp_+ getGitCommit()+_)

//设置任务依赖关系
//首先使组装任务依赖于copyTask以确保它被调用。
variant.assemble.dependsOn copyTask

//然后制作copyTask取决于实际的包装任务。
copyTask.dependsOn variant.packageApplication
}

请注意,复制任务期望作为目的地的文件夹,所以我们必须使用重命名规则。

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
}

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

but AS looks for:

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

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.

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

解决方案

This is normal, though unfortunate.

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.

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.

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.

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.

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

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

I would do something like this:

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天全站免登陆