-
Notifications
You must be signed in to change notification settings - Fork 67
The adb process returned error code -1 #103
-
string adbPath = "D:\AndroidSdk\platform-tools\adb.exe";
var adbCommandLineClient = new AdbCommandLineClient(adbPath);
Console.WriteLine("adb version :" + adbCommandLineClient.GetVersion());
var executeAdbCommand = adbCommandLineClient.ExecuteAdbCommand("start-server"); //error
报错如下:
The adb process returned error code -1 when running command start-server with error output:
- daemon not running; starting now at tcp:5037"
Beta Was this translation helpful? Give feedback.
All reactions
Extends AdbCommandLineClient, overrides RunProcess and RunProcessAsync to what you want, and set Factories.AdbCommandLineClientFactory to create your custom command line client before you using AdbServer.
Example: https://github.com/SharpAdb/AdvancedSharpAdbClient.SampleApp/blob/main/SharpADB/SharpADB/Common/AdbCommandClient.cs
Replies: 7 comments 9 replies
-
adb version :Android Debug Bridge version 1.0.41
Version 35.0.0-11411520
Installed as D:\AndroidSdk\platform-tools\adb.exe
Beta Was this translation helpful? Give feedback.
All reactions
-
private static int RunAdbProcessInner(string adbpath,string command, List<string> errorOutput, List<string> standardOutput) { if (command == null) { throw new ArgumentNullException(nameof(command)); } int status; ProcessStartInfo psi = new ProcessStartInfo(adbpath, command) { CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden, UseShellExecute = false, RedirectStandardError = true, RedirectStandardOutput = true }; using (Process process = Process.Start(psi)) { var standardErrorString = process.StandardError.ReadToEnd(); var standardOutputString = process.StandardOutput.ReadToEnd(); if (errorOutput != null) { errorOutput.AddRange(standardErrorString.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)); } if (standardOutput != null) { standardOutput.AddRange(standardOutputString.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)); } // get the return code from the process if (!process.WaitForExit(5000)) { process.Kill(); } status = process.ExitCode; } return status; } static void Main() { string adbPath = "D:\\AndroidSdk\\platform-tools\\adb.exe"; List<string> stand = new List<string>(); List<string> error = new List<string>(); var status = RunAdbProcessInner(adbPath, "kill-server", stand, error); Console.WriteLine("status:" + status); Console.WriteLine(stand.ToJsonString()); Console.WriteLine(error.ToJsonString()); status = RunAdbProcessInner(adbPath, "start-server", stand, error); Console.WriteLine("status:"+status); Console.WriteLine(stand.ToJsonString()); Console.WriteLine(error.ToJsonString()); }
我运行上面的代码,却启动成功了,这个代码是我在SharpAdbClient抄过来的
输出结果如下:
status:0
[]
[]
status:0
["* daemon not running; starting now at tcp:5037","* daemon started successfully"]
[]
Beta Was this translation helpful? Give feedback.
All reactions
-
我找到了一种解决方案,就是先自己编写代码启动adb.exe 然后再 检测状态就可以了,我猜测错误发生在等待5000ms 超时了,希望可以提供一个 StartServer方法,自定义延迟时间 而不是默认 5000
Beta Was this translation helpful? Give feedback.
All reactions
-
adb.exe 有毛病,有时候不会自己正常退出,我也不知道为什么
Beta Was this translation helpful? Give feedback.
All reactions
-
老哥,这个问题会导致很多人启动不了,你可以看一下我给你发的后面代码,我使用的是 nuget包 版本 3.2.11
Beta Was this translation helpful? Give feedback.
All reactions
-
至少你是第一个有问题的,之前没人提过
Beta Was this translation helpful? Give feedback.
All reactions
-
#if !HAS_PROCESS [DoesNotReturn] #endif protected virtual int RunProcess(string filename, string command, ICollection<string>? errorOutput, ICollection<string>? standardOutput) { #if HAS_PROCESS ProcessStartInfo psi = new(filename, command) { CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden, UseShellExecute = false, RedirectStandardError = true, RedirectStandardOutput = true }; using Process process = Process.Start(psi) ?? throw new AdbException($"The adb process could not be started. The process returned null when starting {filename} {command}"); // get the return code from the process if (!process.WaitForExit(5000)) { process.Kill(); } string standardErrorString = process.StandardError.ReadToEnd(); string standardOutputString = process.StandardOutput.ReadToEnd(); errorOutput?.AddRange(standardErrorString.Split(separator, StringSplitOptions.RemoveEmptyEntries)); standardOutput?.AddRange(standardOutputString.Split(separator, StringSplitOptions.RemoveEmptyEntries)); return process.ExitCode; #else throw new PlatformNotSupportedException("This platform is not support System.Diagnostics.Process. You can start adb server by running `adb start-server` manually."); #endif }
这是来自 AdbCommandLineClient.cs 文件中的代码 方法是 RunProcess
我看了一下代码,发现将这两段代码顺序调换了,就可以执行成功
if (!process.WaitForExit(5000)) { process.Kill(); } //这个代码放到 process.WaitForExit(5000)之前即可 string standardErrorString = process.StandardError.ReadToEnd(); string standardOutputString = process.StandardOutput.ReadToEnd();
你们可以试一下
Beta Was this translation helpful? Give feedback.
All reactions
-
额,这样读不完它就不结束,死了就死掉了
Beta Was this translation helpful? Give feedback.
All reactions
-
好的,谢谢你的解答
Beta Was this translation helpful? Give feedback.
All reactions
-
Extends AdbCommandLineClient, overrides RunProcess and RunProcessAsync to what you want, and set Factories.AdbCommandLineClientFactory to create your custom command line client before you using AdbServer.
Example: https://github.com/SharpAdb/AdvancedSharpAdbClient.SampleApp/blob/main/SharpADB/SharpADB/Common/AdbCommandClient.cs
Beta Was this translation helpful? Give feedback.
All reactions
-
好的,3q
Beta Was this translation helpful? Give feedback.
All reactions
-
抱歉,我明白了。。。
这玩意的确是 ErrorOutput,有错误输出就会报错。。。
Screenshot_20240403-184819_Termux.png
Beta Was this translation helpful? Give feedback.
All reactions
-
不过为什么我的code是-1,是不是这个进程死掉了一直没启动上来
Beta Was this translation helpful? Give feedback.
All reactions
-
不清楚,你用StartServer能正常启动吗
Beta Was this translation helpful? Give feedback.
All reactions
-
Beta Was this translation helpful? Give feedback.
All reactions
-
你这个adb可能真的有点奇怪。。。
Beta Was this translation helpful? Give feedback.