Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tasks] implement dotnet run with an MSBuild …
Browse files Browse the repository at this point in the history
…target

Context: dotnet/sdk#42155
Context: dotnet/sdk#42240
Fixes: dotnet/sdk#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 `<FastDeploy/>` 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`.
  • Loading branch information
jonathanpeppers committed Oct 30, 2024
1 parent aa668a5 commit 8836846
Showing 1 changed file with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ This file contains targets specific for Android application projects.
<UseAppHost>false</UseAppHost>
<!-- see: https://github.com/xamarin/xamarin-macios/blob/a6eb528197854c074d9dd5847328c096890337be/dotnet/targets/Xamarin.Shared.Sdk.props#L38-L52 -->
<_RuntimeIdentifierUsesAppHost>false</_RuntimeIdentifierUsesAppHost>
<RunCommand>dotnet</RunCommand>
<RunArguments>build &quot;$(MSBuildProjectFullPath)&quot; -target:Run --configuration &quot;$(Configuration)&quot;</RunArguments>

<!-- If Xamarin.Android.Common.Debugging.targets exists, we can rely on _Run for debugging. -->
<_RunDependsOn Condition=" '$(_XASupportsFastDev)' == 'true' ">
Expand All @@ -27,8 +25,29 @@ This file contains targets specific for Android application projects.
Install;
StartAndroidActivity;
</_RunDependsOn>
<_AndroidComputeRunArgumentsDependsOn>
Install;
</_AndroidComputeRunArgumentsDependsOn>
</PropertyGroup>

<Target Name="_AndroidComputeRunArguments"
BeforeTargets="ComputeRunArguments"
DependsOnTargets="$(_AndroidComputeRunArgumentsDependsOn)">
<GetAndroidActivityName
Condition=" '$(AndroidLaunchActivity)' == '' "
ManifestFile="$(IntermediateOutputPath)android\AndroidManifest.xml">
<Output TaskParameter="ActivityName" PropertyName="AndroidLaunchActivity" />
</GetAndroidActivityName>
<PropertyGroup>
<RunCommand>$(AdbToolExe)</RunCommand>
<RunCommand Condition=" $(RunCommand) == '' and $([MSBuild]::IsOSPlatform('windows')) ">adb.exe</RunCommand>
<RunCommand Condition=" $(RunCommand) == '' and !$([MSBuild]::IsOSPlatform('windows')) ">adb</RunCommand>
<RunCommand>$([System.IO.Path]::Combine ('$(AdbToolPath)', '$(RunCommand)'))</RunCommand>
<RunArguments>$(AdbTarget) shell am start -S -n &quot;$(_AndroidPackage)/$(AndroidLaunchActivity)&quot;</RunArguments>
<RunWorkingDirectory>$(MSBuildProjectDirectory)</RunWorkingDirectory>
</PropertyGroup>
</Target>

<Target Name="Run" DependsOnTargets="$(_RunDependsOn)" />

<PropertyGroup>
Expand Down

0 comments on commit 8836846

Please sign in to comment.