Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions build-tools/scripts/UnitTestApks.targets
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,13 @@
<Output TaskParameter="AdbTarget" PropertyName="_EmuTarget" />
<Output TaskParameter="EmulatorProcessId" PropertyName="_EmuPid" />
</StartAndroidEmulator>
<Sleep
Condition=" '$(_ValidAdbTarget)' != 'True' "
Milliseconds="10000"
/>
<Xamarin.Android.Tools.BootstrapTasks.Adb
EnvironmentVariables="ADB_TRACE=all"
Condition=" '$(_ValidAdbTarget)' != 'True' "
Arguments="$(_AdbTarget) wait-for-device"
ToolExe="$(AdbToolExe)"
ToolPath="$(AdbToolPath)"
/>
<Sleep
Condition=" '$(_ValidAdbTarget)' != 'True' "
Milliseconds="1000"
/>
<Xamarin.Android.Tools.BootstrapTasks.Adb
EnvironmentVariables="ADB_TRACE=all"
Condition=" '$(_ValidAdbTarget)' != 'True' "
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

Expand Down Expand Up @@ -68,14 +69,79 @@ void Run (string emulator)
Arguments = arguments,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
WindowStyle = ProcessWindowStyle.Hidden,
};

Log.LogMessage (MessageImportance.Low, $"Environment variables being passed to the tool:");
var p = new Process () {
StartInfo = psi,
};

var sawError = new AutoResetEvent (false);

DataReceivedEventHandler output = null;
output = (o, e) => {
Log.LogMessage (MessageImportance.Low, $"[emulator stdout] {e.Data}");
if (string.IsNullOrWhiteSpace (e.Data))
return;
if (e.Data.StartsWith ("Hax ram_size", StringComparison.Ordinal) &&
e.Data.EndsWith (" 0x0", StringComparison.Ordinal)) {
Log.LogError ("Emulator failed to start: ram_size is 0MB! Please re-install HAXM.");
sawError.Set ();
}
};
DataReceivedEventHandler error = null;
error = (o, e) => {
Log.LogMessage (MessageImportance.Low, $"[emulator stderr] {e.Data}");
if (string.IsNullOrWhiteSpace (e.Data))
return;
if (e.Data.StartsWith ("Failed to sync", StringComparison.Ordinal) ||
e.Data.Contains ("Internal error")) {
Log.LogError ($"Emulator failed to start: {e.Data}");
Log.LogError ($"Do you have another VM running on the machine? If so, please try exiting the VM and try again.");
sawError.Set ();
}
};

p.OutputDataReceived += output;
p.ErrorDataReceived += error;

p.Start ();
p.BeginOutputReadLine ();
p.BeginErrorReadLine ();

const int Timeout = 20*1000;
int i = WaitHandle.WaitAny (new[]{sawError}, millisecondsTimeout: Timeout);
if (i == 0 || Log.HasLoggedErrors) {
p.Kill ();
return;
}

p.CancelOutputRead ();
p.CancelErrorRead ();

p.OutputDataReceived -= output;
p.ErrorDataReceived -= error;

p.OutputDataReceived += WriteProcessOutputMessage;
p.ErrorDataReceived += WriteProcessErrorMessage;

p.BeginOutputReadLine ();
p.BeginErrorReadLine ();

EmulatorProcessId = p.Id;
}

static void WriteProcessOutputMessage (object sender, DataReceivedEventArgs e)
{
Console.WriteLine (e.Data);
}

static void WriteProcessErrorMessage (object sender, DataReceivedEventArgs e)
{
Console.Error.WriteLine (e.Data);
}
}
}