2017-02-18 3 views
-1

Ich habe versucht, die Ausgangsdaten von Shell zu lesen mitnew String (Bytes) nicht in Arbeit gradle

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

und es stellte sich heraus, nichts in gradle zu sein, während ich diesen Code einfach in groovy Skript versucht , es funktionierte.

schließlich fand ich heraus, dass das Problem neue String (Bytes) sein sollte, der Code unten funktioniert in groovy Skript aber nicht in Großbuchstaben.

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) 

Jeder könnte mir sagen, was passiert ist? Wirklich ärgerlich ...

----------- Update -------------------

Ich habe versucht, den Code von @ JBirdVegas, es scheint von Android Studio, Version 2.2.3 stabil zu sein. Ich versuchte auch

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 

und immer noch das gleiche wie zuvor.

Anders als bei der Ausgabe in der Befehlszeile, wenn Sie die gradle Aufgabe in gradle Panel jener IDE auslösen:

enter image description here

Wenn Sie einen Zeichensatz von UTF_8, US_ASCII oder ISO_8859_1 wählen, werden Sie nichts sehen .

Antwort

0

Ok sehen, schließlich finde ich heraus, dass dieser Bytes einen ‚\ r‘ (13) enthält, wenn auf die Konsole ausgegeben, der Inhalt der Die ganze Linie ist gelöscht, so dass nichts gesehen werden kann.

0

Scheint für mich zu arbeiten, aber ich vermute, das ist ein Problem mit Charset.

Mit dem Emulator auf meinem Mac ich folgende

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

Es stellt sich heraus, dass das Problem von meinem Android Studio ist. Vielen Dank. – Bennyhuo

Verwandte Themen