Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ This file contains targets specific for Android application projects.
</_RunDependsOn>
<_AndroidComputeRunArgumentsDependsOn Condition=" '$(_AndroidComputeRunArgumentsDependsOn)' == '' ">
_ResolveMonoAndroidSdks;
_EnsureDeviceBooted;
_GetAndroidPackageName;
_AndroidAdbToolPath;
</_AndroidComputeRunArgumentsDependsOn>
Expand Down Expand Up @@ -60,9 +61,14 @@ This file contains targets specific for Android application projects.
Target that ensures the selected device is online and ready. If $(Device) refers to an
unbooted AVD, this target boots the emulator and updates $(AdbTarget) to the resolved
ADB serial (e.g. "-s emulator-5554").
Runs before _GetPrimaryCpuAbi (in commercial builds) so $(AdbTarget) is correct,
and before _DeployApk/_DeployAppBundle (which use AdbTarget for adb commands)
when $(Device) is set.
This target is included in DeployToDeviceDependsOnTargets (in BuildOrder.targets) so it runs
before _DeployApk/_DeployAppBundle/_Upload via DependsOnTargets. Using BeforeTargets alone
is insufficient because the .NET SDK's `dotnet run` invokes DeployToDevice via
ProjectInstance.Build() in-process, where BeforeTargets hooks may not fire.
The BeforeTargets="_GetPrimaryCpuAbi" entry is kept for commercial builds where
_GetPrimaryCpuAbi needs AdbTarget resolved before it runs.
Note: this target CANNOT use DependsOnTargets="_ResolveMonoAndroidSdks" because in commercial
builds _GetPrimaryCpuAbi is in $(_ResolveMonoAndroidSdksDependsOn), which would create a cycle:
Expand All @@ -72,7 +78,7 @@ This file contains targets specific for Android application projects.
***********************************************************************************************
-->
<Target Name="_EnsureDeviceBooted"
BeforeTargets="_GetPrimaryCpuAbi;_DeployApk;_DeployAppBundle"
BeforeTargets="_GetPrimaryCpuAbi"
Condition=" '$(Device)' != '' ">
<BootAndroidEmulator
Device="$(Device)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,14 @@ properties that determine build ordering.
<DeployToDeviceDependsOnTargets Condition=" '$(_AndroidFastDeploymentSupported)' == 'true' ">
$(_MinimalSignAndroidPackageDependsOn);
_GenerateEnvironmentFiles;
_EnsureDeviceBooted;
_Upload;
_AndroidConfigureAdbReverse;
</DeployToDeviceDependsOnTargets>
<DeployToDeviceDependsOnTargets Condition=" '$(_AndroidFastDeploymentSupported)' != 'true' ">
$(_MinimalSignAndroidPackageDependsOn);
_GenerateEnvironmentFiles;
_EnsureDeviceBooted;
_DeployApk;
_DeployAppBundle;
_AndroidConfigureAdbReverse;
Expand Down
17 changes: 13 additions & 4 deletions src/Xamarin.Android.Build.Tasks/Tasks/BootAndroidEmulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,19 @@ protected virtual bool IsOnlineAdbDevice (string adbPath, string deviceId)
}

/// <summary>
/// Gets the AVD name from a running emulator via 'adb -s serial emu avd name'.
/// Gets the AVD name from a running emulator. Tries 'adb shell getprop ro.boot.qemu.avd_name'
/// first (works reliably on modern emulators), then falls back to the emulator console command
/// 'adb emu avd name' (which may return empty on some emulator versions).
/// </summary>
protected virtual string? GetRunningAvdName (string adbPath, string serial)
{
// Primary: use the boot property which is always set on emulators
var name = GetShellProperty (adbPath, serial, "ro.boot.qemu.avd_name");
if (!name.IsNullOrEmpty ()) {
return name;
}

// Fallback: try the emulator console command
string? avdName = null;
try {
var outputLines = new List<string> ();
Expand All @@ -271,9 +280,9 @@ protected virtual bool IsOnlineAdbDevice (string adbPath, string deviceId)
);

if (outputLines.Count > 0) {
var name = outputLines [0].Trim ();
if (!name.IsNullOrEmpty () && !name.Equals ("OK", StringComparison.OrdinalIgnoreCase)) {
avdName = name;
var consoleName = outputLines [0].Trim ();
if (!consoleName.IsNullOrEmpty () && !consoleName.Equals ("OK", StringComparison.OrdinalIgnoreCase)) {
avdName = consoleName;
Comment on lines 282 to +285
}
}
} catch (Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,75 @@ public void APFBDependsOn ([Values (false, true)] bool isAppProject)
StringAssertEx.Contains ("Running target: 'MyPrepareTarget'", builder.LastBuildOutput);
}

[Test]
public void DeployToDeviceDependsOn_ContainsEnsureDeviceBooted ()
{
// _EnsureDeviceBooted must be in DeployToDeviceDependsOnTargets so that it fires
// when the .NET SDK calls ProjectInstance.Build(["DeployToDevice"]) in-process,
// where BeforeTargets hooks are not reliably triggered.
var checkTargets = new Import (() => "CheckDeployOrder.targets") {
TextContent = () => """
<Project>
<Target Name="_CheckDeployOrder">
<Message Text="DeployToDeviceDependsOnTargets=$(DeployToDeviceDependsOnTargets)" Importance="high" />
</Target>
</Project>
"""
};

var proj = new XamarinAndroidApplicationProject {
Imports = { checkTargets }
};

using var builder = CreateApkBuilder ();
builder.Verbosity = LoggerVerbosity.Detailed;
Assert.IsTrue (builder.RunTarget (proj, "_CheckDeployOrder"),
"Build should have succeeded.");

// The property is multi-line, so join all build output into a single string for matching
string allOutput = string.Join ("\n", builder.LastBuildOutput);
StringAssert.Contains ("_EnsureDeviceBooted", allOutput,
"DeployToDeviceDependsOnTargets must contain _EnsureDeviceBooted");

// _EnsureDeviceBooted must appear before _DeployApk to set AdbTarget first
int bootIndex = allOutput.IndexOf ("_EnsureDeviceBooted", StringComparison.Ordinal);
int deployIndex = allOutput.IndexOf ("_DeployApk", StringComparison.Ordinal);
if (deployIndex >= 0) {
Assert.Less (bootIndex, deployIndex,
"_EnsureDeviceBooted must appear before _DeployApk in DeployToDeviceDependsOnTargets");
}
}

[Test]
public void DeployToDeviceDependsOn_MSBuildValidation ()
{
// Validates the same constraint using MSBuild property functions directly,
// as a safety net in case log parsing has edge cases.
var checkTargets = new Import (() => "CheckDeployOrder.targets") {
TextContent = () => """
<Project>
<Target Name="_CheckDeployOrder">
<Error Text="DeployToDeviceDependsOnTargets does not contain _EnsureDeviceBooted"
Condition="!$(DeployToDeviceDependsOnTargets.Contains('_EnsureDeviceBooted'))" />
<PropertyGroup>
<_BootIndex>$(DeployToDeviceDependsOnTargets.IndexOf('_EnsureDeviceBooted'))</_BootIndex>
<_DeployIndex>$(DeployToDeviceDependsOnTargets.IndexOf('_DeployApk'))</_DeployIndex>
</PropertyGroup>
<Error Text="_EnsureDeviceBooted (at $(_BootIndex)) must appear before _DeployApk (at $(_DeployIndex))"
Condition=" '$(_DeployIndex)' != '-1' And $(_BootIndex) &gt; $(_DeployIndex) " />
</Target>
</Project>
"""
};

var proj = new XamarinAndroidApplicationProject {
Imports = { checkTargets }
};

using var builder = CreateApkBuilder ();
Assert.IsTrue (builder.RunTarget (proj, "_CheckDeployOrder"),
"Build should have succeeded — _EnsureDeviceBooted must be in DeployToDeviceDependsOnTargets before _DeployApk.");
}

}
}