我正在尝试编写一个执行adb命令的Gradle任务来清除已连接的android设备的应用程序/数据缓存:
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
}
但是,我仍在寻找它最初挂起的原因。
最佳答案
试试这个:
task clearAppDataCache(type: Exec) {
description = "Clears device app data/cache."
group = "Utils"
commandLine "./pmclear.sh"
}
pmclear.sh:
#!/bin/bash
[ "`adb shell "pm list packages com.my.package"`" == "" ] || adb shell "pm clear com.my.package
关于android - Gradle adb Android Exec任务因失败而挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18367286/