Search code examples
androidandroid-gradle-plugingradle-experimental

Detect Android deployment target from Gradle build file


I'm looking to extend my gradle build with a call to an external tool. The issue is that I need to provide the current CPU architecture that is currently being targeted, for example armeabi or arm64-v8a if the user has chosen to deploy to a physical device. I am having trouble finding a way of determining this information in the gradle build file.

Currently I am performing this action in a custom task which runs before preBuild, similar to this solution. Is there any way to detect the CPU architecture at this point?

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

preBuild.dependsOn customTask

I'm using the experimental plugin 0.7.0-beta3.

Thanks


Solution

  • You can get the CPU architecture (ABI) of the connected device with an ADB shell command:

    adb shell getprop ro.product.cpu.abi
    

    For a Nexus 5 this would return armeabi-v7a as an example.

    Now we have to wrap that command into a gradle method:

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

    Then you can simply call this method from your task:

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