Skip to content

[Xamarin.Android.Build.Tasks] Enable R8 resource shrinking - #13866

Open
jonathanpeppers wants to merge 6 commits into
mainfrom
jonathanpeppers-r8-resource-shrinking
Open

jonathanpeppers wants to merge 6 commits into
mainfrom
jonathanpeppers-r8-resource-shrinking

Conversation

@jonathanpeppers

@jonathanpeppers jonathanpeppers commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Agent assessment

Self-reported assessment, not an independent review.

Assessed revision: aa122f4ad263e44ede1b2c00c237d22cf48c5f80

Dimension Assessment Rationale
Change confidence 8.5/10 ↑ Build task project compiles with 0 warnings; both focused AAB tests pass with the System.Reflection.Metadata scanner and XmlWriter keep file.
↓ The IL reader is new code; CI was not rechecked after the latest commit.
Diagnosis confidence 9/10 ↑ The no-designer build reproduced missing R.txt; tests cover metadata references and package-qualified names.
Evidence fit 8/10 ↑ AAB checks confirm referenced resources remain and unused resources are removed.
↓ Google Play has not checked a bundle from this revision.

Fixes: #13252

Google Play reports resource shrinking as disabled for .NET for Android app bundles. R8 also needs to keep resources referenced by managed metadata and dynamic names.

Run AAPT2 linking when R8 shrinking needs resource symbols, even if resource designer generation is off. <GenerateR8ResourcesInput/> uses System.Reflection.Metadata to scan user and NuGet assemblies for resource IDs and names in IL, metadata constants, field data, and custom-attribute arguments. It recognizes package-qualified names used by Resources.GetIdentifier. The resource designer assembly is not read: its member names are the R.txt names, so a reference to a designer member is matched by name.

Shrinking remains AAB-only; APK and D8 builds are unchanged. Set $(AndroidR8ShrinkResources) to false to opt out. Use tools:keep for computed resource names.

Validation:

  • .\dotnet-local.cmd build src\Xamarin.Android.Build.Tasks\Xamarin.Android.Build.Tasks.csproj -c Debug -v:minimal — passed.
  • .\dotnet-local.cmd build src\Xamarin.Android.Build.Tasks\Tests\Xamarin.Android.Build.Tests\Xamarin.Android.Build.Tests.csproj -c Debug -v:minimal — passed.
  • .\bin\Debug\dotnet\dotnet.exe <test-copy>\Xamarin.Android.Build.Tests.dll --filter 'FullyQualifiedName~PackagingTest.R8ShrinkResources' — 2 passed.
  • git diff --check — passed.

The focused tests ran against a temporary test-assembly copy marked [NoClassCleanup] to avoid stopping the shared ADB server.

Google Play reports that resource shrinking is disabled for Android app bundles. Enable optimized R8 shrinking by default when `$(AndroidLinkTool)` is `r8`, and retain resources referenced by managed code. APK builds are unchanged; set `$(AndroidR8ShrinkResources)` to `false` to opt out.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings December 1, 2026 20:21

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 change can fail clean AAB/R8 builds when designer generation is disabled and incorrectly strip resources referenced through package-qualified lookups, custom attributes, or reflected constants.

3 open issues
What changed in this PR

This PR adds default-enabled R8 resource shrinking for .NET for Android app bundles, with managed resource-reference preservation. APK builds remain unchanged.

Changes:

  • Generates resource keep rules from managed assemblies and passes resource archives to R8.
  • Packages the shrunk resources and updates incremental-build inputs and outputs.
  • Adds packaging coverage and documents the opt-out property and tools:keep guidance.
File Description
src/​Xamarin.Android.Build.Tasks/​Xamarin.Android.D8.targets Runs resource-input generation and connects resource archives to R8.
src/​Xamarin.Android.Build.Tasks/​Xamarin.Android.Common.targets Enables shrinking for R8/AAB builds and packages its output.
src/​Xamarin.Android.Build.Tasks/​Tests/​Xamarin.Android.Build.Tests/​PackagingTest.cs Tests managed resource retention, unused-resource removal, and shrinking metadata.
src/​Xamarin.Android.Build.Tasks/​Tasks/​R8.cs Adds resource archive arguments to the R8 invocation.
src/​Xamarin.Android.Build.Tasks/​Tasks/​GenerateR8ResourcesInput.cs Scans managed references and generates resource keep rules.
Documentation/​docs-mobile/​building-apps/​build-properties.md Documents AndroidR8ShrinkResources and computed-name preservation.

Review effort: Balanced

jonathanpeppers and others added 5 commits December 1, 2026 16:12
Generate R.txt when R8 shrinking runs without resource designers. Scan metadata constants and attribute arguments, and support package-qualified resource names.

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

Replace Mono.Cecil with System.Reflection.Metadata. Do not read the resource designer assembly: its member names are the R.txt names, so a reference to a designer member is matched by name. Styleable arrays from R.txt keep the attributes they list.

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

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.

Support R8 resource shrinking to reduce app size and resolve Play Console warnings

2 participants