From 88368465ecb1662234ee2cc93d0b103e0746cd27 Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Wed, 30 Oct 2024 09:55:30 -0500 Subject: [PATCH 1/2] [Xamarin.Android.Build.Tasks] implement `dotnet run` with an MSBuild target Context: https://github.com/dotnet/sdk/issues/42155 Context: https://github.com/dotnet/sdk/pull/42240 Fixes: https://github.com/dotnet/sdk/issues/31253 The .NET SDK has introduced a new `ComputeRunArguments` MSBuild target that allows you to set `$(RunCommand)` and `$(RunArguments)` in a more dynamic way. So, on Android: * `ComputeRunArguments` depends on `Install`, so the app is deployed, the `` MSBuild target runs, etc. * `$(RunCommand)` is a path to `adb` * `$(RunArguments)` is an `shell am start` command to launch the main activity. The new implementation also allows us to use the `-p` parameter with `dotnet run`, such as: dotnet run -bl -p:AdbTarget=-d This will pass `-d` to `adb`, which allows you to select an attached device if an emulator is running. Previously, we had no way to pass `-p` arguments to `dotnet run`. --- .../Microsoft.Android.Sdk.Application.targets | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.Application.targets b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.Application.targets index 5913bba6b77..8e02ed9df2c 100644 --- a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.Application.targets +++ b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.Application.targets @@ -15,8 +15,6 @@ This file contains targets specific for Android application projects. false <_RuntimeIdentifierUsesAppHost>false - dotnet - build "$(MSBuildProjectFullPath)" -target:Run --configuration "$(Configuration)" <_RunDependsOn Condition=" '$(_XASupportsFastDev)' == 'true' "> @@ -27,8 +25,29 @@ This file contains targets specific for Android application projects. Install; StartAndroidActivity; + <_AndroidComputeRunArgumentsDependsOn> + Install; + + + + + + + $(AdbToolExe) + adb.exe + adb + $([System.IO.Path]::Combine ('$(AdbToolPath)', '$(RunCommand)')) + $(AdbTarget) shell am start -S -n "$(_AndroidPackage)/$(AndroidLaunchActivity)" + $(MSBuildProjectDirectory) + + + From 4db2cb8f96400b9c58ab90b8c5492fff97381b6d Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Thu, 31 Oct 2024 09:16:17 -0500 Subject: [PATCH 2/2] Add test --- .../Tests/InstallAndRunTests.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/MSBuildDeviceIntegration/Tests/InstallAndRunTests.cs b/tests/MSBuildDeviceIntegration/Tests/InstallAndRunTests.cs index 6579abe21aa..6ac736b3fbc 100644 --- a/tests/MSBuildDeviceIntegration/Tests/InstallAndRunTests.cs +++ b/tests/MSBuildDeviceIntegration/Tests/InstallAndRunTests.cs @@ -28,6 +28,24 @@ public void Teardown () proj = null; } + [Test] + public void DotNetRun ([Values (true, false)] bool isRelease) + { + var proj = new XamarinAndroidApplicationProject { + IsRelease = isRelease + }; + using var builder = CreateApkBuilder (); + builder.Save (proj); + + var dotnet = new DotNetCLI (Path.Combine (Root, builder.ProjectDirectory, proj.ProjectFilePath)); + Assert.IsTrue (dotnet.Build (), "`dotnet build` should succeed"); + Assert.IsTrue (dotnet.Run (), "`dotnet run --no-build` should succeed"); + + bool didLaunch = WaitForActivityToStart (proj.PackageName, "MainActivity", + Path.Combine (Root, builder.ProjectDirectory, "logcat.log"), 30); + Assert.IsTrue (didLaunch, "Activity should have started."); + } + [Test] public void NativeAssemblyCacheWithSatelliteAssemblies ([Values (true, false)] bool enableMarshalMethods) {