Skip to content

Fix NativeAOT to use AndroidLinkMode=Full instead of SdkOnly - #10809

Merged
simonrozsival merged 3 commits into
mainfrom
copilot/fix-android-link-mode-defaults
Feb 13, 2026
Merged

Fix NativeAOT to use AndroidLinkMode=Full instead of SdkOnly#10809
simonrozsival merged 3 commits into
mainfrom
copilot/fix-android-link-mode-defaults

Conversation

Copilot AI commented Feb 12, 2026

Copy link
Copy Markdown
Contributor

NativeAOT was getting AndroidLinkMode=SdkOnly (BCL-only trimming) but TrimMode=full (aggressive trimming), creating a contradiction.

Changes

  • Add explicit AndroidLinkMode=Full default for NativeAOT before existing fallback conditions
  • Remove redundant '$(_AndroidRuntime)' == 'NativeAOT' check from TrimMode condition—now follows naturally from AndroidLinkMode=Full
  • Fix indentation on adjacent line to match .editorconfig (spaces not tabs)
<!-- Before: NativeAOT fell through to SdkOnly via PublishTrimmed -->
<AndroidLinkMode Condition=" '$(AndroidLinkMode)' == '' and '$(PublishTrimmed)' == 'true' ">SdkOnly</AndroidLinkMode>
<TrimMode Condition=" '$(TrimMode)' == '' and ('$(AndroidLinkMode)' == 'Full' or '$(_AndroidRuntime)' == 'NativeAOT') ">full</TrimMode>

<!-- After: NativeAOT gets Full explicitly -->
<AndroidLinkMode Condition=" '$(AndroidLinkMode)' == '' and '$(_AndroidRuntime)' == 'NativeAOT' ">Full</AndroidLinkMode>
<AndroidLinkMode Condition=" '$(AndroidLinkMode)' == '' and '$(PublishTrimmed)' == 'true' ">SdkOnly</AndroidLinkMode>
<TrimMode Condition=" '$(TrimMode)' == '' and '$(AndroidLinkMode)' == 'Full' ">full</TrimMode>

Non-NativeAOT configurations (CoreCLR, MonoVM, Debug) remain unchanged.

Original prompt

Problem

In src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.DefaultProperties.targets, when using NativeAOT in Release configuration, AndroidLinkMode defaults to SdkOnly but TrimMode is forced to full. This is contradictory — SdkOnly implies only BCL/SDK assemblies are trimmed, but TrimMode=full aggressively trims all assemblies.

The relevant code (lines 104-111):

    <PublishTrimmed Condition=" '$(PublishTrimmed)' == '' and ('$(AndroidLinkMode)' == 'SdkOnly' or '$(AndroidLinkMode)' == 'Full') ">true</PublishTrimmed>
    <PublishTrimmed Condition=" '$(PublishTrimmed)' == '' and '$(Configuration)' == 'Release' and '$(AndroidLinkMode)' != 'None' ">true</PublishTrimmed>
    <AndroidLinkMode Condition=" '$(AndroidLinkMode)' == '' and '$(PublishTrimmed)' == 'true' ">SdkOnly</AndroidLinkMode>
    <AndroidLinkMode Condition=" '$(AndroidLinkMode)' == '' ">None</AndroidLinkMode>
    <!-- For compat with user code not marked trimmable, only trim opt-in by default. -->
    <TrimMode Condition=" '$(TrimMode)' == '' and ('$(AndroidLinkMode)' == 'Full' or '$(_AndroidRuntime)' == 'NativeAOT') ">full</TrimMode>
    <TrimMode Condition="'$(TrimMode)' == ''">partial</TrimMode>

Fix

  1. Set AndroidLinkMode=Full by default for NativeAOT: Add a new default before the existing AndroidLinkMode fallback defaults (before line 106) so that NativeAOT gets Full instead of falling through to SdkOnly:
<AndroidLinkMode Condition=" '$(AndroidLinkMode)' == '' and '$(_AndroidRuntime)' == 'NativeAOT' ">Full</AndroidLinkMode>

This should go after the PublishTrimmed lines but before the existing AndroidLinkMode defaults on lines 106-107.

  1. Remove the redundant '$(_AndroidRuntime)' == 'NativeAOT' check from the TrimMode condition on line 109: Since NativeAOT now gets AndroidLinkMode=Full, TrimMode=full will follow naturally from the AndroidLinkMode == 'Full' branch. Change line 109 from:
<TrimMode Condition=" '$(TrimMode)' == '' and ('$(AndroidLinkMode)' == 'Full' or '$(_AndroidRuntime)' == 'NativeAOT') ">full</TrimMode>

to:

<TrimMode Condition=" '$(TrimMode)' == '' and '$(AndroidLinkMode)' == 'Full' ">full</TrimMode>

The file to edit is: src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.DefaultProperties.targets

This pull request was created from Copilot chat.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits February 12, 2026 19:03
- Add AndroidLinkMode=Full default for NativeAOT before existing defaults
- Remove redundant NativeAOT check from TrimMode condition
- This resolves the contradiction where NativeAOT would get SdkOnly link mode but full trimming

Co-authored-by: sbomer <787361+sbomer@users.noreply.github.com>
Co-authored-by: sbomer <787361+sbomer@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix AndroidLinkMode and TrimMode conflict in Release configuration Fix NativeAOT to use AndroidLinkMode=Full instead of SdkOnly Feb 12, 2026
Copilot AI requested a review from sbomer February 12, 2026 19:11
@sbomer
sbomer marked this pull request as ready for review February 12, 2026 23:01
Copilot AI review requested due to automatic review settings February 12, 2026 23:01

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.

Pull request overview

Adjusts MSBuild default trimming/linking properties so NativeAOT builds don’t end up with conflicting defaults (AndroidLinkMode=SdkOnly vs TrimMode=full) in the .NET for Android SDK targets.

Changes:

  • Default AndroidLinkMode to Full when $(_AndroidRuntime) is NativeAOT, ahead of existing fallback defaults.
  • Simplify the TrimMode default condition to follow from AndroidLinkMode=Full (removing the redundant NativeAOT runtime check).
  • Fix indentation on an adjacent line to match repository formatting.

@simonrozsival
simonrozsival merged commit 4282327 into main Feb 13, 2026
12 checks passed
@simonrozsival
simonrozsival deleted the copilot/fix-android-link-mode-defaults branch February 13, 2026 10:05
@github-actions github-actions Bot locked and limited conversation to collaborators Mar 16, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants