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

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

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

@discardableResult
static func shell(_ args: String...) -> Int32
{
    let task = Process()
    task.launchPath = "/usr/bin/env"
    task.arguments = args
    task.launch()
    task.waitUntilExit()
    return task.terminationStatus
}

@discardableResult
static func shellAdb(_ args: String...) -> Int32
{
    let task = Process()
    task.launchPath = "/Users/myname/Library/Android/sdk/platform-tools"
    task.arguments = args
    task.launch()
    task.waitUntilExit()
    return task.terminationStatus
}

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

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

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

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

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

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

private func runAdbCommand(_ command: String) -> String {
        let task = Process()
        let pipe = Pipe()
        
        task.standardOutput = pipe
        task.standardError = pipe
        task.arguments = ["-c", adb!.path + " " + command]
        task.launchPath = "/bin/sh"
        task.launch()
        
        let data = pipe.fileHandleForReading.readDataToEndOfFile()
        let output = String(data: data, encoding: .utf8)!.trimmingCharacters(in: .whitespacesAndNewlines)
        return output
    }

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

相关问题