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
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
12 changes: 11 additions & 1 deletion tests/MSBuildDeviceIntegration/Tests/InstallAndRunTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,17 @@ public void DeployToDevice (bool isRelease)

var dotnet = new DotNetCLI (Path.Combine (Root, builder.ProjectDirectory, proj.ProjectFilePath));
Assert.IsTrue (dotnet.Build (), "`dotnet build` should succeed");
Assert.IsTrue (dotnet.Build ("DeployToDevice"), "`dotnet build -t:DeployToDevice` should succeed");

// Pass the device serial as $(Device) — this is the `dotnet run` code path.
// _EnsureDeviceBooted must be in DeployToDeviceDependsOnTargets (not just BeforeTargets)
// because the .NET SDK invokes DeployToDevice via in-process ProjectInstance.Build()
// where BeforeTargets hooks don't reliably fire.
var serial = RunAdbCommand ("get-serialno").Trim ();
Assert.IsTrue (dotnet.Build ("DeployToDevice", parameters: new [] { $"Device={serial}" }),
"`dotnet build -t:DeployToDevice` should succeed");

// Verify _EnsureDeviceBooted actually ran in the chain
dotnet.AssertTargetIsNotSkipped ("_EnsureDeviceBooted");

// Verify correct targets ran based on FastDev support
if (TestEnvironment.CommercialBuildAvailable) {
Expand Down