Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

The adb process returned error code -1 #103

Answered by wherewhere
836145715 asked this question in Q&A
Discussion options

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"
You must be logged in to vote

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

Comment options

adb version :Android Debug Bridge version 1.0.41
Version 35.0.0-11411520
Installed as D:\AndroidSdk\platform-tools\adb.exe

You must be logged in to vote
0 replies
Comment options

 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"]
[]

You must be logged in to vote
0 replies
Comment options

我找到了一种解决方案,就是先自己编写代码启动adb.exe 然后再 检测状态就可以了,我猜测错误发生在等待5000ms 超时了,希望可以提供一个 StartServer方法,自定义延迟时间 而不是默认 5000

You must be logged in to vote
0 replies
Comment options

adb.exe 有毛病,有时候不会自己正常退出,我也不知道为什么

You must be logged in to vote
2 replies
Comment options

老哥,这个问题会导致很多人启动不了,你可以看一下我给你发的后面代码,我使用的是 nuget包 版本 3.2.11

Comment options

至少你是第一个有问题的,之前没人提过

Comment options

#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();

你们可以试一下

You must be logged in to vote
2 replies
Comment options

额,这样读不完它就不结束,死了就死掉了

Comment options

好的,谢谢你的解答

Comment options

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

You must be logged in to vote
1 reply
Comment options

好的,3q

Answer selected by wherewhere
Comment options

抱歉,我明白了。。。
这玩意的确是 ErrorOutput,有错误输出就会报错。。。
Screenshot_20240403-184819_Termux.png

You must be logged in to vote
4 replies
Comment options

不过为什么我的code是-1,是不是这个进程死掉了一直没启动上来

Comment options

不清楚,你用StartServer能正常启动吗

Comment options

Comment options

你这个adb可能真的有点奇怪。。。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
bug Something isn't working help wanted Extra attention is needed

AltStyle によって変換されたページ (->オリジナル) /