1

我正在尝试编写一个执行 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
}

但是,我仍在寻找它首先挂起的原因。

4

1 回答 1

0

尝试这个:

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
于 2013-08-21T21:08:30.193 回答