本文介绍了在Android的摇篮项目复制APK文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自定义任务添加到我的Andr​​oid项目的 build.gradle 复制最后的APK和Proguard的的 mapping.txt 到不同的目录。我的任务依赖于 assembleDevDebug 任务:

I'm trying to add a custom task to my Android project's build.gradle to copy the final APK and Proguard's mapping.txt into a different directory. My task depends on the assembleDevDebug task:

task publish(dependsOn: 'assembleDevDebug') << {
    description 'Copies the final APK to the release directory.'

    ...
}

我可以看到如何使用标准复制任务类型,做一个文件副本,每个文档:

I can see how to do a file copy using the standard Copy task type, as per the docs:

task(copy, type: Copy) {
    from(file('srcDir'))
    into(buildDir)
}

但假设你知道你要复制的文件的名称和位置。

but that assumes you know the name and location of the file you want to copy.

我如何才能找到它建为 assembleDevDebug 任务的一部分APK文件的确切名称和位置?这是作为一个属性?这种感觉就好像我应该能够将文件作为输入申报给我的任务,并宣布他们从组装任务输出,但是我的摇篮福不强够了。

How can I find the exact name and location of the APK file which was built as part of the assembleDevDebug task? Is this available as a property? It feels as if I should be able to declare the files as inputs to my task, and declare them as outputs from the assemble task, but my Gradle-fu isn't strong enough.

我有一些自定义逻辑版本号注入的APK文件名,所以我的发布任务不能只承担了默认名称和位置。

I have some custom logic to inject the version number into the APK filename, so my publish task can't just assume the default name and location.

推荐答案

如果你能得到与devDebug相关的变量对象,您可以用getOutputFile查询它()。

If you can get the variant object associated with devDebug you could query it with getOutputFile().

所以,如果你想发布所有变种你最好是这样的:

So if you wanted to publish all variants you'd something like this:

def publish = project.tasks.create("publishAll")
android.applicationVariants.all { variant ->
  def task = project.tasks.create("publish${variant.name}Apk", Copy)
  task.from(variant.outputFile)
  task.into(buildDir)

  task.dependsOn variant.assemble
  publish.dependsOn task
}

现在你可以叫摇篮publishAll ,它会发布所有你变体。

Now you can call gradle publishAll and it'll publish all you variants.

一个问题的映射文件是Proguard的任务不给你一个getter文件位置,所以目前不能查询。我希望此问题得到解决。

One issue with the mapping file is that the Proguard task doesn't give you a getter to the file location, so you cannot currently query it. I'm hoping to get this fixed.

这篇关于在Android的摇篮项目复制APK文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 11:17