我正在尝试使用以下命令从shell读取输出数据

def cmd = "adb shell echo \$EXTERNAL_STORAGE"
def proc = cmd.execute()
println proc.in.text

当我只是在groovy脚本中尝试该代码时,结果就没什么好用的了。

最终我发现问题应该是新的String(bytes),下面的代码在groovy脚本中起作用,而在gradle中不起作用。
byte[] bytes = [47, 115, 116, 111, 114, 97, 103, 101, 47, 101, 109, 117, 108, 97, 116, 101, 100, 47, 108, 101, 103, 97, 99, 121, 13, 10]
println new String(bytes)

谁能告诉我发生了什么事?真烦人...

-----------更新-------------------

我通过@JBirdVegas尝试了代码,这似乎是由Android Studio 2.2.3版稳定引起的。我也试过
IntelliJ IDEA 2016.3.3
Build #IU-163.11103.6, built on January 17, 2017
Licensed to Benny Huo
Subscription is active until August 6, 2017
JRE: 1.8.0_112-release-408-b6 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o

还是和以前一样

与命令行中的输出不同,在这些IDE的gradle面板中触发gradle任务时,:

android-studio - 新的字符串(字节)在gradle中不起作用-LMLPHP

如果选择UTF_8,US_ASCII或ISO_8859_1的字符集,则将看不到任何内容。

最佳答案

似乎对我有用,但我怀疑Charset存在问题。

在Mac上使用模拟器,我看到以下内容

tasks.create('testAdb') {
    def cmd = "adb shell echo \$EXTERNAL_STORAGE"
    def proc = cmd.execute()
    print "cmd result: $proc.in.text"
    byte[] bytes = [47, 115, 116, 111, 114, 97, 103, 101, 47, 101, 109, 117, 108, 97, 116, 101, 100, 47, 108, 101, 103, 97, 99, 121, 13, 10]
    print "new String bytes (with default charset): ${new String(bytes, Charset.defaultCharset())}"
    println "default charset: ${Charset.defaultCharset()}"
    print "new String bytes: ${new String(bytes)}"
    print "new String bytes (with UTF-8 charset): ${new String(bytes, StandardCharsets.UTF_8)}"
    println "new String bytes (with UTF-16 charset): ${new String(bytes, StandardCharsets.UTF_16)}"
    println "new String bytes (with UTF-16BE charset): ${new String(bytes, StandardCharsets.UTF_16BE)}"
    println "new String bytes (with UTF-16LE charset): ${new String(bytes, StandardCharsets.UTF_16LE)}"
    print "new String bytes (with US_ASCII charset): ${new String(bytes, StandardCharsets.US_ASCII)}"
    print "new String bytes (with ISO_8859_1 charset): ${new String(bytes, StandardCharsets.ISO_8859_1)}"
}


$ ./gradlew testAdb -q
cmd result: /sdcard
new String bytes (with default charset): /storage/emulated/legacy
default charset: UTF-8
new String bytes: /storage/emulated/legacy
new String bytes (with UTF-8 charset): /storage/emulated/legacy
new String bytes (with UTF-16 charset): ⽳瑯牡来⽥浵污瑥搯汥条捹ഊ
new String bytes (with UTF-16BE charset): ⽳瑯牡来⽥浵污瑥搯汥条捹ഊ
new String bytes (with UTF-16LE charset): 猯潴慲敧支畭慬整⽤敬慧祣਍
new String bytes (with US_ASCII charset): /storage/emulated/legacy
new String bytes (with ISO_8859_1 charset): /storage/emulated/legacy

08-17 17:03