我正在尝试编写一个执行 adb 命令以清除连接的 android 设备的应用程序/数据缓存的 Gradle 任务:
task clearAppDataCache(type: Exec) {
description = "Clears device app data/cache."
group = "Utils"
commandLine "$sdkDir/platform-tools/adb"
args = ["shell", "pm", "clear", "com.my.package"]
}
如果设备上安装了 com.my.package android 包,则上述任务有效。但是,如果未安装软件包,则任务会打印出失败,然后以下列方式挂起:
有谁知道为什么会这样?我希望它会以类似于原始 shell 命令运行方式的方式失败并完成。
编辑:
将以下配置子句添加到任务会停止挂起:
doFirst {
if (!packageExists("com.my.package"))
throw new GradleException("com.my.package package is not installed on connected device.")
}
具有以下函数定义:
/**
* Returns true if the packageName argument exists on the the connected device,
* false otherwise.
*/
def packageExists(String packageName) {
def output = "adb shell pm list packages -f".execute().waitFor().text
if (output.contains("$packageName")) return true
return false
}
但是,我仍在寻找它首先挂起的原因。