Skip to content

[release/11.0.1xx] [R8] Preserve bound Java fields in generated keep rules - #13239

Merged
jonathanpeppers merged 2 commits into
release/11.0.1xxfrom
jonathanpeppers-r8-field-preservation
Oct 23, 2026
Merged

jonathanpeppers merged 2 commits into
release/11.0.1xxfrom
jonathanpeppers-r8-field-preservation

Conversation

@jonathanpeppers

Copy link
Copy Markdown
Member

Fixes #13159.

.NET 11 RC2 can remove Java fields that managed code accesses through bindings when AndroidLinkTool=r8 and AndroidR8ObfuscationMode=private-members. Uno's AccessibilityActionCompat.ActionCollapse?.Id ?? 0 reproduces the problem: the Java class remains, but its ACTION_COLLAPSE field is removed.

GenerateProguardConfiguration previously generated member keep rules only from registered methods. Generate field keep rules from [Register] attributes on managed properties and fields as well. These rules preserve the Java field name and type without disabling optimization for the rest of the class.

This is one commit on release/11.0.1xx. It does not include the larger retained-typemap changes from main.

Add a CoreCLR Release build test using Xamarin.AndroidX.Core and Uno's field-access pattern. Check the generated rule and use the existing dexdump utility to assert the field's declaring class, name, and type for both private-members and disabled. Add unit coverage for the DEX field lookup.

Validation on Windows, with MSBUILDDISABLENODEREUSE=1, DOTNET_ROLL_FORWARD=Major, and TEST_ANDROID_JDK_PATH=D:\android-toolchain\jdk-25:

  • .\dotnet-local.cmd build Microsoft.Android.slnx -c Debug -nodeReuse:false -v:quiet — passed.
  • .\dotnet-local.cmd build Microsoft.Android.slnx -t:PackDotNet -c Debug -nodeReuse:false -v:minimal — passed.
  • .\dotnet-local.cmd build src\Xamarin.Android.Build.Tasks\Tests\Xamarin.Android.Build.Tests\Xamarin.Android.Build.Tests.csproj -c Debug -nodeReuse:false -v:quiet — passed.
  • .\dotnet-local.cmd test bin\TestDebug\net10.0\Xamarin.Android.Build.Tests.dll --filter Name~R8PreservesAccessibilityActionField — 2 passed.
  • .\dotnet-local.cmd test bin\TestDebug\net10.0\Xamarin.Android.Build.Tests.dll --filter FullyQualifiedName~DexUtilsTests — 14 passed.
  • .\dotnet-local.cmd test bin\TestDebug\net10.0\Xamarin.Android.Build.Tests.dll --filter FullyQualifiedName~R8Tests — 26 passed.

Pull Request
title and
description
should follow the
commit-messages.md workflow documentation, and in particular should include:

  • Useful description of why the change is necessary.
  • Links to issues fixed
  • Unit tests

Generate field keep rules from Register attributes on managed properties and fields so R8 cannot remove or change fields accessed through JNI. Add the Uno ACTION_COLLAPSE build regression for both obfuscation modes and check the field name and type with dexdump.

Fixes #13159

Co-authored-by: Copilot App <[email protected]>

Copilot-Session: e1ff07de-4ade-4aa2-8af0-dc4f4ded065a
Copilot AI balanced review requested due to automatic review settings October 22, 2026 13:45
@jonathanpeppers jonathanpeppers changed the title [R8] Preserve bound Java fields in generated keep rules [release/11.0.1xx] [R8] Preserve bound Java fields in generated keep rules Oct 22, 2026

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.

🔵 Needs a closer look

The reported instance-field type-narrowing scenario remains unverified by the regression coverage and needs human validation.

1 open issue
What changed in this PR

This servicing fix addresses #13159 by extending generated R8 keep rules to protect Java fields accessed through managed bindings, without broadly disabling class optimization.

Changes:

  • Generate field keep rules from [Register] attributes on managed properties and fields.
  • Add an AndroidX regression test for both obfuscation modes, plus DEX field lookup support and unit coverage.
  • Document preservation of bound Java field names and types.
File Description
src/​Xamarin.Android.Build.Tasks/​Tests/​Xamarin.ProjectTools/​Utilities/​DexUtils.cs Share member lookup logic and support field checks.
src/​Xamarin.Android.Build.Tasks/​Tests/​Xamarin.Android.Build.Tests/​PackagingTest.cs Verify AndroidX field preservation after R8 processing.
src/​Xamarin.Android.Build.Tasks/​Tests/​Xamarin.Android.Build.Tests/​DexUtilsTests.cs Test exact field matching by class, name, and type.
src/​Xamarin.Android.Build.Tasks/​Tasks/​GenerateProguardConfiguration.cs Emit keep rules for registered properties and fields.
Documentation/​docs-mobile/​building-apps/​build-properties.md Explain bound-field preservation guarantees.

Review effort: Balanced

Bind a Java instance int field whose values are limited to 0, 1, and 2. Keep a Java reader live so the field remains present even without generated field rules, then assert the original I descriptor with dexdump.

Verified that omitting field keep rules narrows the field to B and fails the test. With the fix restored, all three static and instance field regression cases pass.

Co-authored-by: Copilot App <[email protected]>

Copilot-Session: e1ff07de-4ade-4aa2-8af0-dc4f4ded065a
@jonathanpeppers jonathanpeppers added the ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable). label Oct 23, 2026
@jonathanpeppers

Copy link
Copy Markdown
Member Author

@dalexsoto review

1 similar comment
@jonathanpeppers

Copy link
Copy Markdown
Member Author

@dalexsoto review

@dalexsoto dalexsoto left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The servicing field rules preserve bound Java field names and types, and exact DEX assertions address both field removal and retained-int narrowing without requiring broader main-branch typemap changes.

Source-only review of the full current PR and inspected static integration; no runtime or installed-product execution is claimed.

@jonathanpeppers
jonathanpeppers merged commit 707d5dc into release/11.0.1xx Oct 23, 2026
44 checks passed
@jonathanpeppers
jonathanpeppers deleted the jonathanpeppers-r8-field-preservation branch October 23, 2026 19:05
jonathanpeppers added a commit that referenced this pull request Oct 26, 2026
Add the test coverage from #13239 to main. These tests protect managed JNI field access from R8 field removal, renaming, and type narrowing. Related issue: #13159.

The static-field test builds a trimmed CoreCLR Release app with Xamarin.AndroidX.Core 1.19.0.1 and checks `ACTION_COLLAPSE` in DEX output with obfuscation disabled and with `private-members`. The instance-field test binds a Java `int` field with values 0, 1, and 2, keeps its Java reader live, and checks that R8 preserves its `I` descriptor instead of narrowing it to `B`.

Add exact field class/name/type matching to the shared DEX test helper and unit coverage for that matching. Main already preserves these fields through its typemap member rules, so this PR contains no production or documentation changes and does not assert the release-specific generated rule.

Co-authored-by: Copilot App <[email protected]>
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 23, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants