Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Xamarin.Android.Build.Tasks] implement dotnet run with an MSBuild target #9470

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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 @@ -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>
Comment on lines -18 to -19
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that before, we had no way to parse all the -p arguments passed to dotnet run through to the underlying Run target. We had $(Configuration) hardcoded here, but that was the only one that worked.


<!-- 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>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Elsewhere, we support $(AdbToolPath) + $(AdbToolExe). As-is, it looks like this requires that $(AdbToolExe)/adb be in %PATH%

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should do at least two things:

  1. Depend upon the _ResolveSdks target, so that we can use $(_AndroidSdkDirectory)
  2. Have $(RunCommand) use Path.Combine($(_AndroidSdkDirectory), $(AdbToolExe)) when they're not empty.
<_AdbToolExe>$(AdbToolExe)</_AdbToolExe>
<_AdbToolExe Condition=" '$(_AdbToolExe)' == '' and $([MSBuild]::IsOSPlatform('windows')) ">adb.exe</_AdbToolExe>
<_AdbToolExe Condition=" '$(_AdbToolExe)' == '' and !$([MSBuild]::IsOSPlatform('windows')) ">adb</_AdbToolExe>

<RunCommand Condition=" '$(_AndroidSdkDirectory)' != '' ">$([System.IO.Path]::Combine($(_AndroidSdkDirectory), $(_AdbToolExe)))</RunCommand>
<RunCommand Condition=" '$(RunCommand)' == '' ">$(_AdbToolExe)</RunCommand>

This way, we can still invoke adb even if it isn't in $PATH, and we'll use the same Android SDK adb that was used to build the rest of the project.

<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
18 changes: 18 additions & 0 deletions tests/MSBuildDeviceIntegration/Tests/InstallAndRunTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down