0

A previous post shows how to get the output of a shell command executed using gradle. However, when I try to apply this to an "adb shell" command, it prints blank lines. For example, this script:

task runTests {
    doLast {
        def stdout = new ByteArrayOutputStream()
        exec {
            commandLine 'cmd' , '/c', 'dir', 'src'
        }
    print stdout
}

prints

Volume in drive C is Windows
Volume Serial Number is 0AEC-E2A0

Directory of C:\Users\mb\android\project

12/02/2016  12:37 PM    <DIR>          .
12/02/2016  12:37 PM    <DIR>          ..
12/02/2016  12:37 PM    <DIR>          androidTest
12/02/2016  12:37 PM    <DIR>          main
12/02/2016  12:37 PM    <DIR>          test
           0 File(s)              0 bytes
           5 Dir(s)  13,056,221,184 bytes free

But this:

task runTests {
    doLast {
        def stdout = new ByteArrayOutputStream()
        exec {
            commandLine 'cmd' , '/c', 'adb', 'shell' , 'ls'
        }
    print stdout
}

prints blank lines. It seems to be printing a blank line for each line of output. If I change the command to

commandLine 'cmd' , '/c', 'adb', 'shell' , 'ls' , 'acct'

Then it prints less blank lines because there are less files in the 'acct' directory. If I run

adb shell ls acct

in the Windows Command Prompt, it prints

cgroup.clone_children
cgroup.event_control
cgroup.procs
cpuacct.stat
cpuacct.usage
cpuacct.usage_percpu
notify_on_release
release_agent
tasks
uid
uid_10006
uid_10014
uid_1037

I'm running Windows 8, Android Studio 2.2 and Gradle 2.10

Update I tried Andrey's suggestion:

task runTests {
    doLast {
        def testOutput = 'adb shell ls acct'.execute([], project.rootDir).text.trim()
        setProperty("archivesBaseName", testOutput)
        println testOutput
    }
}

But this prints the last line of the output, i.e.

uid_1037
Martin
  • 748
  • 7
  • 20

1 Answers1

0

I used the following code to catch and use a command output:

def gitSha = 'git rev-parse --short HEAD'.execute([], project.rootDir).text.trim()

android {
    compileSdkVersion 23
    buildToolsVersion "24.0.1"
    ...
    defaultConfig {
        applicationId "ru.****.***"
        minSdkVersion 21
        targetSdkVersion 23
        versionCode 2
        versionName "0.2"
        setProperty("archivesBaseName", APK_NAME + "-" + versionName + "." + versionCode + "-" + "${gitSha}")
    }
    ...
}

I think adb shell can be called in the same manner.

Andrey Kopeyko
  • 1,556
  • 15
  • 14
  • Thanks for the suggestion, @andrey-kopeyko. I tried this but it prints blank lines for everything except for the very last line of output. In the case of 'adb shell ls acct', it prints 12 blank lines followed by "uid_1037" – Martin Dec 06 '16 at 13:32
  • What code did you tried? Please write down the particular code and the output. – Andrey Kopeyko Dec 06 '16 at 13:46
  • You do not need to wrap your command with `cmd /c` - try to execute `adb` directly, like `adb shell ls -l /` – Andrey Kopeyko Dec 06 '16 at 13:51
  • Andrey, I updated the post to show the command I used and the output. Thanks. – Martin Dec 06 '16 at 14:12