Skip to content

[Xamarin.Android.Build.Tasks] Replace GetAndroidActivityName with XmlPeek - #13493

Open
simonrozsival wants to merge 1 commit into
mainfrom
simonrozsival-xmlpeek-get-android-activity-name
Open

simonrozsival wants to merge 1 commit into
mainfrom
simonrozsival-xmlpeek-get-android-activity-name

Conversation

@simonrozsival

Copy link
Copy Markdown
Member

Context: #13481
Context: #13484

GetAndroidActivityName is a custom MSBuild task. It loads the merged AndroidManifest.xml through AndroidAppManifest (which also needs ReferenceAssemblyPaths to build a version resolver) just to return the first launchable user activity. Every custom task has to be hosted, loaded and kept multithreading-ready, and this one does nothing a single XPath query can't. This continues the effort to replace small tasks with plain MSBuild.

This adds a shared _GetAndroidLaunchableActivity target to Xamarin.Android.Common.targets. It reads $(IntermediateOutputPath)android\AndroidManifest.xml with the built-in <XmlPeek ProhibitDtd="true"/> and sets $(_AndroidLaunchableActivity). It keeps the selection order of AndroidAppManifest.GetLaunchableUserActivityName():

  • Look only at the first <application>.
  • Return the first <activity> whose <intent-filter> has the android.intent.category.LAUNCHER category.
  • If there is none, fall back to the first matching <activity-alias>.
  • Skip empty names and mono.android.__FastDevLauncher.

The query uses the (…)[1]/@android:name form because XmlPeek evaluates its query with XPathNavigator.Select(), so a scalar string(…) expression isn't allowed.

_AndroidComputeRunArguments (dotnet run), RunWithLogging and RunTests now depend on this target and copy its value into $(AndroidLaunchActivity) or $(TargetTestActivity) under the same conditions as before.

The target deliberately has no Inputs/Outputs. It produces only a property, and MSBuild can't infer an XmlPeek result for a skipped target, so the property would come back empty.

Removed: the GetAndroidActivityName task, both of its <UsingTask/> registrations (in Microsoft.Android.Sdk.Application.targets and Xamarin.Android.Common.Debugging.targets), and its two unit tests.

Behavior differences:

  • If the merged manifest is missing, the activity is now empty instead of the task throwing.
  • Like the C# implementation, the query checks only the LAUNCHER category and not the MAIN action.

Testing: not built or tested locally, because of disk-space constraints for this batch; CI validates this PR. The dotnet run/ComputeRunArguments, RunWithLogging and RunTests paths, including the activity-alias and instrumentation fallbacks, need CI coverage.

…Peek

Add a shared `_GetAndroidLaunchableActivity` target to
Xamarin.Android.Common.targets that reads the merged
`$(IntermediateOutputPath)android\AndroidManifest.xml` with
`<XmlPeek ProhibitDtd="true"/>` and sets `$(_AndroidLaunchableActivity)`
to the first `<activity>` (falling back to the first `<activity-alias>`)
in the first `<application>` whose `<intent-filter>` has the
`android.intent.category.LAUNCHER` category, skipping empty names and
`mono.android.__FastDevLauncher`. This mirrors
`AndroidAppManifest.GetLaunchableUserActivityName()`.

`_AndroidComputeRunArguments`, `RunWithLogging` and `RunTests` now
depend on this target instead of calling `<GetAndroidActivityName/>`.

Remove the `GetAndroidActivityName` task, its `<UsingTask/>`
registrations and its unit tests.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The new dependency causes all three device-selection test cases to fail with MSB4057 because their generated project does not define _GetAndroidLaunchableActivity.

2 open issues
What changed in this PR

Replaces the custom GetAndroidActivityName task with shared MSBuild XmlPeek queries, continuing the effort to simplify small build tasks and reduce task-hosting overhead.

Changes:

  • Adds launcher discovery with activity-over-alias precedence and filtering for empty and FastDev launcher names.
  • Routes run-argument computation, logging, and test launching through the shared target.
  • Removes the custom task, its registrations, and its task-specific tests.
File Description
src/​Xamarin.Android.Build.Tasks/​Xamarin.Android.Common.targets Adds shared XPath-based launcher discovery.
src/​Xamarin.Android.Build.Tasks/​Tests/​Xamarin.Android.Build.Tests/​Tasks/​ResolveSdksTaskTests.cs Removes the task’s parallel project-path tests.
src/​Xamarin.Android.Build.Tasks/​Tests/​Xamarin.Android.Build.Tests/​Tasks/​MultiThreadedMigrationTests.cs Removes the task’s launcher-selection tests and unused import.
src/​Xamarin.Android.Build.Tasks/​Tasks/​GetAndroidActivityName.cs Deletes the custom manifest-reading task.
src/​Xamarin.Android.Build.Tasks/​Microsoft.Android.Sdk/​targets/​Microsoft.Android.Sdk.Application.targets Uses shared discovery for run arguments and logging.
src/​Xamarin.Android.Build.Debugging.Tasks/​Xamarin.Android.Common.Debugging.targets Uses shared discovery for RunTests and removes task registration.

Review effort: Balanced

<Target Name="_AndroidComputeRunArguments"
BeforeTargets="ComputeRunArguments"
DependsOnTargets="$(_AndroidComputeRunArgumentsDependsOn)">
DependsOnTargets="$(_AndroidComputeRunArgumentsDependsOn);_GetAndroidLaunchableActivity">
XmlInputPath="$(IntermediateOutputPath)android\AndroidManifest.xml"
ProhibitDtd="true"
Namespaces="&lt;Namespace Prefix='android' Uri='http://schemas.android.com/apk/res/android' /&gt;"
Query="(/manifest/application[1]/activity[intent-filter/category/@android:name='android.intent.category.LAUNCHER' and @android:name!='' and @android:name!='mono.android.__FastDevLauncher'])[1]/@android:name">

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants