【发布时间】:2016-12-05 21:38:54
【问题描述】:
A previous post 展示了如何获取使用 gradle 执行的 shell 命令的输出。但是,当我尝试将其应用于“adb shell”命令时,它会打印空行。例如,这个脚本:
task runTests {
doLast {
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'cmd' , '/c', 'dir', 'src'
}
print stdout
}
打印
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
但是这个:
task runTests {
doLast {
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'cmd' , '/c', 'adb', 'shell' , 'ls'
}
print stdout
}
打印空行。它似乎为每一行输出打印一个空白行。如果我将命令更改为
commandLine 'cmd' , '/c', 'adb', 'shell' , 'ls' , 'acct'
然后它会打印更少的空白行,因为 'acct' 目录中的文件更少。如果我运行
adb shell ls acct
在 Windows 命令提示符中,它会打印
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
我正在运行 Windows 8、Android Studio 2.2 和 Gradle 2.10
更新 我尝试了 Andrey 的建议:
task runTests {
doLast {
def testOutput = 'adb shell ls acct'.execute([], project.rootDir).text.trim()
setProperty("archivesBaseName", testOutput)
println testOutput
}
}
但这会打印输出的最后一行,即
uid_1037
【问题讨论】:
标签: android android-studio gradle adb