From 0c63e0d8d7183df2f818efc18537c73be800d80e Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Thu, 23 Apr 2020 13:53:42 -0500 Subject: [PATCH] [.NET 5] add a `Run` MSBuild target Currently with .NET 5, you can build, deploy, and run an app with: dotnet publish foo.csproj -t:Install -t:StartAndroidActivity This doesn't match the behavior of other platforms that use `dotnet run` to launch. Unfortunately `dotnet run` won't work in its current state: Unable to run your project. Ensure you have a runnable project type and ensure 'dotnet run' supports this project. A runnable project should target a runnable TFM (for instance, netcoreapp2.0) and have OutputType 'Exe'. The current OutputType is 'Library'. But we can supply a `Run` target to start Android applications: https://github.com/dotnet/sdk/blob/645919b8e850771a73f2f7a31b97038d6bdf1092/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.targets#L755-L758 This at least allows this command to work: dotnet publish foo.csproj -t:Run I updated a test to use this command as well. --- .../targets/Microsoft.Android.Sdk.targets | 3 +++ .../Tests/Xamarin.ProjectTools/Common/DotNetCLI.cs | 7 +++++++ tests/MSBuildDeviceIntegration/Tests/XASdkDeployTests.cs | 3 +-- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.targets b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.targets index 8d48d505226..352c3c25612 100644 --- a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.targets +++ b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.targets @@ -20,4 +20,7 @@ + + + diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Common/DotNetCLI.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Common/DotNetCLI.cs index dc8960c8744..eb36f9b4e17 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Common/DotNetCLI.cs +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Common/DotNetCLI.cs @@ -85,6 +85,13 @@ public bool Publish (string target = null) return Execute (arguments.ToArray ()); } + public bool Run () + { + //TODO: this should eventually run `dotnet run --project foo.csproj` + var arguments = GetDefaultCommandLineArgs ("publish", "Run"); + return Execute (arguments.ToArray ()); + } + public IEnumerable LastBuildOutput { get { if (!string.IsNullOrEmpty (BuildLogFile) && File.Exists (BuildLogFile)) { diff --git a/tests/MSBuildDeviceIntegration/Tests/XASdkDeployTests.cs b/tests/MSBuildDeviceIntegration/Tests/XASdkDeployTests.cs index 114c905319b..f7e9ff3209e 100644 --- a/tests/MSBuildDeviceIntegration/Tests/XASdkDeployTests.cs +++ b/tests/MSBuildDeviceIntegration/Tests/XASdkDeployTests.cs @@ -36,8 +36,7 @@ public void DotNetInstallAndRun ([Values (false, true)] bool isRelease) proj.CopyNuGetConfig (relativeProjDir); var dotnet = new DotNetCLI (proj, Path.Combine (fullProjDir, proj.ProjectFilePath)); - Assert.IsTrue (dotnet.Publish ("Install"), "`dotnet publish /t:Install` should succeed"); - AdbStartActivity ($"{proj.PackageName}/{proj.JavaPackageName}.MainActivity"); + Assert.IsTrue (dotnet.Run (), "`dotnet run` should succeed"); bool didLaunch = WaitForActivityToStart (proj.PackageName, "MainActivity", Path.Combine (fullProjDir, "logcat.log"), 30); RunAdbCommand ($"uninstall {proj.PackageName}");