在 Android Gradle 项目中复制 APK 文件

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

问题描述

我正在尝试向我的 Android 项目的 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.'

    ...
}

根据文档,我可以看到如何使用标准 Copy 任务类型进行文件复制:

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 文件的确切名称和位置?这可以作为财产吗?感觉好像我应该能够将文件声明为我的任务的输入,并将它们声明为 assemble 任务的输出,但是我的 Gradle-fu 不够强大.

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 文件名,所以我的 publish 任务不能只假设默认名称和位置.

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
}

现在你可以调用 gradle 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 Gradle 项目中复制 APK 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:排除特定的构建变体 下一篇:“multiDexEnabled true"是什么意思?意思是?

相关文章

最新文章