問題描述
我正在嘗試向我的 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模板網!