Repository navigation
[Xamarin.Android.Build.Tasks] Enable R8 resource shrinking - #13866
Open
jonathanpeppers wants to merge 6 commits into
Open
jonathanpeppers wants to merge 6 commits into
jonathanpeppers wants to merge 6 commits into
Conversation
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>
Contributor
There was a problem hiding this comment.
🟡 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:keepguidance.
| 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
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Agent assessment
Self-reported assessment, not an independent review.
Assessed revision:
aa122f4ad263e44ede1b2c00c237d22cf48c5f80XmlWriterkeep file.↓ The IL reader is new code; CI was not rechecked after the latest commit.
R.txt; tests cover metadata references and package-qualified names.↓ 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 byResources.GetIdentifier. The resource designer assembly is not read: its member names are theR.txtnames, so a reference to a designer member is matched by name.Shrinking remains AAB-only; APK and D8 builds are unchanged. Set
$(AndroidR8ShrinkResources)tofalseto opt out. Usetools:keepfor 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.