从 Gradle 构建文件检测 Android 部署目标

Detect Android deployment target from Gradle build file

我希望通过调用外部工具来扩展我的 gradle 构建。问题是我需要提供当前的目标 CPU 架构,例如 armeabiarm64-v8a 如果用户选择部署到物理设备。我无法找到在 gradle 构建文件中确定此信息的方法。

目前我在预构建之前运行的自定义任务中执行此操作,类似于 this solution。此时有什么方法可以检测 CPU 架构吗?

task customTask(type: Exec) {
    commandLine "myTool.exe", "-architecture=$something"
}

preBuild.dependsOn customTask

我正在使用 experimental plugin 0.7.0-beta3。

谢谢

您可以使用 ADB shell 命令获取已连接设备的 CPU 架构 (ABI):

adb shell getprop ro.product.cpu.abi

对于 Nexus 5,这将 return armeabi-v7a 作为示例。

现在我们必须将该命令包装到 gradle 方法中:

def getDeviceAbi() {
    return "adb shell getprop ro.product.cpu.abi".execute().text.trim()
}

然后您可以简单地从您的任务中调用此方法:

task customTask(type: Exec) {
    commandLine "myTool.exe", "-architecture=" + getDeviceAbi()
}
preBuild.dependsOn customTask