来自macOS应用程序的ADB命令带有特殊字符 [英] ADB command from macOS application with special characters

查看:293
本文介绍了来自macOS应用程序的ADB命令带有特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过macOS应用程序中的 adb 命令从Android设备中提取文件。

I want to pull a file from my android device through an adb command from my macOS application.

一切都可以使用下面的代码完美地完成,除非我要提取的文件名包含特殊字符(例如德国变音符(äöüÄÖÜ))。

Everything works perfect with the code below, except when the name of the file I want to pull contains special characters like german umlauts (äöüÄÖÜ).

我收到此错误:
adb:错误:无法统计远程对象'/ storage / emulated / 0 / Download /Böse ':没有这样的文件或目录

但是当我使用命令 adb pull / storage / emulated / 0 / Download /Böse〜/ Desktop Terminal.app 内,文件将被拉到我的计算机上。

But when I use the command adb pull /storage/emulated/0/Download/Böse ~/Desktop from within the Terminal.app, the file will be pulled to my computer.

奇怪的是,如果我从Xcode控制台输出中复制子字符串 / storage / emulated / 0 / Download /Böse Terminal.app 中也不起作用,直到我删除ö并将其替换为ö从我的键盘输入。

The strange thing here is that if I copy the substring /storage/emulated/0/Download/Böse from the Xcode console output, the command is also not working within the Terminal.app until I delete the ö and replace it with an ö from my keyboard input.

我尝试用Unicode替换ö表示形式 \u {00f6} ,但这没有效果(但是控制台输出仍然显示ö,但是

I tried replacing the ö with the unicode representation \u{00f6}, but this has no effect (but the console output still shows an ö but the 'wrong' encoded one.

// Configure task.
let task = Process()
task.launchPath = "~/Library/Android/sdk/platform-tools/adb"
task.arguments = ["pull", "/storage/emulated/0/Download/Böse", "~/Desktop"]

// Configure pipe.
let pipe = Pipe()
task.standardOutput = pipe
task.standardError = pipe
task.launch()

// Run task.
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: .utf8)
task.waitUntilExit()

// adb: error: failed to stat remote object '/storage/emulated/0/Download/Böse': No such file or directory
print(output)

我在文档中发现了以下内容,即 Process 如何处理我提供的参数:

I found the following in the documentation, how the Process handles the arguments that I provide:


在通过argv []将它们传递给任务之前,NSTask对象将参数中的路径和字符串都转换为适当的C样式字符串(使用fileSystemRepresentation)。参数中的字符串不进行shell扩展,因此您不需要执行特殊的引号,并且不解析shell变量(例如$ PWD)。

The NSTask object converts both path and the strings in arguments to appropriate C-style strings (using fileSystemRepresentation) before passing them to the task via argv[] . The strings in arguments do not undergo shell expansion, so you do not need to do special quoting, and shell variables, such as $PWD, are not resolved.

似乎我不是唯一遇到此问题的人,并且我找到了解决方法:
,但我无法使其与Swift一起工作。

It seems like I am not the only one with this problem, and I found this workaround: How to work around NSTask calling -[NSString fileSystemRepresentation] for arguments, but I was not able to make it work with Swift.

推荐答案

作为一种解决方法,我现在将adb命令写入文件,并从应用程序中的bash命令执行它。

As a workaround I am now writing my adb command to a file and execute it from a bash command in my application.

let source = "/storage/emulated/0/Download/Böse"
let destination = "~/Desktop"
guard let uniqueURL = URL(string: destination + "/" + ProcessInfo.processInfo.globallyUniqueString) else { return }

// Write command to file
let scriptContent = "#!/bin/bash\n~/Library/Android/sdk/platform-tools/adb pull -a \"" + source + "\" \"" + destination + "\""
try? scriptContent.write(to: uniqueURL, atomically: false, encoding: .utf8)

// Configure task.
let task = Process()
task.environment = ["LC_ALL": "de_DE.UTF-8", "LANG": "de_DE.UTF-8"]
task.launchPath = "/bin/bash"
task.arguments = [uniqueURL.path]

// Configure pipe.
let pipe = Pipe()
task.standardOutput = pipe
task.standardError = pipe
try? task.run()

// Run task.
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: .utf8)
task.waitUntilExit()

print(output)

即使此操作目前仍有效,但它并不是令人满意的解决方案非常优雅,效率也不高,因此欢迎任何改进或更好的答案。

Even though this is working for now, it is not a satisfactory solution as it is not very elegant and not efficient too, so any improvements or better answers are welcome.

这篇关于来自macOS应用程序的ADB命令带有特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆