2017-02-18 13 views
-1

我試圖讀取外殼的輸出數據使用新的字符串(字節)將不會在gradle這個

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

工作,它竟然是在沒有任何的gradle而我在Groovy腳本只是試過代碼, 有效。

終於我發現問題應該是新的字符串(字節),下面的代碼在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這個任務:

enter image description here

如果選擇UTF_8,US_ASCII或ISO_8859_1的字符集,你會看到什麼。

回答

0

好吧,我終於發現,這些字節包含了「\ r」(13),當打印到控制檯,的內容整條線被清除,所以什麼也看不到。

0

似乎正在爲我工​​作,但我懷疑這是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 
+0

原來是我的Android Studio的問題。謝謝。 – Bennyhuo

相關問題