The adb process returned error code -1 #103
-
string adbPath = "D:\AndroidSdk\platform-tools\adb.exe"; 报错如下:
|
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 9 replies
-
adb version :Android Debug Bridge version 1.0.41 |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
我找到了一种解决方案,就是先自己编写代码启动adb.exe 然后再 检测状态就可以了,我猜测错误发生在等待5000ms 超时了,希望可以提供一个 StartServer方法,自定义延迟时间 而不是默认 5000 |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
#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.
-
Extends |
Beta Was this translation helpful? Give feedback.
Extends
AdbCommandLineClient
, overridesRunProcess
andRunProcessAsync
to what you want, and setFactories.AdbCommandLineClientFactory
to create your custom command line client before you usingAdbServer
.Example: https://github.com/SharpAdb/AdvancedSharpAdbClient.SampleApp/blob/main/SharpADB/SharpADB/Common/AdbCommandClient.cs