shell Swift不能执行adb和命令行指令

nuypyhwy  于 2023-10-23  发布在  Shell
关注(0)|答案(1)|浏览(163)

我试图在macOS应用程序中使用swift执行命令行指令。代码如下:

  1. @discardableResult
  2. static func shell(_ args: String...) -> Int32
  3. {
  4. let task = Process()
  5. task.launchPath = "/usr/bin/env"
  6. task.arguments = args
  7. task.launch()
  8. task.waitUntilExit()
  9. return task.terminationStatus
  10. }
  11. @discardableResult
  12. static func shellAdb(_ args: String...) -> Int32
  13. {
  14. let task = Process()
  15. task.launchPath = "/Users/myname/Library/Android/sdk/platform-tools"
  16. task.arguments = args
  17. task.launch()
  18. task.waitUntilExit()
  19. return task.terminationStatus
  20. }

我使用'adb --v'和'echo hello'字符串,并将其传递给'shell'函数。
我也尝试了'shellAdb'函数的第一个命令,但它也不工作。
如果我使用'shell'函数,我会得到这些错误:

  1. env: adb --v: No such file or directory
  2. env: echo 'hello': No such file or directory

这两个错误代码都是“127”。
如果我使用'shellAdb'函数,我会得到另一个错误:

  1. [General] Couldn't posix_spawn: error 13
  2. [General] (
  3. 0 CoreFoundation 0x00007fffc5ab52cb __exceptionPreprocess + 171
  4. 1 libobjc.A.dylib 0x00007fffda8c048d objc_exception_throw + 48
  5. 2 CoreFoundation 0x00007fffc5b33c3d +[NSException raise:format:] + 205
  6. 3 Foundation 0x00007fffc74de54e -[NSConcreteTask launchWithDictionary:] + 3134
  7. 4 AndroidPaste 0x0000000100003684 _TZFC12AndroidPaste17TerminalCommandEx8shellAdbftGSaSS__Vs5Int32 + 340
  8. 5 AndroidPaste 0x0000000100002520 _TFC12AndroidPaste14ViewController11pasteActionfCSo8NSButtonT_ + 640
  9. 6 AndroidPaste 0x000000010000269a _TToFC12AndroidPaste14ViewController11pasteActionfCSo8NSButtonT_ + 58
  10. 7 libsystem_trace.dylib 0x00007fffdb3d73a7 _os_activity_initiate_impl + 53
  11. 8 AppKit 0x00007fffc3ca3721 -[NSApplication(NSResponder) sendAction:to:from:] + 456
  12. 9 AppKit 0x00007fffc3787cc4 -[NSControl sendAction:to:] + 86
  13. 10 AppKit 0x00007fffc3787bec __26-[NSCell _sendActionFrom:]_block_invoke + 136
  14. 11 libsystem_trace.dylib 0x00007fffdb3d73a7 _os_activity_initiate_impl + 53
  15. 12 AppKit 0x00007fffc3787b44 -[NSCell _sendActionFrom:] + 128
  16. 13 AppKit 0x00007fffc37ca539 -[NSButtonCell _sendActionFrom:] + 98
  17. 14 libsystem_trace.dylib 0x00007fffdb3d73a7 _os_activity_initiate_impl + 53
  18. 15 AppKit 0x00007fffc3786426 -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2481
  19. 16 AppKit 0x00007fffc37ca272 -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 798
  20. 17 AppKit 0x00007fffc3784ddb -[NSControl mouseDown:] + 832
  21. 18 AppKit 0x00007fffc3e1f24f -[NSWindow(NSEventRouting) _handleMouseDownEvent:isDelayedEvent:] + 6341
  22. 19 AppKit 0x00007fffc3e1ba6c -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] + 1942
  23. 20 AppKit 0x00007fffc3e1af0a -[NSWindow(NSEventRouting) sendEvent:] + 541
  24. 21 AppKit 0x00007fffc3c9f681 -[NSApplication(NSEvent) sendEvent:] + 1145
  25. 22 AppKit 0x00007fffc351a427 -[NSApplication run] + 1002
  26. 23 AppKit 0x00007fffc34e4e0e NSApplicationMain + 1237
  27. 24 AndroidPaste 0x000000010000319d main + 13
  28. 25 libdyld.dylib 0x00007fffdb1a5235 start + 1

这个问题出现在shell和shellAdb函数中,但它们看起来像我已经找到的例子。
我正在使用Swift 3,macOS 10.12.5和Xcode 8.3.2

lfapxunr

lfapxunr1#

使用此链接下载最新版本的平台工具:https://developer.android.com/studio/releases/platform-tools?hl=fr
解压缩压缩并在项目中添加move adb文件。
你可以从bundle中加载:

  1. let adb = Bundle.main.url(forResource: "adb", withExtension: nil)

接下来,你可以用这个方法运行命令:

  1. private func runAdbCommand(_ command: String) -> String {
  2. let task = Process()
  3. let pipe = Pipe()
  4. task.standardOutput = pipe
  5. task.standardError = pipe
  6. task.arguments = ["-c", adb!.path + " " + command]
  7. task.launchPath = "/bin/sh"
  8. task.launch()
  9. let data = pipe.fileHandleForReading.readDataToEndOfFile()
  10. let output = String(data: data, encoding: .utf8)!.trimmingCharacters(in: .whitespacesAndNewlines)
  11. return output
  12. }

如果你不想了解更多的细节,请查看这个仓库:https://github.com/naman14/adb-tools-mac/blob/master/adbconnect/AdbHelper.swift
他直接在项目中添加了adb文件,以便能够执行adb命令。

展开查看全部

相关问题